154

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

$ ssh -v git@github.com
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

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?

Shyam Bhimani
  • 1,310
  • 1
  • 22
  • 37
Massimo Ugues
  • 4,373
  • 8
  • 43
  • 56

19 Answers19

188

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

Error message:

    ssh -v git@github.com
    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.

Problem:

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

    $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.

Solution:

(First of all make sure you generated your keys like explained on 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

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 -T github.com 
    $Enter passphrase for key '.......... (you can smile now :))

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

Kewin Dousse
  • 3,880
  • 2
  • 25
  • 46
Sam
  • 2,647
  • 2
  • 20
  • 25
  • 4
    In other words, you establish [SSH connections over the HTTPS port](https://help.github.com/articles/using-ssh-over-the-https-port). – Enrico Campidoglio Oct 09 '12 at 08:29
  • 1
    At the "Paste the following code in it:", I don't understand. How am I suppose to resolve bad file number? Should I create it and save as notepad file? – David Dimalanta Jul 09 '13 at 01:12
  • 27
    I instead get `ssh: connect to host ssh.github.com port 443: Bad file number` – ZK Zhao Sep 08 '13 at 13:38
  • This also worked for bitbucket.org, when my previously working setup suddenly stopped working. Best part is that the only thing I had to do was make changes to the SSH config file. – Kevin Condon Feb 20 '14 at 20:15
  • I have deleted existing host ssh key and added again - this worked for me – amar May 14 '14 at 10:35
  • 2
    When using a `.ssh/config` file on windows 7, make sure you have a User-Enviromental Var `HOME` with `%USERPROFILE%` as value -> helped me, when my ssh could not find it – Jook Jul 18 '14 at 00:30
  • Awesome answer! It was really helpful... I had very strange issue, when 22 port in my Vagrant box just closed time to time... I created a config and: "Oh God, it's works!". Thank you. – Dmytro Medvid Jun 28 '16 at 07:42
40

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

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

The line which appears even without -v switch:

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.

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

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 -v on command line. In my case it was:

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.

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.

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 -v you have more informative message that deserve their own question, then open another question, then you can make a link to it.

Stéphane Gourichon
  • 6,493
  • 4
  • 37
  • 48
  • 1
    Yup, adding -v to my `scp` command line added "debug1: connect to address 216.34.181.70 port 22: Connection refused" before "Bad file number" so it is not always a "timed out" error. – Qwertie Jul 03 '14 at 19:59
  • Oh, Windows always shows vague error messages, even when the tool is traditionally used on Linux and other UNIX-like systems... – lilydjwg Mar 19 '16 at 11:35
15

This worked for me:

ssh -v git@github.com -p 443
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
5

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

Gerold Meisinger
  • 4,500
  • 5
  • 26
  • 33
5

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.

Fostah
  • 2,947
  • 4
  • 56
  • 78
4

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.

frostymarvelous
  • 2,786
  • 32
  • 43
  • 1
    I get this error as well when using my Verizon Jetpack, which seems to be dropping my connection when I'm using ssh from two separate devices. So something in the Jetpack is dropping the connection, and I get the `bad file number` error when the connection goes down. – cod3monk3y Dec 28 '13 at 15:46
  • 1
    I get this error when using my phone's hotspot connection with my laptop. – Lucas Apr 15 '15 at 19:26
3

If SSH is blocked over 22

just update your origin to https

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

verify that changes were made

git remote -v

marknery
  • 1,533
  • 3
  • 19
  • 27
  • correct, however you will have to do this for every repository with this method. In a config file it is applied globally. – Sam Jun 09 '15 at 12:22
2

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

om39a
  • 1,406
  • 4
  • 20
  • 39
  • 10
    "_eventually_ worked for me" makes me think that you might've been doing other things in the process which may have contributed. – Jake Berger Oct 31 '13 at 18:01
1

On windows I tried to do quit git bash and re-run but didn't work, finally me(frustated) did a restart and it worked the next time :)

nischayn22
  • 447
  • 3
  • 14
1

In my case the IP address of our git host had changed.

Simply flushing the DNS cache fixed the problem.

aboy021
  • 2,096
  • 2
  • 23
  • 23
1

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

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

Community
  • 1
  • 1
VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • 1
    >>First make sure the 'git' is your GitHub user account name. As described on the git guide: _Test everything out. To make sure everything is working you’ll now SSH to GitHub. Don’t change the “git@github.com” part. That’s supposed to be there._ >>Then make sure port 22 isn't somehow blocked --> I disabled the windows xp firewall but nothing changed. – Massimo Ugues Aug 22 '11 at 13:41
1

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.

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

Joe Lencioni
  • 10,231
  • 18
  • 55
  • 66
0

Creating the config file to use port 443 didn't work for me. Finally I tried to turn off my wifi connection, turn it on again and the problem disappeared. Weird. Silly solution but it may help someone :)

teleco
  • 21
  • 5
0

Check your remote with git remote -v Something like ssh:///gituser@myhost:/git/dev.git

is wrong because of the triple /// slash

daitangio
  • 583
  • 1
  • 6
  • 18
0

I saw this issue when I access bitbucket in corporate network, while git works fine in home network.

$ git pull
ssh: connect to host bitbucket.org port 22: Bad file number
fatal: Could not read from remote repository.

I used https protocol to workaround this.

$ git pull https://myaccount@bitbucket.org/myaccount/myrepo.git
Password for 'https://myaccount@bitbucket.org':

Please use corresponding words to replace "myaccount" and "myrepo".

ywu
  • 131
  • 2
  • 6
0

The following solution worked for me when tried to SSH into to AWS EC2 Ubuntu instance from my Windows 7 (32 Bit) PC behind corporate firewall setting up Proxy-

Add the following block to C:\Users\<YOUR_WINDOWS_USER>\.ssh\config file-

> Host *
>      ProxyCommand "C:/Program Files/Git/mingw32/bin/connect.exe" -H <YOUR_PROXY_SERVER_HOST>:<YOUR_PROXY_SERVER_PORT> %h %p
>      IdentityFile "<PATH_OF_YOUR_IDENTITY_FILE>"
>      TCPKeepAlive yes
>      IdentitiesOnly yes
>     
>     Host <SERVER_HOST_NAME_OR_IP_YOU_WANT_TO_SSH_INTO>
>      Port <SERVER_HOST_PORT_YOU_WANT_TO_SSH_INTO>
>      Hostname <SERVER_HOST_NAME_OR_IP_YOU_WANT_TO_SSH_INTO>

You will need to add similar configuration per host that you want to SSH into.

Kunal Patil
  • 745
  • 1
  • 9
  • 18
-1

This is the simple solution for saving some typing you can use the following steps in git bash easily..

(1) create the remote repository

git remote add origin https://{your_username}:{your_password}@github.com/{your_username}/repo.git

Note: If your password contains '@' sign use '%40' instead of that

(2) Then do anything you want with the remote repository

ex:- git push origin master
-1

I had the problem when I had an open FileZilla-Connection on Windows. Closed FileZilla -> Problem solved.

Raphi
  • 9
  • 1
-2

In my case simply restarting the WiFi router helped.