How to do SCP from one EC2 Instance to another EC2 Instance (2024)

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

Reference Link:

http://askleo.com/how_can_i_automate_an_sftp_transfer_between_two_servers.html

How to do SCP from one EC2 Instance to another EC2 Instance (2024)

FAQs

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

To use scp with a key pair use the following command: scp -i path/to/key file/to/copy user@ec2-xx-xx-xxx-xxx.compute1.amazonaws.com:path/to/file . To use it without a key pair, just omit the flag -i and type in the password of the user when prompted.

How do I copy one instance to another instance? ›

Resolution
  1. Create a custom AMI from the instance that you want to use as the source account. ...
  2. Share the AMI with the target account using either the Amazon EC2 console or the AWS Command Line Interface (AWS CLI). ...
  3. From the target account, find the AMI using the EC2 console or the AWS CLI.

How do I transfer files from one server to another in AWS? ›

To transfer files over AWS Transfer Family using WinSCP
  1. Open the WinSCP client.
  2. In the Login dialog box, for File protocol, choose a protocol: SFTP or FTP. ...
  3. For Host name, enter your server endpoint. ...
  4. For Port number, enter the following:

How do I move files from one EC2 instance to another? ›

At the command prompt, type the PSCP command, which includes your . ppk filename, the Public DNS of your EC2 instance, and the name of the file you want to transfer.

How do you establish connection between EC2 instance to another EC2 instance? ›

  1. Option 1: Automatically connect – EC2 console. Task 1: Create an RDS database – optional. Task 2: Launch an EC2 instance – optional. ...
  2. Option 2: Automatically connect – RDS console. Task 1: Launch an EC2 instance – optional. ...
  3. Option 3: Manually connect (create security groups) Task 1: Launch an EC2 instance – optional. ...
  4. Clean up.

How do I copy one object to another? ›

The Object. assign() method can be used to merge two objects and copy the result to a new target. Just like the spread operator, If the source objects have the same property name, the latter object will replace the preceding object. Now, let's look at another example of merging in Typescript.

How do I clone an AWS instance? ›

Clone an AWS instance
  1. Log in to the AWS Management Console.
  2. If required, use the region selector in the top right corner to switch to the region where your instance was launched.
  3. Select your instance and then select the “Create Image” option in the “Actions” menu.
Jul 11, 2019

How to transfer data securely between two EC2 instances created in separate AWS accounts? ›

Solution tutorial
  1. Step 1: Create the EC2 DataSync instance. ...
  2. Step 2: Create and activate the DataSync agent. ...
  3. Step 3: Configure the source SMB location. ...
  4. Step 4: Configure the destination location. ...
  5. Step 5: Create the replication task. ...
  6. Step 6: Start the DataSync task.
Feb 2, 2021

What can be used to transfer data securely between two EC2 instances created in separate AWS accounts? ›

To copy data to or from a self-managed in-cloud file server, or between AWS Storage services in different AWS accounts, you launch an Amazon EC2 instance using a DataSync agent AMI.

What are SFTP commands? ›

Command-line SFTP
CommandFunction
chmodChange the permissions of files on the remote host.
chownChange the owner of files on the remote host.
exit (or quit )Close the connection to the remote host, and exit SFTP.
getCopy a file from the remote host to the local computer.
17 more rows
Feb 26, 2024

How can we migrate one EC2 instance from one region to another region? ›

It's not possible to move an existing instance to another subnet, Availability Zone, or VPC. Instead, you can create a new Amazon Machine Image (AMI) from the source instance to manually migrate the instance. Next, use the new AMI to launch a new instance in the desired subnet, Availability Zone, or VPC.

How do I download files from EC2 instance? ›

  1. Step 1: Connect to Your EC2 Instance. First, you need to connect to your EC2 instance. ...
  2. Step 2: Locate the File. Once you're connected, navigate to the directory containing the file you want to download. ...
  3. Step 3: Exit the EC2 Instance. ...
  4. Step 4: Download the File. ...
  5. Step 5: Verify the Download.
Jul 23, 2023

Can EC2 instances communicate with each other? ›

When you establish peering relationships between VPCs across different AWS Regions, resources in the VPCs (for example, EC2 instances and Lambda functions) in different AWS Regions can communicate with each other using private IP addresses, without using a gateway, VPN connection, or network appliance.

How do you tell one EC2 to talk to other EC2 in another region? ›

We can place 2 instances in different network and then install a security rule. And, then assign a different IPs to the 2 EC2 instances. Both are able to talk with the internet.

How to move data from one instance to another instance in SQL? ›

Step 1: Right click on any database and select Tasks option and in Tasks select Copy Database. If you get the following error that means your SQL Server Agent is not running. For copying the database it is required that SQL Server instance must run. To remove the above error run your SQL Server instance.

How do I create a clone instance? ›

Clone an instance

Find the row of the instance to clone. In the Actions column, click the more_vert More Actions menu. Click Create clone. On the Create a clone page, update the instance ID if needed, and click Create clone, leaving Clone current state of instance selected.

How do I clone a VM instance? ›

Create a VM that is similar to an existing VM

Click the name of the VM. The VM instance details page opens. In the toolbar at the top of the VM instance details page, click Create similar. The Google Cloud console copies the configuration and opens the Create an instance page.

What is an instance copy? ›

Copy method: Creates a completely separate clone from the original. Modifying one has no effect on the other. Instance method: Creates a completely interchangeable clone of the original. Modifying an instanced object is the same as modifying the original.

Top Articles
Latest Posts
Article information

Author: Dan Stracke

Last Updated:

Views: 6348

Rating: 4.2 / 5 (43 voted)

Reviews: 82% of readers found this page helpful

Author information

Name: Dan Stracke

Birthday: 1992-08-25

Address: 2253 Brown Springs, East Alla, OH 38634-0309

Phone: +398735162064

Job: Investor Government Associate

Hobby: Shopping, LARPing, Scrapbooking, Surfing, Slacklining, Dance, Glassblowing

Introduction: My name is Dan Stracke, I am a homely, gleaming, glamorous, inquisitive, homely, gorgeous, light person who loves writing and wants to share my knowledge and understanding with you.