序
相信日常开发中
当我们百思不得解,威武不能屈的时候
亦或是拜读(chao xi)大佬代码的时候
都会打开 github 寻找良医,奈何 git clone 长路漫漫
如便秘,似雷劈
就比如最近在修改博客的主题,代码放在Github上。
当我晚上回家想继续开整的时候,git clone居然报错了!提示 failed to connect xxx prot 443 ...
一番研究(Baidu)之后发现是ssh的问题,下面是我解决这个问题的经过,在此记录下来。
检查ssh key
出现问题后,我的第一反应是ping github.com,发现是可以ping通的
niRvana 2 on 🌱 main 🎉 via 📦 v16.14.2 took 3s
❯ ping github.com
PING github.com (140.82.114.4): 56 data bytes
64 bytes from 140.82.114.4: icmp_seq=0 ttl=49 time=257.264 ms
64 bytes from 140.82.114.4: icmp_seq=1 ttl=49 time=218.137 ms
64 bytes from 140.82.114.4: icmp_seq=2 ttl=49 time=298.723 ms
64 bytes from 140.82.114.4: icmp_seq=3 ttl=49 time=214.944 ms
64 bytes from 140.82.114.4: icmp_seq=4 ttl=49 time=215.080 ms
64 bytes from 140.82.114.4: icmp_seq=5 ttl=49 time=222.197 ms
64 bytes from 140.82.114.4: icmp_seq=6 ttl=49 time=216.783 ms
^C
--- github.com ping statistics ---
7 packets transmitted, 7 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 214.944/234.733/298.723/29.605 ms
那么为什么会出现无法clone项目的情况呢?
继续检查ssh key,当我在终端输入ssh -vT git@github.com验证ssh连接是否正常时,发现一直连接不上
niRvana 2 on 🌱 main 🎉 via 📦 v16.14.2 took 4s
➜ ssh -vT git@github.com
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/scott/.ssh/config
debug1: /Users/scott/.ssh/config line 1: Applying options for github.com
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 ssh.github.com port 22.
debug1: Connection timeout.
可以看到一直是timeout。
重新生成ssh key
我想到的是,既然ssh key失效了,有可能是因为我之前更换过github的绑定邮箱导致我之前用旧邮箱生成的ssh key失效了,所以我们重新生成一个。
终端运行:ssh-keygen -t ed25519 -C "your_email@examle.com"
然后一路按回车
> Generating public/private ALGORITHM key pair.
> Enter a file in which to save the key (/Users/YOU/.ssh/id_ALGORITHM: [Press enter]
> Enter passphrase (empty for no passphrase): [Type a passphrase]
> Enter same passphrase again: [Type passphrase again]
完成操作之后,会在我们的~/.ssh目录生成对应的文件
在后台启动ssh代理
终端运行 eval "$(ssh-agent -s)"
得到运行结果:> Agent pin 59566
提示
如果您使用的是macOS Sierra 10.12.2或更高版本,则需要修改~/.ssh/config文件,自动将密钥加载到ssh代理中,并将密码存储在密钥链中。
首先,检查你的 ~/.ssh/config文件是否存在于默认位置。
如果不存在,运行touch ~/.ssh/config进行创建文件
确认文件存在之后,运行open ~/.ssh/config打开文件
在文件中输入以下内容
Host *.github.com
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
将ssh key添加到Github
创建完ssh key之后,我们需要把创建的key添加到自己的Github账户中。
在终端运行pbcopy < ~/.ssh/id_ed25519.pub 将创建的key的内容复制到剪切板
注意
一定要使用命令的方式去复制,不然你会发现你的key并不会生效
登录你的Github账户,在任何页面的右上角,单击你的个人资料照片,然后单击设置。
在侧边栏的Access部分,单击 SSH and GPG keys之后点击 New SSH key or Add SSH key
在title字段中,为密钥添加描述性标签。例如,如果你使用的是个人笔记本电脑,你可以将此密钥称为“个人笔记本电脑”。
Key type保持默认
把复制的ssh key粘贴到key输入框区,点击 Add SSH Key 保存
验证排查问题
重新生成ssh key并且添加到Github之后,当我信心满满的在终端运行 git clone xxx 时,What the f**k!还是失败...并且有了新的提示
kex_exchange_identification: Connection closed by remote host
并且运行ssh -Tv git@github.com还是timeout
niRvana 2 on 🌱 main 🎉 via 📦 v16.14.2 took 4s
➜ ssh -vT git@github.com
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/scott/.ssh/config
debug1: /Users/scott/.ssh/config line 1: Applying options for github.com
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 ssh.github.com port 22.
debug1: Connection timeout.
正当我百思不得其解时,在stackoverflow上找到了答案
我们需要修改~/.ssh/config 文件
Host github.com
Hostname ssh.github.com
Port 443
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519
将Host从*.github.com改为github.com并且增加一行Hostname ssh.github.com
验证是否成功
终端运行 ssh -vT git@github.com得到如下运行结果
niRvana 2 on 🌱 main 🎉 via 📦 v16.14.2
➜ ssh -vT git@github.com
OpenSSH_9.0p1, LibreSSL 3.3.6
debug1: Reading configuration data /Users/scott/.ssh/config
debug1: /Users/scott/.ssh/config line 1: Applying options for github.com
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 ssh.github.com port 443.
debug1: Connection established.
debug1: identity file /Users/scott/.ssh/id_ed25519 type 3
debug1: identity file /Users/scott/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version babeld-cd305013
debug1: compat_banner: no match: babeld-cd305013
debug1: Authenticating to ssh.github.com:443 as 'git'
debug1: load_hostkeys: fopen /Users/scott/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ssh-ed25519
debug1: kex: server->client cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: kex: client->server cipher: chacha20-poly1305@openssh.com MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: SSH2_MSG_KEX_ECDH_REPLY received
debug1: Server host key: ssh-ed25519 SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU
debug1: load_hostkeys: fopen /Users/scott/.ssh/known_hosts2: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts: No such file or directory
debug1: load_hostkeys: fopen /etc/ssh/ssh_known_hosts2: No such file or directory
debug1: Host '[ssh.github.com]:443' is known and matches the ED25519 host key.
debug1: Found key in /Users/scott/.ssh/known_hosts:1
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: get_agent_identities: bound agent to hostkey
debug1: get_agent_identities: agent returned 1 keys
debug1: Will attempt key: /Users/scott/.ssh/id_ed25519 ED25519 SHA256:NcHu52gCDf5ChaibUMnkhqG3RD3C2W6gQtp5wGh/F6c explicit agent
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<ssh-ed25519-cert-v01@openssh.com,ecdsa-sha2-nistp521-cert-v01@openssh.com,ecdsa-sha2-nistp384-cert-v01@openssh.com,ecdsa-sha2-nistp256-cert-v01@openssh.com,sk-ssh-ed25519-cert-v01@openssh.com,sk-ecdsa-sha2-nistp256-cert-v01@openssh.com,rsa-sha2-512-cert-v01@openssh.com,rsa-sha2-256-cert-v01@openssh.com,ssh-rsa-cert-v01@openssh.com,sk-ssh-ed25519@openssh.com,sk-ecdsa-sha2-nistp256@openssh.com,ssh-ed25519,ecdsa-sha2-nistp521,ecdsa-sha2-nistp384,ecdsa-sha2-nistp256,rsa-sha2-512,rsa-sha2-256,ssh-rsa>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/scott/.ssh/id_ed25519 ED25519 SHA256:NcHu52gCDf5ChaibUMnkhqG3RD3C2W6gQtp5wGh/F6c explicit agent
debug1: Server accepts key: /Users/scott/.ssh/id_ed25519 ED25519 SHA256:NcHu52gCDf5ChaibUMnkhqG3RD3C2W6gQtp5wGh/F6c explicit agent
Authenticated to ssh.github.com ([20.205.243.160]:443) using "publickey".
debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: filesystem
debug1: client_input_global_request: rtype hostkeys-00@openssh.com want_reply 0
debug1: client_input_hostkeys: searching /Users/scott/.ssh/known_hosts for [ssh.github.com]:443 / (none)
debug1: client_input_hostkeys: searching /Users/scott/.ssh/known_hosts2 for [ssh.github.com]:443 / (none)
debug1: client_input_hostkeys: hostkeys file /Users/scott/.ssh/known_hosts2 does not exist
debug1: client_input_hostkeys: host key found matching a different name/address, skipping UserKnownHostsFile update
debug1: Sending environment.
debug1: channel 0: setting env LC_TERMINAL_VERSION = "3.4.16"
debug1: channel 0: setting env LANG = "zh_CN.UTF-8"
debug1: channel 0: setting env LC_TERMINAL = "iTerm2"
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi iscottt! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2132, received 2344 bytes, in 0.6 seconds
Bytes per second: sent 3361.7, received 3696.0
debug1: Exit status 1
至此,问题就成功修复啦,又可以愉快的使用git了!
如果你觉得本文对你有所帮助,可以点个赞或者请我喝杯奶茶~万分感谢🎉🎉🎉