1. 生成新 SSH 金鑰

ssh-keygen -t ed25519 -C "your_email@example.com"

執行後會出現提示:

  • Enter file in which to save the key: 直接回車使用預設名稱 id_ed25519
  • Enter passphrase: 建議設置密碼以增加安全性(也可以直接回車跳過)
  • Enter same passphrase again: 再次輸入密碼確認

提示: 如果你使用的是舊系統不支援 ed25519,可以使用 ssh-keygen -t rsa -b 4096 -C "your_email@example.com" 替代。

2. 設置正確的檔案權限(重要!)

SSH 對檔案權限要求嚴格,否則金鑰可能被忽略:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub

3. 將 SSH 金鑰新增到 ssh-agent

macOS 用戶

# 啟動 ssh-agent
eval "$(ssh-agent -s)"

# 編輯或創建 config 檔案,實現自動載入
vi ~/.ssh/config

i 進入編輯模式,加入以下內容:

Host *
  AddKeysToAgent yes
  UseKeychain yes
  IdentityFile ~/.ssh/id_ed25519

esc 然後輸入 :wq 儲存退出。

# 新增金鑰到 ssh-agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519

Linux 用戶

# 啟動 ssh-agent
eval "$(ssh-agent -s)"

# 新增金鑰
ssh-add ~/.ssh/id_ed25519

Windows 用戶(Git Bash)

# 啟動 ssh-agent
eval "$(ssh-agent -s)"

# 新增金鑰
ssh-add ~/.ssh/id_ed25519

4. 複製公鑰到剪貼簿

macOS

pbcopy < ~/.ssh/id_ed25519.pub

Linux

# 使用 xclip(需先安裝: sudo apt install xclip)
xclip -selection clipboard < ~/.ssh/id_ed25519.pub

# 或直接顯示並手動複製
cat ~/.ssh/id_ed25519.pub

Windows(Git Bash)

clip < ~/.ssh/id_ed25519.pub

5. 將公鑰新增到 GitHub

  1. 登入 GitHub
  2. 點擊右上角頭像 → Settings
  3. 左側選單選擇 SSH and GPG keys
  4. 點擊 New SSH key
  5. Title: 輸入這個金鑰的描述(例如: “My Laptop”)
  6. Key: 貼上剛才複製的公鑰
  7. 點擊 Add SSH key

github-add-ssh-key

6. 配置多個 Git 服務(選用)

如果你使用多個 Git 託管服務或多個帳號,可以編輯 ~/.ssh/config:

vi ~/.ssh/config

i 進入編輯模式,加入配置:

# GitHub 個人帳號
Host github.com
  HostName github.com
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519

# GitHub 工作帳號
Host github-work
  HostName github.com
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/id_ed25519_work

# Bitbucket
Host bitbucket.org
  HostName bitbucket.org
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/bitbucket_id_ed25519

# GitLab
Host gitlab.com
  HostName gitlab.com
  User git
  PreferredAuthentications publickey
  IdentityFile ~/.ssh/gitlab_id_ed25519

esc 然後輸入 :wq 儲存。

使用工作帳號時:

git clone git@github-work:company/repo.git

7. 測試連接

GitHub

ssh -T git@github.com

成功的回應:

Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

Bitbucket

ssh -T git@bitbucket.org

GitLab

ssh -T git@gitlab.com

常見問題排解

❌ Permission denied (publickey)

可能原因:

  1. 公鑰沒有正確新增到 GitHub
  2. 檔案權限不正確
  3. ssh-agent 沒有載入金鑰

解決方法:

# 檢查權限
ls -la ~/.ssh

# 確認金鑰已載入
ssh-add -l

# 如果沒有,重新新增
ssh-add ~/.ssh/id_ed25519

# 詳細除錯模式
ssh -vT git@github.com

❌ 首次連接提示無法確認主機真實性

The authenticity of host 'github.com' can't be established.
ED25519 key fingerprint is SHA256:xxx...
Are you sure you want to continue connecting (yes/no/[fingerprint])?

輸入 yes 並按回車(注意要完整輸入 yes,不能只按回車)。

❌ ssh-add 後重啟電腦金鑰消失

參考步驟 3 的 config 配置,確保已加入 AddKeysToAgent yes

❌ 設定正確的檔案權限

chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_ed25519
chmod 644 ~/.ssh/id_ed25519.pub
chmod 600 ~/.ssh/config

安全提示

  1. 永遠不要分享你的私鑰 (id_ed25519)
  2. 建議為私鑰設置 passphrase 增加安全性
  3. 定期輪換金鑰 每年或離職時更新
  4. 刪除不用的金鑰 在 GitHub Settings 中移除舊裝置的金鑰
  5. 備份私鑰 但要加密存放在安全的地方

將現有專案改用 SSH

如果你的專案目前使用 HTTPS,可以切換到 SSH:

# 查看當前遠端 URL
git remote -v

# 更改為 SSH URL
git remote set-url origin git@github.com:username/repo.git

# 確認更改
git remote -v

祝程式設計愉悅! 🚀

如果遇到問題,可以使用 ssh -vvv git@github.com 查看詳細除錯資訊。