无法通过SSH克隆GitHub仓库:Manjaro系统连接问题
SSH克隆GitHub仓库失败问题排查与解决
问题场景
刚安装Manjaro Plasma系统,使用SSH克隆GitHub仓库时遇到连接问题,执行操作及报错如下:
[andreas@Bixente ~]$ ssh-add -l Could not open a connection to your authentication agent. [andreas@Bixente ~]$ eval "$(ssh-agent -s)" Agent pid 5346 [andreas@Bixente ~]$ ssh-add -l The agent has no identities. [andreas@Bixente ~]$ ssh-add ~/.ssh/github Identity added: /home/andreas/.ssh/github (andreas@Bixente) [andreas@Bixente ~]$ git clone git@github.com:Username/project.git ~/TestDir Cloning into '/home/andreas/TestDir'... Connection closed by 140.82.121.3 port 22 fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.
重启系统后需要重复启动ssh-agent并添加密钥,更换默认命名密钥后问题依旧,ssh -Tvvv git@github.com显示连接超时:
[andreas@Bixente .ssh]$ ssh-add id_ed25519 Identity added: id_ed25519 (andreas@Bixente) [andreas@Bixente .ssh]$ ssh -Tvvv git@github.com OpenSSH_9.1p1, OpenSSL 3.0.7 1 Nov 2022 debug1: Reading configuration data /etc/ssh/ssh_config debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/home/andreas/.ssh/known_hosts' debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/home/andreas/.ssh/known_hosts2' debug2: resolving "github.com" port 22 debug3: resolve_host: lookup github.com:22 debug3: ssh_connect_direct: entering debug1: Connecting to github.com [140.82.121.3] port 22. debug3: set_sock_tos: set socket 3 IP_TOS 0x48 debug1: connect to address 140.82.121.3 port 22: Connection timed out ssh: connect to host github.com port 22: Connection timed out
同一密钥在朋友的MacBook Air上可正常使用,且本地从未被要求将GitHub添加至known_hosts。
解决步骤
1. 解决端口22连接超时问题
从debug日志看,核心问题是网络无法连接GitHub的22端口,大概率是本地网络或运营商屏蔽了22端口。改用GitHub提供的443替代端口即可:
- 编辑
~/.ssh/config文件(无则新建),添加以下内容:Host github.com Hostname ssh.github.com Port 443 - 保存文件后执行
ssh -T git@github.com测试连接,正常会弹出主机密钥确认提示,输入yes完成验证,之后即可正常克隆仓库。
2. 解决ssh-agent重启后失效问题
每次重启都要手动启动ssh-agent和添加密钥,可通过Plasma的自动启动功能解决:
- 创建启动脚本
~/.ssh/start_ssh_agent.sh,内容如下:#!/bin/bash # 检查ssh-agent是否已运行 if ! pgrep -x "ssh-agent" > /dev/null; then eval "$(ssh-agent -s)" fi # 检查密钥是否已加载,未加载则自动添加 ssh-add -l > /dev/null || ssh-add ~/.ssh/id_ed25519 - 给脚本添加执行权限:
chmod +x ~/.ssh/start_ssh_agent.sh - 打开Plasma系统设置 → 开机和关机 → 自动启动,点击「添加」选择该脚本,设置为开机自动运行。
3. 关于known_hosts的说明
之前没被要求添加GitHub到known_hosts,是因为根本没成功建立连接。端口问题解决后,第一次成功连接时会自动触发主机密钥验证,确认后会自动写入~/.ssh/known_hosts。
内容的提问来源于stack exchange,提问作者AndreasInfo
相关产品推荐
相关产品推荐

