Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 系統透過 SSH 管理 VPS 完整指南:金鑰認證、代理穿透、多會話復用與安全加固

編輯:Linux 教學

從 Windows Putty 遷移至 Linux 原生 OpenSSH 管理 VPS,能獲得更強的自動化能力、更完整的協定支援 (Ed25519、FIDO2)、原生代理指令與終端機多工器整合。本文涵蓋 Ed25519 金鑰生成與分發、SSH Config 別名與多跳代理、ControlMaster 連線復用、systemd 服務化持久化、代理穿透 (corkscrew/ProxyJump/ncat)、安全加固清單 完整實戰指南。

一、Ed25519 金鑰生成與部署 (現代最佳實踐)


# 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 別名與多跳代理 ~/.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

三、ControlMaster 連線復用 (極大加速多會話/腳本)


# ~/.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

四、代理穿透方案對比 (公司 HTTP/HTTPS 代理環境)

方案工具/指令優點缺點適用場景
ProxyCommand + ncat (OpenBSD netcat)ProxyCommand ncat --proxy-type http --proxy 10.0.0.1:3128 %h %p原生支援、無額外依賴 (ncat 常預裝)、支援 CONNECT需 ncat (非 GNU netcat)現代 Linux 首選
ProxyCommand + corkscrewProxyCommand 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 服務整合較新系統才支援服務級代理

實際配置範例 (HTTP 代理 + 認證)


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/zellij) 實現會話持久化


# 遠端直接啟動/接管 tmux
ssh vps-prod -t "tmux attach || tmux new"
# 或本地腳本
ssh vps-prod -t "zellij attach main || zellij new"

配合 ControlPersist,斷開本地終端機後遠端 tmux 會話保持運行,下次連線即時恢復。

六、安全加固清單 (Client 與 Server 端)

Client 端 ~/.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

Server 端 /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  # 應被拒絕

七、自動化腳本範例:批次執行命令於多台 VPS


#!/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 等自動化框架,實現真正的基礎設施即代碼。

Copyright © Windows教程網 All Rights Reserved