Recently for a POC purpose, there was a need of establishing passwordless secure shell file transfer (SCP) between two Linux servers for automated scripts to work. It wasted my couple of hours, so I decided to pen it down in the blog.
(*note server1 is the source server and server2 will be the destination server and user is root in this case)
Login into server1, you will land in the following directory /root. Perform ‘ls -a’ and see if any ‘.ssh’ directory is available. If not present, create one:
server1# mkdir ~/.ssh
server1# cd .ssh
server1# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): id_rsa
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in id_rsa.
Your public key has been saved in id_rsa.pub.
The key fingerprint is:
Copy the ‘id_rsa.pub’ file to server2 in .ssh directory within file system /root.
server1# scp /root/.ssh/id_rsa.pub root@server2:/root/.ssh/local_copy/id_rsa.server1.pub Password:
The direct scp will not work still now and will ask for server2 login password. So transport the id_rsa.server1.pub file via WinSCP or any other method.
If that is also giving you an issue, copy the content of the file from server1 and paste it to newly created file in server2.
Go to server2 /root/.ssh. Append the RSA fingerprint of server1 to server2 Authorization Key:
server2# cat id_rsa.server1.pub >> .ssh/authorized_keys
Now the connection between server1 and server2 is passwordless
[root@server1]# scp -p test_file.txt server2:/root
*Note repeat steps 1-6 for all target servers you would like passwordless access from server1
* FYI Ensure your /home/user directory has the permission 755 also