68

I work at win7 and set up git server with sshd. I git --bare init myapp.git, and clone ssh://git@localhost/home/git/myapp.git in Cywgin correctly. But I need config git of Cygwin again, I want to git clone in Git Bash. I run git clone ssh://git@localhost/home/git/myapp.git and get following message

ssh_exchange_identification: Connection closed by remote host

then I run ssh -vvv git@localhost in Git Bash and get message

debug2: ssh_connect: needpriv 0
debug1: Connecting to localhost [127.0.0.1] port 22.
debug1: Connection established.
debug1: identity file /c/Users/MoreFreeze/.ssh/identity type -1
debug3: Not a RSA1 key file /c/Users/MoreFreeze/.ssh/id_rsa.
debug2: key_type_from_name: unknown key type '-----BEGIN'
debug3: key_read: missing keytype
debug3: key_read: missing whitespace 
// above it repeats 24 times
debug2: key_type_from_name: unknown key type '-----END'
debug3: key_read: missing keytype
debug1: identity file /c/Users/MoreFreeze/.ssh/id_rsa type 1
debug1: identity file /c/Users/MoreFreeze/.ssh/id_dsa type -1
ssh_exchange_identification: Connection closed by remote host

it seems my private keys has wrong format? And I find that there are exactly 25 line in private keys without BEGIN and END. I'm confused why it said NOT RSA1 key, I totally ensure it is RSA 2 key.

Any advises are welcome. btw, I have read first 3 pages on google about this problem.

8
  • How to analysis? Try a "ssh -vvv git@localhost" and check the permissions of .ssh directory (700) and id_rsa(.pub) files (600). See stackoverflow.com/questions/3712443/… or stackoverflow.com/questions/5470680/… for illustration.
    – VonC
    Apr 12, 2012 at 18:03
  • Have you generated rsa keys for sshd on server machine? It may also be the case of different ssh versions on client and server machine.
    – shark555
    Apr 16, 2012 at 23:58
  • @shark555 I don't know why server need a rsa key? I don't know what differences between ssh versions on client and server, so I generate a new rsa key on server and copy to client and add public key to authorized_keys on server. But it still doesn't work.
    – MoreFreeze
    Apr 18, 2012 at 1:22
  • Try to fiddle with "Protocol" setting in sshd.conf on the server. Check ssh server log also.
    – shark555
    Apr 18, 2012 at 8:51
  • @shark555 it drives me mad. I try to generate RSA1 key, and git bash can find it but it return same error. I use ssh-host-config to generate a sshd service, and keep sshd_config as origin.
    – MoreFreeze
    Apr 19, 2012 at 9:19

28 Answers 28

80

I had this problem today and I realize that I was connected to 2 differente networks (LAN and WLAN), I solved it just disconnecting the cable from my Ethernet adapter. I suppose that the problem is caused because the ssh key is tied with the MAC address of my wireless adapter. I hope this helps you.

6
  • hey, dude what a big thanks for you! I disenable the wireless adapter and then it works! Thanks again.
    – MoreFreeze
    Sep 21, 2012 at 1:57
  • 3
    For me, just unplugging and then again plug-in the LAN cable solved the problem.
    – akelec
    Sep 11, 2017 at 11:10
  • Thanks. I actually have 2 wireless adapter, sometimes one works, and sometimes another. Your post suggested me to enable the "old" one, and wah lah, git is working!
    – Ben Lin
    Mar 1, 2021 at 2:39
  • 1
    So, basically you just asked, "Is it plugged in?" I love it.
    – Parapluie
    Mar 30, 2021 at 18:36
  • 2
    @akelec can't believe this worked but it did Apr 20, 2022 at 9:21
45

For fixing the issues add the Hostname for Git on ~/.ssh/config,

Host github.com
 Hostname ssh.github.com
 Port 443

In my case github!

5
  • 2
    Thanks for your advice, It works for me. I had the same problem when I use VPN to connect to github,since github was blocked in China. Via VPN I found http is ok ,but when I use ssh with default port 22 and default host name github.com,"kex_exchange_identification: Connection closed by remote host" error always happened.
    – Allen
    Mar 20, 2021 at 7:42
  • Maybe you need chown $USER ~/.ssh/config and chmod 644 ~/.ssh/config if you meet the error Bad owner or permissions
    – ding van
    Sep 6, 2022 at 9:22
  • Was in an airport on free wifi, and it blocked me on port 22. Implemented this, and everything worked perfectly (after I confirmed the authenticity of the host). Nov 8, 2022 at 11:35
  • adding hostname worked for me! May 6 at 4:17
  • 1
    Worked for me, thanks! I am in mainland China too.
    – intlsy
    May 17 at 13:56
30

I just ran into this today and it was because the server I was trying to connect to was overloaded with processing. So it may be possible that the server is low on memory or CPU starved.

3
  • Really, it happened to me when a server (if it's interesting, with RHEL) had "hanged" due to 100% CPU usage. Nasty thing.
    – John_West
    Mar 31, 2016 at 19:34
  • too true, it happens with GitLab all the time and whenever it does, it returns a 500 error on the website. Annoying much! Jan 9, 2017 at 22:47
  • Github.com has been failing due to a lot of timeouts. We get a variety of errors, including this one, depending on the system. Not sure what's happening on their side, but this week (2019-04-15) has been really slow despite their status page showing perfect :)
    – Benxamin
    Apr 17, 2019 at 15:17
17

Just enter on the server side :

echo 'SSHD: ALL' >> /etc/hosts.allow

It sorted it out for me.

7
  • Yes, denyhosts sometimes can deny your address (debian 7) Sep 16, 2013 at 1:00
  • 3
    I tried doing this and I got a permission denied...I tried using sudo and it still says permission denied...any fixes?
    – ragzputin
    Mar 29, 2016 at 0:47
  • ^ Same: permission denied, even when including sudo
    – dylanh724
    Sep 11, 2017 at 4:14
  • I think you can use chmod 777 /etc/host.allow and the append should work. Feb 5, 2019 at 22:24
  • 2
    Do NOT chmod 777 that file. That's global access to guests. Instead, modify it properly: sudo -s "echo 'SSHD: ALL' >> /etc/hosts.allow" Apr 10, 2020 at 16:11
15

Hit the following ssh restart command in linux

prayag@prayag:~/backup/NoisyNeighbour$ service ssh restart 
stop: Rejected send message, 1 matched rules; type="method_call", sender=":1.75" (uid=1417676764 pid=5933 comm="stop ssh ") interface="com.ubuntu.Upstart0_6.Job" member="Stop" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")
start: Rejected send message, 1 matched rules; type="method_call", sender=":1.76" (uid=1417676764 pid=5930 comm="start ssh ") interface="com.ubuntu.Upstart0_6.Job" member="Start" error name="(unset)" requested_reply="0" destination="com.ubuntu.Upstart" (uid=0 pid=1 comm="/sbin/init")
2
12

Make sure you are not connect to any kind of VPN.

2
  • 2
    @deborah-digges actually you need to make sure you VPN host has access to your git repository. If it does you should be fine.
    – Campinho
    Jun 9, 2016 at 7:36
  • This was my issue - disconnected from the VPN and it worked fine.
    – Magnus
    May 8, 2019 at 20:20
7

Got the same error message. Turning off WiFi and turning it back on again worked for me.

0
7

After removing/deleting the rm ~/.ssh/known_hosts, my issue was fixed

0
5

I solved it after changing the ssh port & MaxStartups variable in /etc/ssh/sshd_config to ,

port 2244
MaxStartups 100

Then, restart the service

service sshd restart

If still it does not work, restart you system.

4

if hostname does not work, try IP address.

This is going on right now so I have to say. I try to ssh with my host name and it does not work

ssh [email protected]

this gives the error "ssh_exchange_identification: Connection closed by remote host"

this USED to work one hour back.

BUT, and here is the interesting part, the IP address works!

ssh [email protected]

(of course the actual IP address is different)

Go figure!

3

Remove any config in ~/.ssh/config or other ssh config places which will disallow it to reach to Github servers

In my case it was below config, and I wasn't connected to VPN.

Host *
  ProxyJump 10.0.0.50 

Note :- If I am connected to VPN, This config would mean git clone or any git operation will be send via VPN which might be slow depending on type of VPN we are using.

1
  • 1
    This is also my case, just start vpn, then it works.
    – Eric
    Jun 21 at 4:10
2

Hi I fix this on one vps service, restarting it, other way is if you have a console from your service o any other way to run a command in your remote machine the only command you must run is restart the ssh daemon and enjoy!! :P

/etc/init.d/ssh restart
2

We migrated our git host instance/servers this morning to a new data center and while being connected to both: VPN (from remote/home) or when in office network, I got the same error and was not able to connect to clone any GIT repo.

Cloning into 'some_repo_in_git_dev'...
ssh_exchange_identification: Connection closed by remote host
fatal: Could not read from remote repository.

This will help if you are connecting to some or all servers via a jump host server.

Earlier in my ~/.ssh/config file, my setting to connect were:

Host * !ssh.somejumphost.my.company.com
     ProxyCommand ssh -q -W %h:%p ssh.somejumphost.my.company.com

What this means is, for any SSH based connection, it will connect to any * server via the given jump host server except/by ignoring "ssh.somejumphost.my.company.com" server (as we don't want to connect to a jump host via jump host server.

To FIX the issue, all I did was, change the config to ignore git server as well:

Host * !ssh.somejumphost.my.company.com !mycompany-git.server.com !OrMyCompany-some-other-git-instance.server.com
     ProxyCommand ssh -q -W %h:%p ssh.somejumphost.my.company.com

So, now to connect to mycompany-git.server.com while doing git clone (git SSH url), I'm telling SSH not to use a jump host for those two extra git instances/servers.

2

For me this was caused by a limit on the number of concurrent ssh sessions. I added the two params below to /etc/ssh/sshd_config and then things worked.

echo 'MaxSessions 2000' >> /etc/ssh/sshd_config
echo 'MaxStartups 2000' >> /etc/ssh/sshd_config
service ssh restart
2

Similar to Arun Sangal the problem lied in an in .ssh/config entry

Host my.sshhost.com
  ProxyCommand ssh -q -W %h:%p myremotemachine.my.company.com

The remote machine was added to avoid with ssh for VPN connections and worked well. But for the vacation period I switched off the myremotemachine and run into the described problem.

2

To solve this edit /etc/ssh/ssh_config and comment the following line 1

ProxyCommand /usr/bin/sss_ssh_knownhostsproxy -p %p %h

https://www.evilbox.ro/linux/solve-ssh_exchange_identification-connection-closed-by-remote-host/ for reference

1
  • 4
    Please add some explanation to your answer such that others can learn from it
    – Nico Haase
    Apr 11, 2020 at 21:32
2

Disconnecting and reconnecting to the current network worked for me.

2

Today Gitlab Server was in maintainence. Please check if it's working later.

1

Got the same error too when connecting to GitHub with ssh as I move from one workplace to another. according to my situation, it seems that dns servers of different networks may get various ip address of github and the known_hosts file not identify it when changes happened. So change dns or switch back original network may work.

0
1

You can get "ssh_exchange_identification: Connection closed by remote host" if your sshd service is not operational!

If you have access to the server check you have the sshd service running with:

  ps aux | grep ssh

and check it is listening on port 22:

 netstat -plant | grep :22

more details here

1

Simple server reboot solved the problem for me. Try hard reboot , if dont work after soft reboot.

1

Please use Port 7999 and Try. it will work

1

I solved it this way.

ssh -vvv <username>@github.com to see github IP.

Then opened browser and opened github via IP to find it was an issue with firewall.

1

I experienced this today and I just do a:

12345@123456 MINGW64 ~/development/workspace/test (develop)
$ git status
Refresh index: 100% (1204/1204), done.
On branch develop
Your branch is up to date with 'origin/develop'.

nothing to commit, working tree clean

12345@123456 MINGW64 ~/development/workspace/test (develop)
$ git fetch

Then all worked again.

1

For me, the issue was that there was a proxy set in /etc/ssh/ssh_config and it was down, solved the issue by whitelisting the remote git IP and removing the proxy line.

Hope this helps someone.

0
1

In windows machine, remove the content of config file present in C:\Users{yourusername}.ssh

This worked well for me.

1
  • Bad idea in most cases. Your config was obviously wrong and the defaults were better. Nov 10, 2022 at 17:30
1

If you are using a VPN, Turn it off and try to push again.

1

In my case I am developing a script for setting up Raspberry Pies. I forgot to add in that script enabling of the ssh.

Not the answer you're looking for? Browse other questions tagged or ask your own question.