SCP to remote host as different user without password

· There are scenarios when we need to copy some file from one Linux Machine to another Linux Machine without using passwords (for automating the regular jobs ).

· Additional requirement may be to use a different user from source machine to destination machine.

Environment Details:

HostnameUser suser

Below are the steps to achieve the same:

Create a public-private key pair on source machine using ssh-keygen command

[]$ ssh-keygen

Generating public/private rsa key pair.

Enter file in which to save the key (/home/suser/.ssh/id_rsa): /home/suser/.ssh/id_rsa_test

Enter passphrase (empty for no passphrase):

Enter same passphrase again:

Your identification has been saved in /home/suser/.ssh/id_rsa_test.

Your public key has been saved in /home/suser/.ssh/

The key fingerprint is:



From the above command it below points are to be noted:
Default location for public and private keys files are : user_home/.ssh/ dir
Which is: /home/suser/.ssh dir .
Default private key file name: id_rsa
Default public key file name:

Since in my environment, files with these names were already present, a prompt as provided to overwrite the existing file or not.
I have chosen not to overwrite the files and hence provided another name for the file.

Once the keys are generated, we need to copy the contents of the public key to the remote machine in file: user_home/ .ssh/authorized_keys
In my env file name will be: /home/duser/ .ssh/authorized_keys

cat /home/suser/.ssh/ | ssh 'cat > .ssh/authorized_keys'

Above command will append the contents of the public key from source machine to destination machine.

Now ssh and scp command can be executed without password prompts.

[ ~]$ ssh
[ ~]$
Please follow and like us:

Please add your comments here