Git SSH 错误:“连接到主机:文件编号错误”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7144811/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 11:38:58  来源:igfitidea点击:

Git SSH error: "Connect to host: Bad file number"

gitgithubssh

提问by Massimo Ugues

I followed the git guidebut I have this strange issue when trying to connect to github:

我遵循了git 指南,但是在尝试连接到 github 时遇到了这个奇怪的问题:

$ ssh -v [email protected]
OpenSSH_4.6p1, OpenSSL 0.9.8e 23 Feb 2007
debug1: Reading configuration data /c/Documents and Settings/mugues/.ssh/config
debug1: Applying options for github.com
debug1: Connecting to github.com [207.97.227.239] port 22.
debug1: connect to address 207.97.227.239 port 22: Attempt to connect timed out without establishing a connection
ssh: connect to host github.com port 22: Bad file number

This is my config file under .ssh

这是我在 .ssh 下的配置文件

Host github.com
    User git
    Hostname github.com
    PreferredAuthentications publickey
    IdentityFile "C:\Documents and Settings\mugues\.ssh\id_rsa"
    TCPKeepAlive yes
    IdentitiesOnly yes

Any idea?

任何的想法?

回答by Sam

After having this problem myself, I found a solution that works for me:

在自己遇到这个问题后,我找到了一个适合我的解决方案:

Error message:

错误信息:

    ssh -v [email protected]
    OpenSSH_5.8p1, OpenSSL 1.0.0d 8 Feb 2011
    debug1: Connecting to github.com [207.97.227.239] port 22.
    debug1: connect to address 207.97.227.239 port 22: Connection timed out
    ssh: connect to host github.com port 22: Connection timed out
    ssh: connect to host github.com port 22: Bad file number

You will only see the bad file number message when on windows using the MINGGW shell. Linux users will just get Timed out.

在 Windows 上使用 MINGGW shell 时,您只会看到错误的文件编号消息。Linux 用户只会超时。

Problem:

问题:

SSH is probably blocked on port 22. You can see this by typing

SSH 可能在端口 22 上被阻止。您可以通过键入来查看这一点

    $nmap -sS github.com -p 22
    Starting Nmap 5.35DC1 ( http://nmap.org ) at 2011-11-05 10:53 CET
    Nmap scan report for github.com (207.97.227.239)
    Host is up (0.10s latency).
    PORT   STATE    SERVICE
    22/tcp ***filtered*** ssh

    Nmap done: 1 IP address (1 host up) scanned in 2.63 seconds

As you can see the state is Filtered, which means something is blocking it. You can solve this by performing an SSH to port 443 (your firewall / isp will not block this). It is also important that you need to ssh to "ssh.github.com" instead of github.com. Otherwise, you will report to the webserver instead of the ssh server. Below are all the steps needed to solve this problem.

正如您所看到的,状态是 Filtered,这意味着有什么东西在阻止它。您可以通过对端口 443 执行 SSH 来解决此问题(您的防火墙/ISP 不会阻止此操作)。同样重要的是,您需要 ssh 到“ssh.github.com”而不是 github.com。否则,您将向 web 服务器而不是 ssh 服务器报告。以下是解决此问题所需的所有步骤。

Solution:

解决方案:

(First of all make sure you generated your keys like explained on http://help.github.com/win-set-up-git/)

(首先确保你生成了你的密钥,就像在http://help.github.com/win-set-up-git/ 上解释的那样)

create file ~/.ssh/config (ssh config file located in your user directory. On windows probably %USERPROFILE%\.ssh\config

创建文件 ~/.ssh/config(ssh 配置文件位于您的用户目录中。在 Windows 上可能 %USERPROFILE%\.ssh\config

Paste the following code in it:

在其中粘贴以下代码:

    Host github.com
    User git
    Hostname ssh.github.com
    PreferredAuthentications publickey
    IdentityFile ~/.ssh/id_rsa
    Port 443

Save the file.

保存文件。

Perform ssh like usual:

像往常一样执行 ssh:

$ssh -T github.com 
    $Enter passphrase for key '.......... (you can smile now :))

Note that I do not have to supply the username or port number.

请注意,我不必提供用户名或端口号。

回答by Stéphane Gourichon

The key information is written in @Sam's answer but not really salient, so let's make it clear.

关键信息写在@Sam 的回答中,但并不突出,所以让我们说清楚。

"Bad file number" is not informative, it's only a sign of running git's ssh on Windows.

“错误的文件号”没有提供信息,它只是在 Windows 上运行 git 的 ssh 的标志。

The line which appears even without -vswitch:

即使没有-v开关出现的行:

ssh: connect to host (some host or IP address) port 22: Bad file number

is actually irrelevant.

其实是无关紧要的

If you focus on it you'll waste your time as it is not a hint about what the actual problem is, just an effect of running git's ssh on Windows. It's not even a sign that the git or ssh install or configuration is wrong. Really, ignore it.

如果你专注于它,你会浪费你的时间,因为它不是关于实际问题是什么的暗示,只是在 Windows 上运行 git 的 ssh 的结果。这甚至不是 git 或 ssh 安装或配置错误的迹象。真的,忽略它

The very same command on Linux produced instead this message for me, which gave an actual hint about the problem:

Linux 上的相同命令为我生成了这条消息,它给出了有关问题的实际提示:

ssh: connect to host (some host or IP address) port 22: Connection timed out

Actual solution: ignore "bad file number" and get more information

实际解决方案:忽略“坏文件号”并获取更多信息

Focus on lines being added with -von command line. In my case it was:

专注于-v在命令行上添加的行。就我而言,它是:

debug1: connect to address (some host or IP address) port 22: Attempt to connect timed out without establishing a connection

My problem was a typo in the IP address, but yours may be different.

我的问题是 IP 地址的拼写错误,但您的可能不同。

Is this question about "bad file number", or about the many reasons why a connection could time out ?

这个问题是关于“错误的文件号”,还是关于连接可能超时的多种原因?

If someone can prove that "bad file number" only appears when the actual reason is "connection time out" then it makes some sense to address why connection could time out.

如果有人可以证明“错误的文件号”仅在实际原因是“连接超时”时出现,那么解决连接可能超时的原因就有意义了。

Until that, "bad file number" is only a generic error message and this question is fully answered by saying "ignore it and look for other error messages".

在此之前,“错误的文件号”只是一个通用的错误消息,这个问题完全可以通过说“忽略它并查找其他错误消息”来回答。

EDIT: Qwertie mentioned that the error message is indeed generic, as it can happen on "Connection refused" also. This confirms the analysis.

编辑:Qwertie 提到错误消息确实是通用的,因为它也可能发生在“连接被拒绝”上。这证实了分析。

Please don't clutter this question with general hints and answer, they have nothing to do with the actual topic (and title) of this question which is "Git SSH error: “Connect to host: Bad file number”". If using -vyou have more informative message that deserve their own question, then open another question, then you can make a link to it.

请不要用一般的提示和答案来混淆这个问题,它们与这个问题的实际主题(和标题)无关,即“Git SSH 错误:“连接到主机:错误的文件号””。如果使用-v你有更多的信息,值得他们自己提问,然后打开另一个问题,然后你可以创建一个链接。

回答by Srinivas Kattimani

This worked for me:

这对我有用:

ssh -v [email protected] -p 443

回答by Gerold Meisinger

Maybe your firewall or a blocker application (PeerBlock etc.) is blocking your port

也许您的防火墙或阻止程序(PeerBlock 等)正在阻止您的端口

回答by Fostah

You can also try to:

您还可以尝试:

telnet example.com 22

to see if you have connectivity to the server. I saw this message and it ended up being the VPN I was on was blocking access. Disconnected from the VPN and I was good to go.

查看您是否可以连接到服务器。我看到了这条消息,结果是我使用的 VPN 阻止了访问。与 VPN 断开连接,我很高兴。

回答by frostymarvelous

What I found is that, this happens when your connection is poor. I had it a few minutes ago when pushing to my repo, it kept failing and a while after that, the connection went down.

我发现的是,当您的连接不佳时会发生这种情况。几分钟前我在推送到我的仓库时遇到了它,它一直失败,过了一段时间,连接断开了。

After it came back up, the push immediately went through.

在它回来后,推动立即通过。

I believe it can be caused by either a drop in connection from either your side or theirs.

我相信这可能是由于你方或他们方的连接下降造成的。

回答by marknery

If SSH is blocked over 22

如果 SSH 被阻止超过 22

just update your originto https

只需将您的更新origin为 https

git remote set-url origin https://github.com/ACCOUNT_NAME/REPO_NAME.git

git remote set-url origin https://github.com/ACCOUNT_NAME/REPO_NAME.git

verify that changes were made

验证是否进行了更改

git remote -v

git remote -v

回答by om39a

Try to quit the git bash instance through which you made the setup and try reopening. It eventually worked for me.

尝试退出您进行设置的 git bash 实例并尝试重新打开。它最终对我有用。

回答by Joe Lencioni

I just had the same problem and tried every solution that I could find, but none worked. Eventually, I tried quitting Git Bash and re-opening it, and everything worked perfectly.

我只是遇到了同样的问题,并尝试了我能找到的所有解决方案,但都没有奏效。最终,我尝试退出 Git Bash 并重新打开它,一切正常。

So, try quitting Git Bash and re-opening it.

因此,尝试退出 Git Bash 并重新打开它。

回答by VonC

Double check that you have published your public keys through your GitHub Administration interface.

仔细检查您是否已通过 GitHub 管理界面发布了您的公钥。

Then make sure port 22 isn't somehow blocked (as illustrated in this question)

然后确保端口 22 没有被阻止(如本问题所示