【问题标题】:Github connection with ssh fail because of bad name resolution由于名称解析错误,Github 与 ssh 的连接失败
【发布时间】:2017-08-20 00:08:58
【问题描述】:

我已经用我的 github 帐户注册了一个 ssh 密钥。 钥匙在

~/.ssh/id_rsa_github_snail
~/.ssh/id_rsa_github_snail.pub

我有一个 repo,其中远程 url 配置为使用 ssh:

$ git remote -v
origin  git@github.com:danielsank/pong.git (fetch)
origin  git@github.com:danielsank/pong.git (push)

但是,当我尝试从公司计算机推送时,我收到以下错误消息

danielsank@snail:~/src/pong$ git push
ssh: connect to host github.com.<company>.<name>.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.

请注意,由于某种原因,ssh 尝试连接到 github.com.&lt;company&gt;.&lt;name&gt;.com 而不是 github.com

我在/etc/resolve.conf中发现了以下可疑位:

# It is common within <company> to lookup hostnames with a dot in it like for an
# example '<machine>.<site>'. We set 'options ndots:2' so that such hostnames
# get tried first with the <company> search domains appended. Without this such
# queries might be tried first against an external DNS resolver.
options ndots:2

可以 ping github.com。

我尝试将网址设置为git@github.com.:danielsank/pong.git。 之后,当我尝试推送时,我被要求接受来自 github 的主机指纹,但随后出现不同的错误:

Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

我想这是由于 repo 名称中的额外点而发生的。

是否有解决此配置的好方法? 如果不附加公司名称,决议不会再试一次,我有点惊讶......


【问题讨论】:

  • 您的/etc/resolv.conf 中有什么内容?你可以ping github.com吗?试试ping github.com.(注意尾随点)。尝试将origin 更改为git@github.com.:danielsank/pong.git(再次注意添加的点)。
  • https 版本好用吗? https://github.com/danielsank/pong.git
  • @Trolleymusic 目前没有,因为我在我的 github 帐户上启用了 2-factor(这就是整个事情的开始)。

标签: git github ssh


【解决方案1】:

您在/etc/resolve.conf 或公司的名称服务器中似乎有奇怪的 DNS 设置。要防止这些 DNS 设置将 .&lt;company&gt;.&lt;name&gt;.com 附加到带有点的主机名,请在主机名末尾附加一个点:

ping github.com. # <- note the dot!
git remote set-url origin git@github.com.:danielsank/pong.git

关于第二个问题——如果你想使用非标准名称的密钥,你可以将密钥分配给~/.ssh/config中的主机:

Host github.com github.com.
    User git
    IdentityFile ~/.ssh/id_rsa_github_snail

【讨论】:

  • 合并 2 个答案。