ssh: connect to host github.com port 22: Connection refused
大家好,我是杰森,GitHub 對大家來說一定不陌生,無論是學習還是交(爬)朋(項)友(目),但是今天,我好像和它失聯了……

當我像往常一樣clone專案時,卻得到了這樣的報錯
$ git clone [email protected]:appletdevelop/full-stack.git
Cloning into ‘full-stack’...
ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.
什么都不能阻止打工人搬磚,必須要解決,經過一番排查,終于找到了問題的根源,分享兩種解決方案,大家注意避坑,
方案一:配置 DNS
因為錯誤資訊顯示 Connection refused ,所以我們需要去看看建立連接時發生了什么,為什么會出錯,查看日志,果然發現端倪
$ ssh -vT [email protected]
OpenSSH_9.0p1, OpenSSL 1.1.1o 5 July 2022
debug1: Reading configuration data /c/Users/jason/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [::1] port 22.
debug1: connect to address ::1 port 22: Connection refused
debug1: Connecting to github.com [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host github.com port 22: Connection refused
日志顯示,IPv6 和 IPv4 的 localhost 地址分別為 ::1 和 127.0.0.1,這意味著我們在連接 github 時,其域名將會被決議為 localhost 地址,當然也就無法連接,
打開查詢網站,找到 github.com 的 IP 地址

Windows 下,打開本機 hosts 檔案
C:\Windows\System32\drivers\etc
添加域名映射,并在 cmd 視窗重繪 DNS 配置
140.82.112.4 github.com
# Refreshing DNS configurations
$ ipconfig /flushdns
重新拉取,成功,
方案二:修改埠號
從上面的報錯資訊中可以發現,重點在這一句
ssh: connect to host github.com port 22: Connection refused
ssh 連接 GitHub 的 22 號埠被拒絕,但是 ping 一下 github.com 能通,瀏覽器訪問也沒有問題,那有可能是該埠被防火墻蔽掉了,既然 22 埠拒絕訪問,我們不妨嘗試使用 443 埠進行連接,
使用 vim 指令編輯 ssh 組態檔,添加以下埠資訊
$ vim ~/.ssh/config
# Add the following configuration information
Host github.com
Hostname ssh.github.com
Port 443
測驗訪問是否成功,通常不出意外的話意外就來了……
$ ssh -T [email protected]
The authenticity of host ‘[ssh.github.com]:443([unknown ip address]:443)’ can’t be established.
xxx key fingerprint is xxx:xxx.
This host key is known by the following other names/addresses:
# Delete the RSA information in line 8
~/.ssh/known_hosts:8: github.com
Host key verification failed.
這與 ssh 的運行機制有關,ssh 會將本機訪問過的計算機的 public key 記錄在 ~/.ssh/known_hosts 下,當下次訪問相同計算機時,若公鑰不同則會發出警告,避免受到攻擊,這里只需要找到 known_hosts 檔案中對應 ip 的 RSA 并洗掉便可解決,
再次測驗,看到以下資訊則表示訪問成功
$ ssh -T [email protected]
Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access.
這樣訪問 GitHub 時,ssh 就會連接 443 埠,不會報錯,

總結
總結下本次踩坑的原因,主要有兩點:
- 使用了綠色上網小工具;
- 運營商劫持 了
DNS決議;
總之:“網上沖浪也要注意暗礁,低頭走路也要抬頭看路”,以上就是本期分享啦,希望可以幫到您!
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/498595.html
標籤:其他
