53
votes

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.

27
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
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
@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
Try to fiddle with "Protocol" setting in sshd.conf on the server. Check ssh server log also. - shark555
@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

27 Answers

69
votes

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.

25
votes

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.

16
votes

Just enter on the server side :

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

It sorted it out for me.

13
votes

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")
10
votes

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

7
votes

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
votes

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

5
votes

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

3
votes

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
votes

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.

1
votes

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
1
votes

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.

1
votes

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.

1
votes

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
votes

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
votes

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
votes

Disconnecting and reconnecting to the current network worked for me.

1
votes

In My Case, I had Same Problem Today Beacuse Gitlab Server Is In MainTainence Please Check once is Working

0
votes

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
votes

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

0
votes

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
0
votes

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

0
votes

Please use Port 7999 and Try. it will work

0
votes

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.

0
votes

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
votes

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

This worked well for me.

0
votes

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