Assumption for SFTP setup
Needed to copy a file from SERVER2
USER2 into SERVER1
USER1
2. Navigate to the .ssh folder, issue below command which will create a public/private key pair
$ ssh-keygen –t rsa
Generating
public/private rsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
c1:21:e 3:01:26:0d:f7:ec:52:0e:0c:90:9b:6e:d8:47 user1@server1.com
The key's randomart image is:
+--[ RSA ....]----+
Note:Enter file in which to save the key (/home/user1/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in .ssh/id_rsa.
Your public key has been saved in .ssh/id_rsa.pub.
The key fingerprint is:
c1:21:e 3:01:26:0d:f7:ec:52:0e:0c:90:9b:6e:d8:47 user1@server1.com
The key's randomart image is:
+--[ RSA ....]----+
..
If we need to connect to the server automatically issuing the sftp command, don’t provide any passphrase. (just hit enter)
If we need the public/private key to be generated to a different file, provide a file name when Enter file in which to save key is prompted.
3. After issuing the ssh-keygen command, two files would be generated in the local folder id_rsa and id_rsa.pub.
Share the id_rsa.pub (public key) to the SERVER2 USER2, to which the server is going to connect.
1. Login to SERVER2 USER2, check if the .ssh folder exists. If not, create .ssh folder and change permission to 700 using the chmod 777 .ssh command.
2. Navigate to .ssh folder, check if there is any file named authorized_keys, if there is no file create it and add the id_rsa.pub content to it.
3. If authorized_keys file doesn’t exists, create one and provide 600 permission to the file using chmod 600 authorized_keys.
4. Copy the content of id_rsa.pub to the authorized_keys, just using the cp command.
$ sftp user2@server2.com
…
sftp >
Link:
https://askleo.com/how_can_i_automate_an_sftp_transfer_between_two_servers/
Exception that may occur:
Authentication refused: bad ownership or modes for directory /xxx/yyy
Solution:
Ensure the permission of the folder .ssh (700) and authorized key (600).
In my case the folder xxx was created by a different userZ and the yyy was created by different user2.
After making the folder xxx to the user2, the issue got resolved.
More information for debugging can be found in the log location /var/log/secure (Redhat 6.6)
