從 Windows Putty 遷移至 Linux 原生 OpenSSH 管理 VPS,能獲得更強的自動化能力、更完整的協定支援 (Ed25519、FIDO2)、原生代理指令與終端機多工器整合。本文涵蓋 Ed25519 金鑰生成與分發、SSH Config 別名與多跳代理、ControlMaster 連線復用、systemd 服務化持久化、代理穿透 (corkscrew/ProxyJump/ncat)、安全加固清單 完整實戰指南。
# 1. 生成 Ed25519 金鑰對 (比 RSA 3072 更安全、更快、更短)
ssh-keygen -t ed25519 -C "vps-admin-$(date +%Y%m%d)" -f ~/.ssh/id_ed25519_vps
# 建議設定密碼保護私鑰
# 2. 複製公鑰至 VPS (需密碼登入一次)
ssh-copy-id -i ~/.ssh/id_ed25519_vps.pub user@vps-ip
# 或手動
cat ~/.ssh/id_ed25519_vps.pub | ssh user@vps-ip "mkdir -p ~/.ssh && chmod 700 ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
# 3. 驗證無密碼登入
ssh -i ~/.ssh/id_ed25519_vps user@vps-ip "hostname"
~/.ssh/config
# 基礎別名
Host vps-prod
HostName 203.0.113.10
User admin
Port 2222
IdentityFile ~/.ssh/id_ed25519_vps
IdentitiesOnly yes
ServerAliveInterval 30
ServerAliveCountMax 3
TCPKeepAlive yes
Compression yes
# 跳板機/堡壘機 (ProxyJump, OpenSSH 7.3+)
Host vps-internal
HostName 10.0.1.50
User admin
IdentityFile ~/.ssh/id_ed25519_vps
ProxyJump vps-prod
# 等同於 ssh -J vps-prod vps-internal
# 多層跳轉
Host vps-deep
HostName 10.0.2.10
User admin
ProxyJump vps-prod,vps-internal
# ~/.ssh/config 全域或特定 Host
Host *
ControlMaster auto
ControlPath ~/.ssh/control/%r@%h:%p
ControlPersist 30m
# ControlPersist yes # 永久持久化,直到手動關閉
# 測試效果
time ssh vps-prod true # 首次建立 ~1-2s
time ssh vps-prod true # 復用 ~0.05s (快 20-40 倍)
# 關閉所有復用連線
ssh -O exit vps-prod
| 方案 | 工具/指令 | 優點 | 缺點 | 適用場景 |
|---|---|---|---|---|
| ProxyCommand + ncat (OpenBSD netcat) | ProxyCommand ncat --proxy-type http --proxy 10.0.0.1:3128 %h %p | 原生支援、無額外依賴 (ncat 常預裝)、支援 CONNECT | 需 ncat (非 GNU netcat) | 現代 Linux 首選 |
| ProxyCommand + corkscrew | ProxyCommand corkscrew 10.0.0.1 3128 %h %p | 專為 HTTP 代理設計、穩定 | 需額外安裝、不支援 SOCKS | 舊系統、純 HTTP 代理 |
| ProxyJump 經跳板機 | ProxyJump user@jump-host | 無需代理協定轉換、端對端加密 | 需跳板機可直連目標 | 堡壘機架構 |
| systemd-socket-proxyd (systemd 247+) | systemd-socket-proxyd --http-proxy=https://10.0.0.1:3128 | 系統級、支援 systemd 服務整合 | 較新系統才支援 | 服務級代理 |
Host vps-behind-proxy
HostName 203.0.113.20
User admin
IdentityFile ~/.ssh/id_ed25519_vps
ProxyCommand ncat --proxy-type http --proxy-auth user:pass --proxy 10.0.0.1:3128 %h %p
ServerAliveInterval 15
# 遠端直接啟動/接管 tmux
ssh vps-prod -t "tmux attach || tmux new"
# 或本地腳本
ssh vps-prod -t "zellij attach main || zellij new"
配合 ControlPersist,斷開本地終端機後遠端 tmux 會話保持運行,下次連線即時恢復。
~/.ssh/config
Host *
# 僅允許現代加密演算法
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512
HostKeyAlgorithms ssh-ed25519-cert-v01@openssh.com,ssh-ed25519,rsa-sha2-512,rsa-sha2-256
# 禁用弱演算法
PubkeyAcceptedAlgorithms ssh-ed25519,ssh-ed25519-cert-v01@openssh.com,rsa-sha2-512,rsa-sha2-256
# 信任首次使用 (TOFU) 改為嚴格檢查
StrictHostKeyChecking accept-new
UserKnownHostsFile ~/.ssh/known_hosts
# 禁用 X11/代理轉發 (除非必要)
ForwardX11 no
ForwardAgent no
/etc/ssh/sshd_config
Port 2222 # 非標準埠減少掃描噪音
PermitRootLogin no # 禁用 root 登入
PubkeyAuthentication yes
PasswordAuthentication no # 強制金鑰認證
AuthenticationMethods publickey
MaxAuthTries 3
ClientAliveInterval 30
ClientAliveCountMax 2
AllowUsers admin deploy # 白名單用戶
# AllowGroups sshusers # 或群組白名單
Ciphers chacha20-poly1305@openssh.com,aes256-gcm@openssh.com,aes128-gcm@openssh.com
MACs hmac-sha2-256-etm@openssh.com,hmac-sha2-512-etm@openssh.com
KexAlgorithms curve25519-sha256,curve25519-sha256@libssh.org,diffie-hellman-group16-sha512
# 套用並驗證
sshd -t && systemctl reload sshd
ssh -o PreferredAuthentications=password vps-prod # 應被拒絕
#!/bin/bash
# run_on_vps.sh
hosts=(vps-web1 vps-web2 vps-db1 vps-cache1)
cmd="${*:-uptime}"
for h in "${hosts[@]}"; do
echo "=== $h ==="
ssh -o ConnectTimeout=5 "$h" "$cmd" 2>&1 | sed "s/^/[$h] /"
done
配合 ControlMaster,10 台 VPS 並發執行可在秒級完成。
遷移至 Linux 原生 SSH 生態,不僅獲得 Ed25519、ProxyJump、ControlMaster 等現代功能,更能將 VPS 管理納入 Ansible、Fabric、Just、Task 等自動化框架,實現真正的基礎設施即代碼。