Summary

A beginner-friendly guide to SSH covering key authentication, ~/.ssh/config shortcuts, SSH agent passphrases, port forwarding, and common troubleshooting. The core message is to abandon password-based SSH in favor of key pairs, and to use config files to eliminate repetitive typing. Covers the practical commands a developer uses daily.

初學者 SSH 指南:從金鑰認證設定、config 捷徑、SSH agent 避免重複輸入 passphrase、port forwarding 到常見故障排除。核心訴求是用金鑰取代密碼,並用 config 文件消除重複輸入。

Key Points

  • 金鑰認證設定流程ssh-keygen -t rsa -b 4096ssh-copy-id user@host → 無密碼登入,三步驟
  • ~/.ssh/config 快捷方式:用 Host block 定義別名、使用者名稱、金鑰路徑,讓 ssh myserver 取代 ssh ubuntu@192.168.1.10
  • 防 timeoutServerAliveInterval 60 + ServerAliveCountMax 3 保持連線活躍
  • SSH agenteval "$(ssh-agent -s)" + ssh-add 讓有 passphrase 的金鑰只需輸入一次
  • Port forwardingssh -L 5432:localhost:5432 user@host 把遠端資料庫映射到本地端口,安全訪問
  • 常見故障chmod 600 ~/.ssh/id_rsa 修正權限;ssh-keygen -R host 清除舊 fingerprint

Insights

SSH config 文件是被低估的生產力工具。大多數開發者知道 SSH key auth,但少有人系統性地維護 ~/.ssh/config,導致反覆輸入長命令。對於管理多台伺服器(dev/staging/prod + 多個雲帳號)的工程師,config 文件能節省可觀時間。

Port forwarding 是安全訪問內網服務的標準方式,適用於資料庫、Redis、內部管理介面,不需要在伺服器上額外開放端口。

Connections

Raw Excerpt

“Pro Tip: If you ever catch yourself typing the same SSH command twice, automate it. SSH is powerful not because of complexity, but because it rewards efficiency.”