在 macOS 上綁定 Gitee 或 GitHub 的 SSH Key,通常分為以下幾步操作,包括生成 SSH key、添加到 ssh-agent,并配置到 Gitee 或 GitHub 平臺。
1. 檢查是否已有 SSH Key
ls -al ~/.ssh
看看是否已有 id_rsa 或 id_ed25519 等文件。如果沒有就需要生成。
2. 生成新的 SSH Key
以下以 ed25519 算法為例(推薦使用)
ssh-keygen -t ed25519 -C "你的郵箱@example.com"
如果提示輸入保存路徑,建議直接回車使用默認路徑:~/.ssh/id_ed25519
3. 啟動 ssh-agent 并添加 SSH key
# 啟動 ssh-agent
eval "$(ssh-agent -s)"# 創建 SSH config 文件(如果不存在)
touch ~/.ssh/config# 添加以下內容(防止每次都輸密碼)
echo -e "Host *\n AddKeysToAgent yes\n UseKeychain yes\n IdentityFile ~/.ssh/id_ed25519" >> ~/.ssh/config# 添加 key 到 ssh-agent
ssh-add --apple-use-keychain ~/.ssh/id_ed25519
4. 復制 SSH 公鑰
pbcopy < ~/.ssh/id_ed25519.pub
這條命令會將 SSH 公鑰復制到剪貼板。
5. 添加到 GitHub 或 Gitee
GitHub:
直接訪問
Sign in to GitHub · GitHub
-
粘貼進去,點擊 Add SSH key
Gitee:
https://gitee.com/profile/sshkeys
-
點擊 添加公鑰
-
粘貼進去并保存
6. 測試是否成功連接
GitHub:
ssh -T git@github.com
Gitee:
ssh -T git@gitee.com
如果你看到類似 “Hi username! You’ve successfully authenticated…” 就說明成功了。