· 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.
Below are the steps to achieve the same:
Create a public-private key pair on source machine using ssh-keygen command
[firstname.lastname@example.org]$ 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/id_rsa_test.pub. The key fingerprint is: 2d:66:09:56:c9:20:c0:04:64:0c:45:6b:08:59:69:93 email@example.com [firstname.lastname@example.org]$
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: id_rsa.pub
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/id_rsa_test.pub | ssh email@example.com '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.
[firstname.lastname@example.org ~]$ ssh email@example.com [firstname.lastname@example.org ~]$