I want to copy files using scp between two remote hosts with key-based authentication.

scp -i /home/user/.ssh/id_rsa user@host1:/tmp/myfile user@host2:/tmp/

The problem is that the host2 still prompts me for a password, even though I have copied the public key to both servers. Is there a way to make this work?

  • 1
    What does it say in the /var/log/secure or /var/log/auth on host2? And what do you mean by 'copied the public key to both servers' did you literary put the id_rsa.pub in host2's .ssh directory (which is wrong) or did you add it to the authorized_keys file in the .ssh directory of host2. – devnull Feb 11 '15 at 1:23
  • Let's say I swaped the hosts and the second one ask me the password. In fact I started using the same host for test. – user2264099 Feb 11 '15 at 1:30
  • I am confused by what you just said, could you elaborate more? – devnull Feb 11 '15 at 1:32
  • Also take a look at this and see if that helps. – devnull Feb 11 '15 at 1:36
  • I mean if I try scp -i /home/user/.ssh/id_rsa user@host2:/tmp/someotherfile user@host1:/tmp/ host1 ask me the password now. Other try is: scp -i /home/user/.ssh/id_rsa user@host1:/tmp/file1 user@host1:/tmp/file1.copy and I have to enter the password one time. I will try using ~/.ssh/config to specify the key for each host. – user2264099 Feb 11 '15 at 1:54

Do you start from host1 or host2? in which case there is a redundant host_, also if both user are the same, and same from starting host, user@ is redundant.

To sum up, assuming you start from host1

  • copy content of id_rsa.pub to the authorized_keys on host2
  • to fetch remote file (remote to local)

    scp -i /home/user/.ssh/id_rsa host2:/tmp/somedistantfile /tmp
  • to send (local to remote )

    scp -i /home/user/.ssh/id_rsa /tmp/somelocalfile host2:/tmp

You can copy directly between the remote machines by using:

ssh -A -t -v user-1@source-host scp /path/to/source/file user-2@dest-host:/path/to/dest/file

This will not use your local machine but will directly copy the file from source-host to dest-host which might be useful for large files.

You should first do ssh-add .ssh/my-private-key or use -i .ssh/my-private-key param in the above command.

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.