萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 伺服器教學

Linux SSH 管理 VPS 進階:金鑰認證與 HTTP 代理穿透實戰

記錄使用 SSH 金鑰認證登入 VPS,並在企業內網 HTTP 代理環境下透過 corkscrew 建立 SSH 隧道的完整配置流程。

一、SSH 金鑰認證配置

  1. 本地生成金鑰對
    ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    # 一路 Enter 使用預設路徑 ~/.ssh/id_rsa
  2. 部署公鑰至伺服器
    ssh-copy-id user@vps_ip
    # 或手動將 ~/.ssh/id_rsa.pub 內容追加至伺服器 ~/.ssh/authorized_keys
  3. 驗證免密登入
    ssh user@vps_ip

⚠️ 關鍵提醒:authorized_keys 權限必須 600,.ssh 目錄 700,擁有者為該使用者。

二、HTTP 代理環境下 SSH 穿透(corkscrew)

1. 編譯安裝 corkscrew

wget https://www.agroman.net/corkscrew/corkscrew-2.0.tar.gz
tar zxf corkscrew-2.0.tar.gz
cd corkscrew-2.0
./configure && make && sudo make install

2. 配置 SSH Client 使用代理

# 編輯 ~/.ssh/config
Host *
    ProxyCommand corkscrew proxy_host proxy_port %h %p
    # 若代理需認證
    # ProxyCommand corkscrew proxy_host proxy_port %h %p ~/.ssh/proxy_auth

# proxy_auth 格式:username:password

3. 測試連線

ssh -v user@vps_ip
# 除錯輸出應顯示 "Connection established" 經由代理

三、進階:SSH 多路複用加速

# ~/.ssh/config 追加
Host *
    ControlMaster auto
    ControlPath ~/.ssh/sockets/%r@%h-%p
    ControlPersist 600
# 需先建立目錄:mkdir -p ~/.ssh/sockets

四、Windows 客戶端

  • PuTTY:使用 puttygen 轉換私鑰為 .ppk,Connection → Proxy 設定 HTTP 代理。
  • Windows 10+ OpenSSH:原生支援 ssh 指令,配置檔同 ~/.ssh/config。
copyright © 萬盛學電腦網 all rights reserved