I am trying to copy files from one server directly to another, bypassing my local computer.

I did

scp -r [email protected]:~/data/* [email protected]:~/data/
Host key verification failed.
lost connection

Is this even possible? How may I fix it?


7 Answers 7


Something I use fairly often when there is no connection possible between the two servers

scp -3 user@server1:/path/to/file user@server2:/path/to/file


-3 Copies between two remote hosts are transferred through the local host. Without this option the data is copied directly between the two remote hosts. Note that this option disables the progress meter.

Assuming youu have a good connection to both, its not too slow.


Yes this is possible, but only if server1.com can reach server2.com by using that name. If not you will probably get a message: ssh: connect to host server2 port 22: Connection refused

The man page for scp clearly states that it copies between hosts on a network and that:

Copies between two remote hosts are also permitted.

What you should do (once) is ssh to [email protected] and from there ssh to [email protected]. Answer the authenticity question you'll get with yes, to get rid of the host key verification message while doing the scp

You might have to enable the forwarding agent with:

scp -o "ForwardAgent yes" -r [email protected]:~/data/* [email protected]:~/data/

But make sure you realise the danger in that (man ssh_config)

  • I just sshed from server1.com to server2.com, and I indeed answered the question and succeeded. Then, I go back to my local computer, do it again, and get the server2.com's password prompt. But it fails there with:Permission denied (publickey,keyboard-interactive). lost connection Feb 12, 2015 at 8:41
  • 1
    @SibbsGambling I think this relies on forwarding your private key. Can you login to both servers without providing a password? Could you login from server1 to server2 without providing a password. You might have to set "ForwardAgent yes".
    – Anthon
    Feb 12, 2015 at 8:59

Firstly you need to check i you are able to ssh to server2 without any error, if you get the same error, then please open the file ~/.ssh/known_hosts and search the server2 key and delete it.

Then check if you are able to ping the server by the host name

If not then please edit both the server host file as mentioned below

 #vi /etc/hosts  
 server1 192.186.x.x  
 server2 192.186.x.xx   
 :wq   ---> to save  

After you are done with this cd to the location of the file which you want to copy to the server2

the scp file_name user@server2:/path_to_the_directory ---> you can use the IP instead of the hostname you can mention -R to copy a directory.


Check if ~/.ssh/known_hosts have proper records for both servers

Check also if server1 have proper ~/.ssh/known_hosts record for server2


For Linux ,Use the following command to seamlessly copy file/folder from one server to another. This also ensures that the job is completed even if you are disconnected in between from the servers.

nohup bash -c 'scp -r [user_source@Soure_Server_ip]:/[path_to_folder] [user_dest@destination_server_ip]:/[path_to_destination_folder]'>/dev/null 2>&1          

First of all, you need to create a configuration file in ~/.ssh/config and write into it something like:

Host src_host
    HostName example.com
    User user1
    Port 22
    IdentitiesOnly yes
    IdentityFile /path/to/secret-key

Host dest_host
    User user2
    Port 3333
    IdentitiesOnly yes
    IdentityFile /path/to/secret-key2

Then, you can check whether the configuration file is working with ssh -F ~/.ssh/config src_host, where -F allows you to specify a non-default path to a configuration file. I had to go through this process, since ssh was ignoring ~/.ssh/config.

Finally, you can scp between the two hosts:

scp -3v -F .ssh/config src-host:/path/to/file1 dest-host:/path/to/file2
  • -3 directs the traffic through your machine
  • -v produces a verbose output

You can use this below command to copy file from one server to other:

scp source_user@source_remote_host:/usr/bin/mysql_backup.sh \

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .