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.

Yamaneko
  • 3,433
  • 2
  • 38
  • 57
MoreFreeze
  • 2,856
  • 3
  • 24
  • 34
  • How to analysis? Try a "`ssh -vvv git@localhost`" and check the permissions of `.ssh` directory (700) and `id_rsa(.pub)` files (600). See http://stackoverflow.com/questions/3712443/creating-ssh-keys-for-gerrit-and-hudson/3712619#3712619 or http://stackoverflow.com/questions/5470680/gitosis-post-receive-hook-to-deploy-repository-getting-public-key-errors/5471139#5471139 for illustration. – VonC Apr 12 '12 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 '12 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 '12 at 01:22
  • Try to fiddle with "Protocol" setting in sshd.conf on the server. Check ssh server log also. – shark555 Apr 18 '12 at 08: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 '12 at 09:19
  • Are you able JUST login with ssh ? I would like to know if it's git specific problem or rather something more serious. Maybe you should just try different SSH server for Windows? – shark555 Apr 19 '12 at 10:36
  • @shark555 I think it is problem that I have try many ssh(like copssh) and never clean system, so there is something wrong about Git or keys. In fact I have set up git server on my other machine for once, and I can ssh with Git Bash. But thank for your help. Have a good day. – MoreFreeze Apr 20 '12 at 02:03
  • If you have checked all the usual stuff (permissions, etc.) and you still get those messages, they do not necessarily indicate that anything is wrong with your RSA type 2 key. I have seen those exact messages on a working/valid RSA type 2 private key. The messages seem to be related to checking if it is a type 1 key or some other type of key. A key with those messages (using the -vvv option) WILL STILL WORK. I am logged into my server with such a key right now. – MountainX Jun 22 '13 at 18:05

28 Answers28

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.

PachinSV
  • 3,680
  • 2
  • 29
  • 41
42

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!

Rub21
  • 647
  • 7
  • 5
  • 1
    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 '21 at 07: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 06 '22 at 09: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). – Noah Bar-Shain Nov 08 '22 at 11:35
  • adding hostname worked for me! – Maya Nastasya May 06 '23 at 04:17
  • Worked for me, thanks! I am in mainland China too. – intlsy May 17 '23 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.

Cory Klein
  • 51,188
  • 43
  • 183
  • 243
  • 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 '16 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! – TechnicalTophat Jan 09 '17 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 '19 at 15:17
17

Just enter on the server side :

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

It sorted it out for me.

hornetbzz
  • 9,188
  • 5
  • 36
  • 53
David Okwii
  • 7,352
  • 2
  • 36
  • 29
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")
prayagupa
  • 30,204
  • 14
  • 155
  • 192
  • 8
    The fact that restarting helps is ... worrisome. – Raphael Jun 02 '14 at 23:27
  • 1
    @Raphael it may be because the SSH service was not started: https://unix.stackexchange.com/questions/128894/ssh-exchange-identification-connection-closed-by-remote-host-not-using-hosts-d/439945#439945 – baptx Apr 25 '18 at 11:17
12

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

Campinho
  • 432
  • 5
  • 13
  • 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 09 '16 at 07:36
  • This was my issue - disconnected from the VPN and it worked fine. – Magnus May 08 '19 at 20:20
7

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

Dan Sheffler
  • 81
  • 1
  • 3
7

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

asmmahmud
  • 4,844
  • 2
  • 40
  • 47
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.

Vikas Gupta
  • 10,779
  • 4
  • 35
  • 42
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 root@host.example.net

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 root@192.168.0.100

(of course the actual IP address is different)

Go figure!

Kinjal Dixit
  • 7,777
  • 2
  • 59
  • 68
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.

itsjwala
  • 106
  • 7
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
elin3t
  • 1,881
  • 1
  • 24
  • 29
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.

AKS
  • 16,482
  • 43
  • 166
  • 258
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
thebiggestlebowski
  • 2,610
  • 1
  • 33
  • 30
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.

Vlad Isajkin
  • 361
  • 3
  • 8
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

Community
  • 1
  • 1
2

Disconnecting and reconnecting to the current network worked for me.

Debu Shinobi
  • 2,057
  • 18
  • 21
2

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

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Paresh Shiyal
  • 534
  • 4
  • 15
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.

di tang
  • 21
  • 2
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

ErichBSchulz
  • 15,047
  • 5
  • 57
  • 61
1

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

Babasaheb
  • 11
  • 3
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.

Jenil Mewada
  • 575
  • 4
  • 14
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.

pvma
  • 403
  • 8
  • 14
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.

1

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

This worked well for me.

Siddharth Sachdeva
  • 1,812
  • 1
  • 20
  • 29
1

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

M1X
  • 4,971
  • 10
  • 61
  • 123
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.

Evgeniy Raev
  • 11
  • 1
  • 4