45

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

I did

scp -r usrname@server1.com:~/data/* usrname@server2.com:~/data/
Password: 
Host key verification failed.
lost connection

Is this even possible? How may I fix it?

1
  • Can you post the output of following command- scp -r -v usrname@server1.com:~/data/* usrname@server2.com:~/data/
    – pragmatic
    Feb 12 '15 at 10:30
73

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

source

-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.

2
18

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 username@server1.com and from there ssh to username@server2.com. 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 usrname@server1.com:~/data/* usrname@server2.com:~/data/

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

2
  • 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 '15 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 '15 at 8:59
1

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.

0

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

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

0

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          
0

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
    HostName 99.111.22.333
    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
-2

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 \
         target_user@target_remote_host:/var/tmp/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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