在 Mac 上配置 GitHub SSH 密钥:一步到位
需要在 Mac 上通过 SSH 访问 GitHub?下面这份笔记把生成密钥、添加公钥、配置多账号、测试连通性等步骤压缩在一篇里,按顺序操作即可。 1) 生成 SSH 密钥 ssh-keygen -t ed25519 -C "your_personal_email@example.com" 推荐 ED25519,安全性足够且生成速度快。 默认保存到 ~/.ssh/id_ed25519;如需区分用途,可改名为 id_rsa_personal 等。 2) 把密钥交给 ssh-agent eval "$(ssh-agent -s)" ssh-add ~/.ssh/id_ed25519 如果你给私钥加了密码,ssh-agent 会帮你缓存解密后的会话,避免频繁输入。 3) 在 GitHub 添加公钥 pbcopy < ~/.ssh/id_ed25519.pub 进入 GitHub 网页:Settings → SSH and GPG keys → New SSH key。 粘贴公钥并保存。 4) 配置 ~/.ssh/config(支持多账号) nano ~/.ssh/config 示例配置: # 个人账号 Host personal HostName github.com User git IdentityFile ~/.ssh/id_ed25519 # 公司账号 Host work HostName github.com User git IdentityFile ~/.ssh/id_rsa_work 克隆时对应使用 git@personal:owner/repo.git 或 git@work:owner/repo.git。 如果遇到“权限过高”报错,执行 chmod 600 ~/.ssh/id_ed25519。 5) 测试连接 ssh -T git@github.com 出现 “Hi xxx! You’ve successfully authenticated…” 即表示连通正常;如提示 host key 变更,确认无 MITM 风险后按提示处理。 ...