SSH 让 Git 在与 GitHub 通信时可以方便地进行身份验证。完整配置包括创建密钥对、登记公钥,以及让 SSH 客户端选用正确的身份。本文也介绍硬件安全密钥、多账号和网络屏蔽 SSH 默认端口时的做法。
Shell 示例适用于 Linux、macOS 或 Windows Git Bash 下的 Bash。原生 Windows PowerShell 管理 SSH 代理的命令不同,使用它时请参阅对应平台的说明。1
选择并生成 SSH 密钥
新建软件密钥时可选 Ed25519。旧系统如果不支持它,GitHub 文档给出的替代方案是 4096 位 RSA 密钥:1
ssh-keygen -t ed25519 -C "[email protected]"
# Alternative for a client without Ed25519 support:
ssh-keygen -t rsa -b 4096 -C "[email protected]"
邮箱参数是标签。按提示选择文件位置并设置口令。如果建议位置已有密钥,应另选文件名,以免覆盖。口令用于保护保存的私钥,同名的 .pub 文件是要向 GitHub 登记的公钥。
GitHub 于 2022 年 3 月 15 日停止接受 DSA(ssh-dss)密钥。2021 年 11 月 2 日以后生成的 RSA 密钥必须使用 SHA-2 签名。支持 RSA 密钥并不等于仍支持旧 SHA-1 签名算法,较老客户端可能需要升级。1
硬件安全密钥
OpenSSH 8.2 通过 ed25519-sk 和 ecdsa-sk 类型支持 FIDO 认证器。它们需要兼容的认证器,例如受支持的 YubiKey,并不限定品牌。设备和 SSH 客户端都要支持所选类型。2
插入安全密钥并生成密钥对:
ssh-keygen -t ed25519-sk -C "[email protected]"
# Use this alternative if the authenticator does not support Ed25519:
ssh-keygen -t ecdsa-sk -C "[email protected]"
按设备提示操作,需要时触摸密钥。和软件密钥一样,把生成的公钥登记到 GitHub。设备和本地密钥文件都要保留:在默认的非驻留配置下,本地私钥文件含有调用认证器所需的句柄。2
-sk 后缀说明认证有硬件支持,不能据此认定某个算法在所有部署中都更安全。请选用客户端、认证器和密钥类型都支持的组合。
加载密钥并登记到 GitHub
在使用 OpenSSH 代理的 Bash 会话中,如果还没有可用代理,先启动它,再加载私钥:
eval "$(ssh-agent -s)"
ssh-add ~/.ssh/id_ed25519
如果自己起了文件名,或者使用硬件密钥,就换成实际文件名。代理能为后续 SSH 操作提供已加载的密钥,减少重复输入口令。1
显示公钥:
cat ~/.ssh/id_ed25519.pub
在 GitHub 账号设置中打开 SSH and GPG keys,选择 New SSH key,类型选 Authentication Key,贴入公钥。标题可以注明机器或设备。只上传公钥,私钥文件应留在本机。3
SSH 身份认证与提交签名是两种用途。如果也想用这把密钥签名提交,GitHub 要求另行登记为签名密钥。3 配置与验证问题见在 GitHub 上给 Git 提交签名。
验证连接并克隆仓库
测试身份认证:
ssh -T git@github.com
首次连接时,先把显示的主机密钥指纹与 GitHub 公布的指纹比对,再接受。成功响应会指出你的 GitHub 账号,并说明不能获得 shell。这是预期行为,因为连接用于 Git 操作。即使认证成功,测试也可能以状态码 1 退出。4
这个 URL 中的 SSH 用户名是 git;GitHub 根据密钥识别你的账号。仓库的所有者出现在路径里:
git clone git@github.com:OWNER/REPOSITORY.git
例如,所有者可以是组织而非你的个人账号。接受认证的账号需要有该仓库的访问权限。
为不同账号选择不同密钥
两个 GitHub 账号会使用同一台服务器和 SSH 用户名。可以在 ~/.ssh/config 中为每个身份取不同的本地主机别名,通过仓库 URL 选中相应密钥。5
Host github-personal
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_personal
IdentitiesOnly yes
Host github-work
HostName github.com
User git
IdentityFile ~/.ssh/id_ed25519_work
IdentitiesOnly yes
创建两把密钥,分别将公钥登记到对应账号。测试别名,并在仓库 URL 中使用:
ssh -T git@github-personal
ssh -T git@github-work
git clone git@github-personal:PERSONAL-OWNER/REPOSITORY.git
git clone git@github-work:WORK-ORGANIZATION/REPOSITORY.git
对已有工作副本,可以更新远程 URL:
git remote set-url origin git@github-work:WORK-ORGANIZATION/REPOSITORY.git
IdentitiesOnly yes 将认证尝试限制为配置好的身份,以免遍历代理提供的所有密钥。但不同匹配块中的 IdentityFile 可以累加。如果仍选错了密钥,请检查宽泛的 Host * 规则。具体主机配置放在通用默认配置前面。5
22 端口受限时使用 443
连接 22 端口超时可能源于网络过滤,密钥本身未必有问题。GitHub.com 在 ssh.github.com:443 提供 SSH 端点。先测试,再修改配置:6
ssh -T -p 443 git@ssh.github.com
如果认证成功,可以直接通过这个端点克隆:
git clone ssh://[email protected]:443/OWNER/REPOSITORY.git
若想继续使用普通的 GitHub SSH URL,可这样配置:
Host github.com
HostName ssh.github.com
Port 443
User git
然后再次测试:
ssh -T git@github.com
对前述账号别名,应在各自配置块中设置 HostName ssh.github.com 和 Port 443,同时保留各自的 IdentityFile。匹配 github.com 的配置块不会自动匹配 github-work 或 github-personal。5
这是运行在常用于 HTTPS 的端口上的 SSH 流量,代理或防火墙仍有可能拦截。GitHub 文档指出,这项办法不适用于 GitHub Enterprise Server 或启用了数据驻留的 Enterprise Cloud。6
处理 known_hosts 提示
切换端口后,即使已经信任 github.com,仍可能遇到 [ssh.github.com]:443 的首次连接提示。请与 GitHub 官方列表核对指纹后再接受新条目。遇到密钥变化警告时应核实原因,保留主机密钥验证。46
查询 GitHub 的 IP 范围
要做网络诊断或配置防火墙,可通过 GitHub 的 Meta API 查询服务的 IP 范围:7
curl https://api.github.com/meta
这些范围会变化。应查询端点获得当前值,不宜复制旧报错或文档示例中的地址。
参考资料
原稿收集的资料
原稿还收集了以下讨论。相关主题已纳入正文,技术细节的依据见官方文档。
-
Git push/pull 时如何处理连接 github.com 的 22 端口超时? Stack Overflow,未注明日期。https://stackoverflow.com/questions/15589682/how-to-fix-ssh-connect-to-host-github-com-port-22-connection-timed-out-for-g (包括 https://stackoverflow.com/a/52817036 的回答)。
-
Longair, M.(2011 年 10 月 28 日)。关于为同一服务器选择不同 SSH 身份的回答。Stack Overflow。https://stackoverflow.com/a/7927828
摘要:“即使用户名和主机相同,也可以在 ~/.ssh/config 里加以区分。”(Longair,2011 年,第 1 段)
-
gertvdijk(2012 年 11 月 21 日)。关于避免 SSH 提交无关密钥的回答。Server Fault。https://serverfault.com/a/450807
摘要:“指定 IdentityFile 基本上只是把密钥加入代理已经提供给客户端的列表。”(gertvdijk,2012 年,以 Basically 开头的一段)
-
bk2204(2022 年 1 月 29 日)。关于为 Git 选择 SSH 密钥算法的回答。Information Security Stack Exchange。https://security.stackexchange.com/a/259198
摘要:“只要使用的服务器支持,通常我会推荐 Ed25519 密钥。”(bk2204,2022 年,第 1 段)
-
CircleCI. (n.d.). GitHub SSH Deprecation Information & Resolutions. https://support.circleci.com/hc/en-us/articles/4417542273179-GitHub-SSH-Deprecation-Information-Resolutions
Footnotes
-
GitHub. (n.d.). Generating a new SSH key and adding it to the ssh-agent. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent ↩ ↩2 ↩3 ↩4
-
OpenSSH. (2020, February 14). OpenSSH 8.2 release notes. Retrieved February 19, 2025, from https://www.openssh.org/txt/release-8.2 ↩ ↩2
-
GitHub. (n.d.). Adding a new SSH key to your GitHub account. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account ↩ ↩2
-
GitHub. (n.d.). Testing your SSH connection. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/connecting-to-github-with-ssh/testing-your-ssh-connection ↩ ↩2
-
OpenBSD. (n.d.). ssh_config(5): OpenSSH client configuration file. OpenBSD manual pages. Retrieved February 19, 2025, from https://man.openbsd.org/ssh_config ↩ ↩2 ↩3
-
GitHub. (n.d.). Using SSH over the HTTPS port. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port ↩ ↩2 ↩3
-
GitHub. (n.d.). REST API endpoints for meta data. GitHub Docs. Retrieved February 19, 2025, from https://docs.github.com/en/rest/meta/meta#get-github-meta-information ↩