ssh-keygen 金鑰管理完整教學
在 Linux 系統管理與開發環境中,SSH(Secure Shell)是最核心的遠端連線協議。雖然密碼認證直觀易懂,但在自動化腳本、持續整合(CI/CD)以及追求安全性的場景中,基於金鑰(Key-based)的無密碼登入才是業界標準。ssh-keygen 作為 OpenSSH 套件中負責生成、管理和轉換金鑰的核心指令,其功能遠比大多數使用者想像的要強大。本文將深入解析 ssh-keygen 的實際應用,幫助你建立更安全的 SSH 連線環境。
生成金鑰:選擇合適的演算法
執行 ssh-keygen 最基礎的用途是生成一對公鑰與私鑰。現代 SSH 協定支援多種加密演算法,選擇正確的演算法至關重要。目前推薦使用 ed25519,因為它在提供同等安全性的同時,計算速度更快且金鑰體積更小。若需相容較舊的系統,則可選擇 rsa 演算法,建議至少使用 4096 位元長度。
以下範例展示如何在 Ubuntu 22.04 或 Debian 12 上生成一組 Ed25519 金鑰,並設定保護密碼(Passphrase):
$ ssh-keygen -t ed25519 -C "your_email@example.com"
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/user/.ssh/id_ed25519):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_ed25519
Your public key has been saved in /home/user/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:8b7a2e3f9c1d4e5f6a7b8c9d0e1f2a3b4c5d6e7f your_email@example.com
The key's randomart image is:
+--[ED25519 256]--+
| .o+= |
| o oB. |
| . = o+ |
| o oE |
| . S . |
| . o |
| . o |
| . o |
| . |
+----[SHA256]-----+
在上述輸出中,請注意 fingerprint(指紋)是金鑰的唯一識別碼,可用於驗證金鑰是否正確。randomart image 則是一串視覺化的圖案,方便人類快速肉眼比對金鑰是否一致,防止中間人攻擊。
部署公鑰:自動化登入設定
生成金鑰後,私鑰(private key)必須嚴格保密,而公鑰(public key)則需要部署到目標伺服器。傳統做法是手動複製公鑰內容並附加到遠端主機的 ~/.ssh/authorized_keys 文件中,但 ssh-copy-id 指令能簡化此過程。
$ ssh-copy-id -i ~/.ssh/id_ed25519.pub user@remote_host
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/user/.ssh/id_ed25519.pub"
The authenticity of host 'remote_host (192.168.1.100)' can't be established.
ED25519 key fingerprint is SHA256:xxxxxx.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
user@remote_host's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'user@remote_host'"
and check to make sure that only the key(s) you wanted were added.
執行後,系統會提示輸入遠端伺服器的密碼。驗證通過後,公鑰即被寫入遠端主機。此時,你應該能夠直接執行 ssh user@remote_host 而無需再次輸入密碼(若私鑰未設密碼)或僅需輸入私鑰的 Passphrase。
進階應用:金鑰格式轉換與保護
有時你需要將金鑰用於不同環境,例如 AWS 或某些雲端服務商要求 PEM 格式,而 OpenSSH 預設使用自身格式。ssh-keygen 支援格式轉換:
# 將 OpenSSH 格式轉換為 PEM 格式(適用於 AWS EC2 等)
$ ssh-keygen -p -m PEM -f ~/.ssh/id_ed25519
此外,若你忘記了金鑰的 Passphrase 或希望移除它(不建議用於高安全環境),可以使用 -p 選項修改:
$ ssh-keygen -p -f ~/.ssh/id_ed25519
Enter old passphrase:
Key has comment '/home/user/.ssh/id_ed25519'
Enter new passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved with the new passphrase.
常見問題與解決方案
1. 權限錯誤:Permissions are too open
SSH 對檔案權限極為敏感。若 ~/.ssh 目錄或 id_rsa 私鑰檔案權限過於開放(例如其他人可讀寫),SSH 會拒絕使用該金鑰。
- 解決方法:修正權限至僅使用者可讀寫。
chmod 700 ~/.ssh chmod 600 ~/.ssh/id_ed25519 chmod 644 ~/.ssh/id_ed25519.pub
2. 金鑰類型不被支援
在極舊的伺服器上,可能預設禁用 ed25519 或 rsa-sha2-256 等現代演算法,導致連線失敗。
- 解決方法:在
~/.ssh/config中指定相容的演算法,或升級伺服器端的 OpenSSH 版本。Host old_server HostName 192.168.1.50 PubkeyAcceptedAlgorithms +ssh-rsa HostKeyAlgorithms +ssh-rsa
小結
ssh-keygen 不僅是生成金鑰的工具,更是管理 SSH 身份識別的核心。透過正確選擇演算法(推薦 Ed25519)、妥善設定檔案權限,並善用 ssh-copy-id 部署公鑰,你可以大幅提升系統管理的效率與安全性。記住,金鑰管理如同保險箱鑰匙,私鑰務必妥善保管,切勿隨意分享,並定期更新金鑰以應對潛在的安全風險。