绑定 SSH key(macos)
在 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…” 就说明成功了。