在mac系统中,使用ssh连接GitHub
- 英文步骤详见:Setting up Git | The Odin Project
- 详细中文步骤可见:【GitHub从入门到精通】GitHub配置SSH公钥实现免密码拉取代码和上传代码保姆级详细教程-CSDN博客
跟随1
教程过程中,使用private GitHub email address。
在2.5testing连接时出现超时,无法获得响应。
随后更换邮箱设置,不使用private邮箱,改为自己邮箱重新连接。此时terminal使用ssh -T git@github.com
尝试连接时反馈connection closed
jasonwu@JasonWudeMacBook-Air ~ % ssh -T git@github.com
Connection closed by 198.xx.xx.xx port 22
使用下述指令查看更多调试信息
ssh -vT git@github.com
得到结果如下
jasonwu@JasonWudeMacBook-Air ~ % ssh -vT git@github.com
OpenSSH_9.6p1, LibreSSL 3.3.6
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/jasonwu/.ssh/id_rsa type 0
debug1: identity file /Users/jasonwu/.ssh/id_rsa-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa_sk type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ed25519 type 3
debug1: identity file /Users/jasonwu/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ed25519_sk type -1
debug1: identity file /Users/jasonwu/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_xmss type -1
debug1: identity file /Users/jasonwu/.ssh/id_xmss-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_dsa type -1
debug1: identity file /Users/jasonwu/.ssh/id_dsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.6
kex_exchange_identification: Connection closed by remote host
Connection closed by 198.xx.xx.xx port 22
该结果可能是由于防火墙和IP限制(可能的原因是国内环境使用代理IP导致GitHub的连接受限)
GitHub 允许使用端口 443 作为 SSH 的替代端口(通常用于 HTTPS 流量),这可以绕过一些防火墙限制。可以使用以下命令来通过另一端口测试:
ssh -T -p 443 git@ssh.github.com
最终连接成功
jasonwu@JasonWudeMacBook-Air ~ % ssh -T -p 443 git@ssh.github.com
The authenticity of host '[ssh.github.com]:443 ([198.18.0.89]:443)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This key is not known by any other names.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.
Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.
要想一劳永逸,可以:
- 更改防火墙规则,允许22端口使用ssh。
- 或者修改ssh连接的config,强行让ssh连接走http流量的443端口。
具体步骤:修改~/.ssh/config
文件(没有的话就新建一个),添加以下内容:
再次使用下述命令,成功连接~Host github.com Hostname ssh.github.com Port 443 User git
ssh -T git@github.com
真正的解法在这:
https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port