关于 hetzner-server
hetzner-server 是指导使用 hcloud CLI 创建和管理 Hetzner Cloud 服务器的技能,面向需要部署和维护云服务器的操作者。内容涵盖防火墙、初始化、SSH 配置及访问方式。
主要功能
- 01使用 hcloud CLI 创建服务器
- 02创建时应用可复用防火墙配置
- 03通过 cloud-init 执行初始化脚本
- 04配置 swap 并检查安装进度
- 05同步服务器 SSH 配置
- 06使用 SSH Agent 转发克隆私有仓库
适用场景
- 01创建带初始化脚本的 Hetzner Cloud 服务器
- 02在服务器创建后更换防火墙规则
- 03为服务器设置 swap 并核验状态
- 04通过转发的 SSH Agent 访问私有 GitHub 仓库
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ connorads/ dotfiles --skill hetzner-server安装后在 Claude Code 中输入/hetzner-server确认已加载。
详细介绍
Hetzner 服务器管理
使用 hcloud CLI 创建和管理 Hetzner Cloud 服务器。
前置条件
- 已安装
hcloudCLI(通过 mise:hcloud = "latest") - 已通过 https://console.hetzner.cloud 获取 API 令牌,并运行
hcloud context create <name>完成身份验证
Cloud 防火墙
可重复使用的防火墙配置文件,可在创建服务器时应用。运行中的服务器也可以更换防火墙——使用 apply-to-resource / remove-from-resource。
| 防火墙 | 规则 | 使用场景 |
|---|---|---|
ts-ssh | UDP 41641(Tailscale)+ TCP 22(SSH) | 开发机器——用于初始设置;将 UFW 限制为仅允许 Tailscale 后,换用 ts-only |
ts-only | UDP 41641(Tailscale) | 仅允许通过 Tailscale 访问,不开放公网端口 |
ts-web | UDP 41641(Tailscale)+ TCP 80,443(HTTP/S) | 接受公网 Web 流量的服务器 |
更换运行中服务器的防火墙
hcloud firewall remove-from-resource ts-ssh --type server --server dev
hcloud firewall apply-to-resource ts-only --type server --server dev
快速参考
创建服务器
# Prefer ARM (best value)
hcloud server create \
--name dev \
--type cax21 \
--image ubuntu-24.04 \
--location nbg1 \
--ssh-key connorads \
--ssh-key connor@penguin \
--firewall ts-ssh
# x86 fallback
hcloud server create \
--name dev \
--type cpx21 \
--image ubuntu-24.04 \
--location nbg1 \
--ssh-key connorads \
--ssh-key connor@penguin \
--firewall ts-ssh
# IPv6-only (saves ~$0.60/month on IPv4)
hcloud server create \
--name dev \
--type cax21 \
--image ubuntu-24.04 \
--location nbg1 \
--ssh-key connorads \
--ssh-key connor@penguin \
--firewall ts-ssh \
--without-ipv4
仅 IPv6 注意事项: hcloud server ip <name> 和 hcssh 都假定使用 IPv4——
server ip 不会输出有用信息,而 hcssh 只读取 ipv4 列,因此生成的 Host 条目会为空。对于 --without-ipv4 服务器,请使用
hcloud server ip --ipv6 <name>(返回 /64;主机地址为 ...::1),并
手动编写 SSH Host 条目;或者在运行 ts up 后,直接通过 Tailscale 连接。
使用 user-data(自动运行安装脚本)
# Use heredoc - process substitution <(echo '...') escapes the shebang incorrectly
hcloud server create \
--name dev \
--type cax21 \
--image ubuntu-24.04 \
--location nbg1 \
--ssh-key connorads \
--ssh-key connor@penguin \
--firewall ts-ssh \
--user-data-from-file - <<'EOF'
#!/bin/bash
curl -fsSL https://raw.githubusercontent.com/connorads/dotfiles/master/install.sh | bash
EOF
dotfiles 安装大约需要 5 分钟。监控进度:
# Quick status check
ssh connor@$(hcloud server ip dev) "cloud-init status"
# View recent installation logs
ssh connor@$(hcloud server ip dev) "sudo journalctl -u cloud-final -n 50 --no-pager"
# Follow installation in real-time
ssh connor@$(hcloud server ip dev) "sudo journalctl -u cloud-final -f"
# Check if tools are installed
ssh connor@$(hcloud server ip dev) "which zsh mise && echo \$SHELL"
配置 swap(生产环境推荐)
Ubuntu 云镜像默认不包含 swap。创建时通过 cloud-init 添加 swap:
# Create server with 16GB swap (1:1 ratio for 16GB RAM server)
hcloud server create \
--name dev \
--type cax31 \
--image ubuntu-24.04 \
--location nbg1 \
--ssh-key connorads \
--ssh-key connor@penguin \
--firewall ts-ssh \
--user-data-from-file - <<'EOF'
#cloud-config
swap:
filename: /swapfile
size: 16G
maxsize: 16G
EOF
推荐的 swap 大小:
- 4GB RAM → 4-8GB swap
- 8GB RAM → 8GB swap
- 16GB 及以上 RAM → 16GB swap(1:1 比例)
为现有服务器添加 swap:
# Create 16GB swap file
ssh connor@$(hcloud server ip dev) "sudo fallocate -l 16G /swapfile && \
sudo chmod 600 /swapfile && \
sudo mkswap /swapfile && \
sudo swapon /swapfile && \
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab"
# Verify swap is active
ssh connor@$(hcloud server ip dev) "free -h"
查询参考信息
服务器类型、位置、镜像、SSH 密钥以及日常服务器操作都可以实时查询——需要时查询即可,无需记忆:
hcloud server-type list # ARM cax* preferred (best value); cpx* x86 fallback
hcloud location list # nbg1 is the usual default here
hcloud image list --type system --architecture arm
hcloud ssh-key list
hcloud server list|ip|delete|poweroff|poweron|reboot|rebuild # rebuild keeps the IP
克隆 GitHub 仓库(SSH Agent 转发)
使用 <name>-agent SSH 主机(已启用 Agent 转发)克隆私有仓库,无需将密钥复制到服务器。如果遇到主机密钥错误,请先添加 GitHub 的主机密钥。
# First time only: add GitHub's host key
ssh dev "ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null"
# Confirm forwarded agent is visible
ssh dev-agent "ssh-add -l"
# Clone with agent forwarding (use -agent suffix)
ssh dev-agent "mkdir -p ~/git && cd ~/git && git clone [email protected]:you/repo.git"
# Clone specific branch
ssh dev-agent "mkdir -p ~/git && cd ~/git && git clone [email protected]:you/repo.git && cd repo && git checkout branch-name"
# Push/pull with agent forwarding
ssh dev-agent "cd repo && git push"
用于交互式会话(例如 lazygit):
ssh dev-agent
# Then on server: git clone/push/pull works with forwarded agent
创建后的设置
创建服务器后,务必清除该 IP 的所有旧主机密钥(Hetzner 会重复使用 IP):
ssh-keygen -R $(hcloud server ip dev) 2>/dev/null
ssh-keyscan $(hcloud server ip dev) >> ~/.ssh/known_hosts 2>/dev/null
然后生成或更新 SSH 配置条目:
hcssh # update ~/.ssh/config with all Hetzner servers
hcssh --dry-run # preview without writing
这会在托管区块(# BEGIN/END hetzner-managed)内为每台服务器创建两个 Host 条目:
<name>- 不启用 Agent 转发(对 AI Agent 更安全)<name>-agent- 启用 Agent 转发(用于与 GitHub 进行 git push/pull)
创建或删除服务器后,请再次运行 hcssh,确保 SSH 配置保持同步。
这样 VS Code Remote-SSH 就能在下拉列表中显示该服务器。
可选:仅允许通过 Tailscale 使用 SSH
运行 ts up 并确认可通过 Tailscale 使用 SSH(ts ssh connor@dev)后,在服务器上运行以下命令,将 SSH 限制为仅通过 Tailscale 接口访问:
sudo ufw delete allow 22/tcp
sudo ufw allow in on tailscale0 to any port 22 proto tcp comment 'SSH via Tailscale'
sudo ufw reload
备用方案:如果被锁定在服务器之外,可使用 Hetzner Cloud Console VNC。
注意事项
- 仅使用 IPv6 可节省费用,但从 IPv4 网络访问需要 Tailscale/cloudflared
- user-data 首次启动时以 root 身份运行
- dotfiles 的 install.sh 会创建用户
connor,并安装 Nix、home-manager 和 mise 工具