User Tools

Site Tools


transferring_files_to_an_ec2_instance

Transferring files to an EC2 Instance

Jun 2017


Introduction


There are a few ways to transfer files to and from an Amazon EC2 instance. If you are using a desktop environment, then you can use programs like Putty, WinSCP or MobaXterm. These graphically driven programs make transferring files very simple.

However, from the command line it can be a little trickier. In this example we will look at transferring files to and from an Amazon EC2 instance from the Linux command line using SCP.


Get your Key File


To be able to connect to your Amazon EC2 instance you will require your EC2 Instance key pair. Copy your key file to a directory on your source Linux server.



Before we can use the key we have to change its permissions, if you don't do this Amazon will reject the key on security grounds.

Use chmod to change your key.pem file permissions.


  chmod 0400 keyfile.pem


The keyfile.pem should now have the following permissions: -r——–. 1 root root 1692 Jul 26 07:47 keyfile.pem


File Transfer to EC2


We are now ready to transfer files to and from our EC2 Instance. To connect to the EC2 instance you can use either the currently assigned IP Address, your Elastic IP Address if you have one, or the Public DNS (IPv4).

My test AWS EC2 Intance has the following details.

Elastic IP Address: 35.177.4.81
Public DNS (IPv4): ec2-35-177-4-81.eu-west-2.compute.amazonaws.com


The syntax of SCP is as follows:

  scp -i <keypair> /source/myfile.txt login@address:/destination


I am going to transfer a file called TestFile.txt to the home directory on my EC2 Instance, for this I can use the following syntax:

  scp -i keyfile.pem /home/root/TestFile.txt ec2-user@35.177.4.81:/home/ec2-user


If this is the first time connecting to an instance, you will see the following message:
<sxh [text], gutter: false; highlight: 0-0;> The authenticity of host '35.177.4.81 (35.177.4.81)' can't be established. ECDSA key fingerprint is 02:8c:04:6c:e9:d5:aa:97:e3:40:35:a6:d9:f6:24:ed. Are you sure you want to continue connecting (yes/no)? </sxh>
Type yes and press enter.

You should see a transfer confirmation similar to this:

  TestFile.txt      100%    0     0.0KB/s   00:00


You can connect to your EC2 Instance via SSH to check the file(s) have copied:

  sudo ssh -i keyfile.pem ec2-user@35.177.4.81


<sxh [text], gutter: false; highlight: 0-0;> Last login: Tue Jul 25 21:16:51 2017 from dsl-217-155-64-20.zen.co.uk

     __|  __|_  )
     _|  (     /   Amazon Linux AMI
    ___|\___|___|

https://aws.amazon.com/amazon-linux-ami/2017.03-release-notes/ 4 package(s) needed for security, out of 6 available Run “sudo yum update” to apply all updates. </sxh>
if you list the home directory, you will see your file:

  ls -l
  
  -rw-r--r-- 1 ec2-user ec2-user 0 Jul 26 07:05 TestFile.txt


The file permissions are the same, but the owner and group have changed (no root user on the EC2 Instance I guess.)


File Transfer from EC2


To retrieve file(s) from the EC2 instance, use the following syntax.

  sudo scp -i keyfile.pem ec2-user@35.177.4.81:/home/ec2-user/TestFile.txt /home/root/


You will see a confirmation of the file being copied.

TestFile.txt 100% 0 0.0KB/s 00:00


Copy Entire Directory


If you wish to copy an entire directory you can use the -r switch.

Here I have a directory called TestFolder and I want copy this directory and its contents (three files, file1.txt, file2.txt and file3.txt) to my EC2 instance.

To copy the directory and the files, use the following syntax:

sudo scp -i keyfile.pem -r /home/root/TestFolder ec2-user@35.177.4.81:/home/ec2-user

file1.txt     100%    0     0.0KB/s   00:00
file2.txt     100%    0     0.0KB/s   00:00
file3.txt     100%    0     0.0KB/s   00:00


If we look at the destination folder, we will see our folder and files.

ls -l TestFolder

-rw-r--r-- 1 ec2-user ec2-user 0 Jul 26 07:22 file1.txt
-rw-r--r-- 1 ec2-user ec2-user 0 Jul 26 07:22 file2.txt
-rw-r--r-- 1 ec2-user ec2-user 0 Jul 26 07:22 file3.txt



transferring_files_to_an_ec2_instance.txt · Last modified: 2023/03/09 22:35 by 127.0.0.1