7

I have 2 servers, Server1 and Server2. On Server1 I have a user named user1. On Server2 I have the user named user2.

I need to be able to write a script that runs on Server1, which will scp some files to user2@Server2. Is there any way to do this without prompting for a password? I can put the password in a config file or something if necessary.

I am not able to create user2@Server1 user by the way.

2
  • Sure... scp to a remote server using the same username as the local account is exactly no different than doing it with a different username. If one works, the other will work too, you just have to specify the username otherwise the default is to use the same username!
    – Celada
    Feb 2, 2015 at 15:18
  • For the general question, see unix.stackexchange.com/questions/42674/… ... actually, that's a different user too.
    – derobert
    Feb 2, 2015 at 15:55

1 Answer 1

24

What you want are ssh-key pairs, these create 'trusted networks' that allow for password-less authentication:

On your client (server1):

[user@server1]# ssh-keygen -t rsa -b 2048
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): # Hit Enter
Enter passphrase (empty for no passphrase): # Hit Enter
Enter same passphrase again: # Hit Enter
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.

Now copy your public key to your remote server (server2):

ssh-copy-id user2@server2
[OR]
cat ~/.ssh/id_rsa.pub | ssh user2@server2 "mkdir -p ~/.ssh \
    && cat >>  ~/.ssh/authorized_keys"

Now when you run the scp (or any other ssh) command you shouldn't be prompted for a password:

scp file user2@server2:/drop/location
2

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