ssh:连接到主机端口22:连接超时



在我的新工作计算机上安装git,生成ssh密钥并将其添加到gitlab后,我试图克隆一个项目,但我得到了以下错误:

ssh: connect to host <private-domain>.com port 22: Connection timed out
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.

我还尝试了使用verbose选项测试ssh连接的命令,得到的是:

$ ssh -Tvvv appgit@<private_domain>.com
OpenSSH_8.8p1, OpenSSL 1.1.1m  14 Dec 2021
debug1: Reading configuration data /etc/ssh/ssh_config
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts' -> '/h/.ssh/known_hosts'
debug3: expanded UserKnownHostsFile '~/.ssh/known_hosts2' -> '/h/.ssh/known_hosts2'
debug2: resolving "<private_domain>.com" port 22
debug3: resolve_host: lookup <private_domain>.com:22
debug3: ssh_connect_direct: entering
debug1: Connecting to <private_domain>.com [<serv.ip.add.ress>] port 22.
debug3: set_sock_tos: set socket 4 IP_TOS 0x48
debug1: connect to address <serv.ip.add.ress> port 22: Connection timed out
ssh: connect to host <private_domain>.com port 22: Connection timed out

我知道域存在,ping <private-domain>.com有效。我不认为这是代理问题,因为我没有连接http或https。

这个答案中的任何修复都没有改变任何东西。(我在Windows上(


我注意到,如果我删除~/.ssh文件夹中的密钥,我会收到同样的错误,这让我认为这是密钥问题,而不是网络问题。如何确定git使用的密钥正确?

我尝试过ssh-keygen -lf ~/.ssh/id_rsa -E md5,看看指纹是否与gitlab上的指纹匹配(确实匹配(,但这只会给我文件夹中的指纹,而不一定是git使用的指纹。GitguiHelp>Show SSH Key确实正确显示了我的密钥。

仔细检查:

  • 远程服务器至少在端口22 上应答

    curl -v telnet://<private_domain>.com:22
    

(connect to address <serv.ip.add.ress> port 22: Connection timed out部分似乎表明远程服务器没有侦听,或者本地服务器阻止了任何出口SSH连接(

  • 远程GitLab服务器确实配置了一个名为appgit的技术帐户:通常使用的默认帐户是git
    以防万一,用ssh -Tvvv git@<private_domain>.com再次测试

并确保您的密钥使用默认命名方案(如~/.ssh/id_rsa[.pub](

我通过删除~/.ssh/known_hosts中的行来解决这个问题。删除所有与该地址相关的主机或ip。

www.####.com,xx.xx.xxx.xxx
10.15.##.## ssh-rsa AAAAB3NzaC1yc2EA

如果您使用的是在自定义域上运行的gitlab,您可以执行以下

  • 通过执行ssh添加操作,将您的git私钥添加到本地机器上的ssh代理
  • 将以下配置文件添加到您当前的用户主目录中。(~/.ssh/config,相应地更新参数(

Host gitlab
HostName mycustomgitlabdomain.com
User my-git-user
IdentityFile ~/.ssh/my_private_key

此外,检查以下

  • 您是否能够使用任何用户在远程服务器上执行SSH
  • 检查防火墙规则,如果有任何阻塞
  • 通过将域名替换为IP地址来检查访问
  • 检查远程服务器的SSH端口(可能将SSH服务配置为在不同端口上运行(

最新更新