关于 hetzner-server
通过 hcloud 命令行创建和管理 Hetzner Cloud 服务器,适用于需要操作云主机和配置远程访问的开发者。内容覆盖防火墙、SSH、初始化脚本、交换空间及服务器配置同步。
主要功能
- 01创建和查询云服务器
- 02选择服务器类型、镜像与位置
- 03配置可复用的云防火墙
- 04通过 cloud-init 初始化服务器
- 05管理 SSH 密钥和连接
- 06同步 Hetzner 服务器 SSH 配置
适用场景
- 01创建用于开发的 Hetzner Cloud 主机
- 02为服务器设置初始化脚本和交换空间
- 03限制服务器 SSH 的公开访问
- 04使用 SSH Agent 转发克隆私有仓库
安装
复制下面的命令,在终端中运行。
npx skills add https:/ / github.com/ pedronauck/ skills --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) | 开发机——用于初始设置,运行 tsonlyssh 后换为 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
使用 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 cax33 \
--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 及以上 → 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"
常用命令
# List servers
hcloud server list
# Get server IP
hcloud server ip dev
# SSH to server
ssh connor@$(hcloud server ip dev)
# Delete server
hcloud server delete dev
# Power operations
hcloud server poweroff dev
hcloud server poweron dev
hcloud server reboot dev
# Rebuild (reinstall OS, keeps IP)
hcloud server rebuild dev --image ubuntu-24.04
服务器类型(常用)
欧盟地区的价格,单位为 USD(美国地区价格约高 20%):
| 类型 | 架构 | vCPU | RAM | 磁盘 | 约 USD/月 |
|---|---|---|---|---|---|
| cax11 | ARM | 2 | 4GB | 40GB | $4.50 |
| cax21 | ARM | 4 | 8GB | 80GB | $8 |
| cax31 | ARM | 8 | 16GB | 160GB | $16 |
| cpx21 | x86 | 3 | 4GB | 80GB | $9 |
| cpx31 | x86 | 4 | 8GB | 160GB | $18 |
完整列表:hcloud server-type list
位置
| ID | 城市 | 国家 |
|---|---|---|
| fsn1 | Falkenstein | DE |
| nbg1 | Nuremberg | DE |
| hel1 | Helsinki | FI |
| ash | Ashburn | US |
| hil | Hillsboro | US |
| sin | Singapore | SG |
SSH 密钥
# List keys
hcloud ssh-key list
# Add a key
hcloud ssh-key create --name mykey --public-key-from-file ~/.ssh/id_ed25519.pub
映像
# List system images
hcloud image list --type system
# ARM images
hcloud image list --type system --architecture arm
克隆 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 推送或从 GitHub 拉取)
创建或删除服务器后,再次运行 hcssh,以保持 SSH 配置同步。
这样,VS Code Remote-SSH 就能在下拉列表中显示该服务器。
可选:仅允许通过 Tailscale 使用 SSH
运行 ts up 并确认可通过 Tailscale 使用 SSH(ts ssh connor@dev)后,在服务器上运行 tsonlyssh,从 UFW 中移除公共端口 22。此后只能通过 Tailscale 接口访问 SSH。
备用方案:如果无法访问,可使用 Hetzner Cloud Console VNC。
备注
- ARM(cax*)服务器最适合开发工作,性价比最高
- 仅使用 IPv6 可以省钱,但要从 IPv4 网络访问,必须使用 Tailscale/cloudflared
- 首次启动时,user-data 以 root 身份运行
- dotfiles 的 install.sh 负责创建用户
connor,安装 Nix、home-manager 和 mise 工具