macOS 上为 GitHub 生成 SSH Key 的完整步骤
一、检查是否已有 SSH Key
打开「终端」执行:
ls -al ~/.ssh
如果看到类似:
id_ed25519id_ed25519.pubid_rsaid_rsa.pub
说明已经存在 SSH Key,可以选择继续使用,或者重新生成一个新的。
二、生成新的 SSH Key(推荐 Ed25519)
GitHub 官方推荐使用 Ed25519:
ssh-keygen -t ed25519 -C "你的GitHub邮箱"
例如:
ssh-keygen -t ed25519 -C "you@example.com"
然后:
提示保存路径时,直接按 Enter(默认路径:
~/.ssh/id_ed25519)设置密码(可选,建议设置)
再输入一次确认
如果系统不支持 ed25519,可以使用 RSA:
ssh-keygen -t rsa -b 4096 -C "你的GitHub邮箱"
三、启动 ssh-agent 并添加 Key
启动 agent:
eval "$(ssh-agent -s)"
添加 SSH Key:
ssh-add ~/.ssh/id_ed25519
如果你用的是 RSA:
ssh-add ~/.ssh/id_rsa
四、把公钥添加到 GitHub
复制公钥:
pbcopy < ~/.ssh/id_ed25519.pub
打开 GitHub:
进入 Settings
点击 SSH and GPG keys
点击 New SSH key
粘贴刚才复制的内容
保存
五、测试是否成功
ssh -T git@github.com
如果成功,会看到类似提示:
Hi 用户名! You've successfully authenticated, but GitHub does not provide shell access.
说明配置成功 ✓