SSH 密鑰配置指南#
快速配置 SSH 密鑰,用於伺服器免密登錄、Git 認證和提交簽名
1. 生成 SSH 密鑰對#
# 生成 Ed25519 密鑰對(推薦)
ssh-keygen -t ed25519 -C "[email protected]"
# 交互提示:
# Enter file: 直接回車(使用默認路徑 ~/.ssh/id_ed25519)
# Enter passphrase: 可選密碼保護或直接回車跳過
生成結果:
- 私鑰:
~/.ssh/id_ed25519(⚠️ 保密) - 公鑰:
~/.ssh/id_ed25519.pub(可公開)
2. 配置 SSH Config 文件#
# 創建/編輯配置文件
touch ~/.ssh/config
chmod 600 ~/.ssh/config
nano ~/.ssh/config
配置模板:
# 開發伺服器
Host dev-server
HostName 192.168.1.100
User your_username
Port 22
IdentityFile ~/.ssh/id_ed25519
# 生產伺服器
Host prod-server
HostName prod.example.com
User admin
Port 2222
IdentityFile ~/.ssh/id_ed25519
# GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519
配置項說明:
Host: 別名(自定義)HostName: 實際伺服器地址User: 登錄用戶名Port: SSH 端口(默認 22)IdentityFile: 私鑰路徑
使用效果:
# 配置前
ssh [email protected]
# 配置後
ssh dev-server
3. 配置伺服器免密登錄#
方法 1:使用 ssh-copy-id(推薦)#
# 使用完整地址
ssh-copy-id [email protected]
# 或使用別名
ssh-copy-id dev-server
方法 2:手動複製#
# 1. 複製公鑰內容
cat ~/.ssh/id_ed25519.pub
# 2. 登錄伺服器
ssh [email protected]
# 3. 在伺服器執行
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo "公鑰內容" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
exit
4. 配置 Git 提交簽名#
# 配置用戶信息
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# 配置 SSH 簽名
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true
配置文件效果 (~/.gitconfig):
[user]
email = [email protected]
name = Your Name
signingkey = /Users/username/.ssh/id_ed25519.pub
[gpg]
format = ssh
[commit]
gpgsign = true
5. 添加公鑰到 Git 平台#
GitHub#
# 複製公鑰到剪貼板(macOS)
cat ~/.ssh/id_ed25519.pub | pbcopy
- 訪問:https://github.com/settings/keys
- 點擊 New SSH key
- 添加兩次:
- Key type:
Authentication Key→ Title:MacBook Pro→ 粘貼公鑰 - Key type:
Signing Key→ Title:MacBook Pro Signing→ 粘貼公鑰
- Key type:
💡 同一公鑰可同時用於認證和簽名
GitLab#
- 複製公鑰:
cat ~/.ssh/id_ed25519.pub | pbcopy - 訪問:https://gitlab.com/-/profile/keys
- 粘貼公鑰,Usage type 選擇:
Authentication & Signing
Gitee#
- 複製公鑰:
cat ~/.ssh/id_ed25519.pub | pbcopy - 訪問:https://gitee.com/profile/sshkeys
- 粘貼公鑰,點擊確定
6. 常見問題#
Q1: Permission denied (publickey)#
# 檢查密鑰權限
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
# 重新複製公鑰
ssh-copy-id username@server
Q2: 提交沒有 Verified 標記#
# 檢查郵箱(必須與 GitHub 賬號一致)
git config user.email
# 檢查簽名配置
git config --list | grep sign
# 確認已在 GitHub 添加 Signing Key
Q3: 簽名時要求輸入密碼#
# 添加到 ssh-agent(macOS)
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
# 或配置自動加載(編輯 ~/.ssh/config)
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
Q4: 多個密鑰管理#
# ~/.ssh/config
# 個人 GitHub
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
# 公司 GitHub
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
🎯 總體流程#
# 1. 生成密鑰
ssh-keygen -t ed25519 -C "[email protected]"
# 2. 配置 Git 簽名
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
git config --global gpg.format ssh
git config --global user.signingkey ~/.ssh/id_ed25519.pub
git config --global commit.gpgsign true
# 3. 複製公鑰
cat ~/.ssh/id_ed25519.pub | pbcopy
# 4. 添加到 GitHub
# Settings → SSH and GPG keys → New SSH key
# 添加兩次:Authentication Key + Signing Key
# 5. 測試
ssh -T [email protected]
📋 常用命令#
# 密鑰管理
ssh-keygen -t ed25519 -C "email" # 生成密鑰
cat ~/.ssh/id_ed25519.pub | pbcopy # 複製公鑰
ssh-keygen -p -f ~/.ssh/id_ed25519 # 修改密鑰密碼
# SSH 連接
ssh-copy-id user@host # 複製公鑰到伺服器
ssh -T [email protected] # 測試 GitHub 連接
ssh dev-server # 使用別名登錄
# SSH Agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519 # 添加密鑰(macOS)
ssh-add -l # 查看已添加密鑰
# Git 配置
git config --list | grep -E "(user|gpg|sign)" # 查看簽名配置
git log --show-signature # 查看提交簽名
git commit --no-gpg-sign -m "msg" # 臨時禁用簽名
# 權限修復
chmod 700 ~/.ssh
chmod 600 ~/.ssh/config
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
📚 附錄#
SSH 密鑰類型對比#
| 算法 | 密鑰長度 | 推薦度 |
|---|---|---|
| Ed25519 | 256 位 | ⭐⭐⭐⭐⭐ 首選(最安全、最快) |
| RSA | 4096 位 | ⭐⭐⭐ 兼容性好 |
| ECDSA | 256/384/521 位 | ⭐⭐⭐ 部分老系統不支持 |
| DSA | 1024 位 | ❌ 已過時,不推薦 |
安全建議#
- ✅ 為私鑰設置密碼保護
- ✅ 私鑰權限設為
600 - ✅ 定期備份密鑰到安全位置
- ❌ 不要上傳私鑰到雲存儲
- ❌ 不要通過聊天工具發送私鑰
Blog 版本:1.0
適用平台:macOS / Linux / Windows (WSL)