Not working scp command: Connection closed by remote host (Mac OS X)

I'm working on a remote server through Mac terminal, since I updated it to OSX 10.10 from 10.5 I started receiving this message every time I try to scp from the server to my computer:

ssh_exchange_identification: Connection closed by remote host
lost connection

If I perform scp backwards (copying from mac to server) it works just fine, and it works fine if I do it form another mac.

If I do a verobse scp, it gives me this:

Executing: program /usr/bin/ssh host xx.xx.xx.x, user User, command scp -v -t /Users/User
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug2: ssh_connect: needpriv 0
debug1: Connecting to xx.xx.xx.x [xx.xx.xx.x] port 22.
debug1: Connection established.
debug1: identity file /home/user/.ssh/identity type -1
debug1: identity file /home/user/.ssh/identity-cert type -1
debug1: identity file /home/user/.ssh/id_rsa type -1
debug1: identity file /home/user/.ssh/id_rsa-cert type -1
debug1: identity file /home/user/.ssh/id_dsa type -1
debug1: identity file /home/user/.ssh/id_dsa-cert type -1
debug1: identity file /home/user/.ssh/id_ecdsa type -1
debug1: identity file /home/user/.ssh/id_ecdsa-cert type -1
ssh_exchange_identification: Connection closed by remote host
lost connection

I tried looking in the various host, config, ssh files but I didn't mange to solve much.

Answers


I just had the same issues with an SCP from my personal Mac to a Mac desktop we use as a local server. Running sudo sshd -t showed me the following hint:

/var/empty must be owned by root and not group or world-writable

All I did was change the owner back to root (I'm not sure why it wasn't already, but I remember upgrading from Mavericks to Yosemite gave me a couple headaches regarding permissions):

cd /var
sudo chown root empty

I hope it helps.


I got this problem too.

I tried "sudo sshd -t" , but there seemed no problem.

Then I checked my "hosts.allow" & "hosts.deny", no problem ether.

Finally I found out that I use the '~' path int the command, here's it:

scp ~/Downloads/afile root@host2:~/Downloads

I changed it to the following one:

scp /home/Downloads/afile root@host2:/home/Downloads

And it works now. Hope this can help.


Check the target user's .bashrc or equivalent file. ~/.bashrc is sourced for non-interactive logins. If there's an echo or command that outputs anything, it will break the SCP protocol.


'sudo sshd -t' clearly showed which config line is not valid. Got over the SCP problem once commenting that config line. Thanks a lot for answers.


System Preferences pane → Sharing applet → check the Remote Login checkbox. This will enable SSH, and in turn, SCP.


I was getting same error when I was using scp -P [wrong_custom_port] user@host:/source /destination

After googling for a while and not finding any direct answers I finally realized that I was using different custom port number for my ssh connection on remote host.

Hope this to help someone with similar problem.


User on server and on your desktop have to create keys locally before using ssh (or scp). You can create a strong key (algorithm RSA and length 4096) with this command :

ssh-keygen -t rsa -b 4096 -C "username"

If you already have key on your server check permission on ~/.ssh dir

As mentioned here, owner of .ssh dir and file in this directory must be your user. Permission on .ssh dir must be 700 (drwx------), private key files 600 (-rw------) and public key must be 644 (-rw-r--r--). You can check this with :

ls -alh

If it is not the case you can change this :

   chown -R username ~/.ssh
   chmod 700 ~/.ssh
   chmod 600 ~/.ssh/id_*
   chmod 644 ~/.ssh/*.pub

Need Your Help

Can PyChef be used to upload cookbook?

python chef knife pychef

I want to upload cookbooks through a python program , without using knife utility.