8

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.

devnull
  • 5,431
  • 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 02 '15 at 15:18
  • For the general question, see http://unix.stackexchange.com/questions/42674/how-can-i-set-up-ssh-on-linux-to-log-in-password-less ... actually, that's a different user too. – derobert Feb 02 '15 at 15:55

1 Answers1

27

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
devnull
  • 5,431
  • BTW: We have a question about how to copy the key at http://unix.stackexchange.com/questions/29386/how-do-you-copy-the-public-key-to-a-ssh-server – derobert Feb 02 '15 at 15:58
  • does trust exist if i change my pw? Or do I have to redo this?? – jim Mar 11 '21 at 17:04