How to do SCP from one EC2 Instance to another EC2 Instance

Nowadays, many websites are being hosted on cloud (Amazon Web Services) instead of traditional dedicated/shared Linux servers. Programmers who are using “scp” command to copy data from one Linux server to other Linux server are now facing issues to use same “scp” command for two ec2 instances/servers of AWS. The main reason behind this is, while using “scp” command, it asks for password of the server where the data is being copied and with ec2 instances of AWS, we normally have .ppk files and not a password.

Here is a solution for all the guys facing similar issue. If you implement this solution you can use scp command on ec2 instances without a password.

Steps to be followed:

  1. Steps to perform on first EC2 Instance (call server 1)
    1. Generate the keypair on server 1 on which you plan to run scp, ssh, sftp or rsync
    2. Login to server through shell and run the following command from anywhere
      ssh-keygen -t rsa
    3. It usually shows location where files will be generated
      /root/.ssh/id_rsa or /home/ec2-user/.ssh/id_rsa
    4. Do not enter any passphrase
  2. Steps to perform on second EC2 Instance (call server 2)
    1. Check the “sshd_config” on that server
    2. Typically it’s present in /etc/ssh/sshd_config
    3. Please uncomment following two lines in sshd_config
      RSAAuthentication yes
      PubkeyAuthentication yes
      Note => If you are logged in as EC2 User. Please do sudo su because only then it will allow editing of file /etc/ssh/sshd_config
    4. Now find authorized_keys file of server2
    5. Location of authorized_keys file of server 2 as
      /home/ec2-user/.ssh/authorized_keys OR /root/.ssh/authorized_keys
    6. Next step is to append contents of id_rsa.pub file of server 1 to authorized_keys file of server 2
    7. Location of id_rsa.pub file of server 1 as
      /home/ec2-user/.ssh/id_rsa.pub OR /root/.ssh/id_rsa.pub
    8. Once you copy the contents of id_rsa.pub file of server1 to authorized_keys file of server2, YOU ARE DONE !

You will have to follow same steps if you want to run ssh, rsync or sftp commands.

Example:

Now command to fire on server 1
scp test1.txt ec2-user@ip-10-252-1-56.us-west-2.compute.internal:/home/ec2-user/
where ec2-user@ip-10-252-1-56.us-west-2.compute.internal is Private DNS of server 2
You will get Private DNS of ec2 instance at AWS console

Facebooktwittergoogle_plusredditpinterestlinkedinmail