AWS

How to transfer files from EC2 instance to local machine

While managing EC2 instances on AWS, you need to move data from an EC2 instance to the local machine. By default, the AWS EC2 instance requires SSH key authentication to access the instance, so sometimes, it is difficult for beginners to download data from the EC2 instance to the local machine. There are multiple ways to move data from the EC2 instance to the local machine, and this blog describes the following ways.

Moving files via scp command

The scp (secure copy) command is used to securely transfer files from local machines to remote servers and vice versa. This command uses the SSH protocol to move files to and from the remote server. You need to have the private key pair used to SSH into the server to use this command. If password-based authentication is enabled on your SSH server, you can use the username and password instead of the SSH key to move the files from the EC2 instance to the local machine.

The scp command can be used as follows.

ubuntu@ubuntu:~$ scp -i <private key pair> <username>@<public IP>:<file source on EC2> <file destination on local>

Or if the password-based authentication is enabled.

ubuntu@ubuntu:~$ scp <private key pair> <username>@<public IP>:<file source on EC2 instance> <file destination on local machine>

To copy a file “file1.txt” from the home directory of the EC2 instance to the current working directory on the local system, use the scp command as follows.

ubuntu@ubuntu:~$ scp -i PublicKP.pem ubuntu@<public IP>:~/file1.txt ./

Or if the password-based authentication is enabled.

ubuntu@ubuntu:~$ scp ubuntu@<public IP>:~/file1.txt ./

After copying the file, use the ls command to check whether the file was copied from the EC2 instance or not.

ubuntu@ubuntu:~$ ls

By default, the scp command copies the data over port 22 (default SSh port), but if the SSH server is running on some other port like 5000, the -P option is also added along with the scp command to specify the port.

ubuntu@ubuntu:~$ scp -i PublicKP.pem -P 5000 ubuntu@:~/file1.txt ./

To copy a directory along with all the files inside it recursively, use the -r option.

ubuntu@ubuntu:~$ scp -i PublicKP.pem -P 5000 -r ubuntu@<public IP>:~/dir ./

After downloading the directory from EC2, list all the files inside it using the ls command.

ubuntu@ubuntu:~$ ls dir/

Moving files via S3

S3 (simple storage service) is a highly available and infinitely scalable storage service provided by AWS. The best part of this service is you will only be charged for what storage you use. S3 can be used as an intermediate service to transfer files from an EC2 instance to the local system. First, transfer the file from the EC2 instance to the S3 and then download the file from the S3 console. To move files to S3, the first SSH into your EC2 instance.

ubuntu@ubuntu:~$ ssh ubuntu@<public ip address> -i <key pair>

Before moving the file to S3, first, make sure,

  • The awscli package is installed on your instance
  • The instance has access to upload files to S3

You can read about installing the awscli package and granting access to the instance to upload files on S3 here (How to transfer files from EC2 to S3). Upload the file to the S3 bucket using the following command.

ubuntu@ubuntu:~$ aws s3 mv text-file.txt s3://linuxhint-demo-bucket/

After uploading the file from the EC2 instance to S3, now download the file from S3 to the local system. For this, login to the AWS management console and search for the S3 service.

Now search for the bucket name from the S3 console you have moved your file to, ‘linuxhint-demo-bucket’ in this case.

Go to the bucket, select the file you transferred from the EC2 instance and click on the ‘Download’ button to download the file on your local system.

Moving files via FTP server

Another way to transfer files from the EC2 instance to the local system is by using the FTP server. FTP is a file transfer protocol used to transfer files from one system to another system. To transfer files to your local system from the EC2 instance, first install the FTP server on your EC2 instance.

ubuntu@ubuntu:~$ sudo apt install vsftpd

Start and enable the vsftpd service on your EC2 instance.

ubuntu@ubuntu:~$ sudo systemctl start vsftpd

ubuntu@ubuntu:~$ sudo systemctl enable vsftpd

After installing the FTP server on the EC2 instance, now set the password for the user. EC2 instance, by default, does not have a password set for the default user account. That is why you need to set up the password for the default user on the EC2 instance.

ubuntu@ubuntu:~$ sudo passwd <username>

After setting up the password for the default user account on the EC2 instance, now install the filezilla FTP client application on your local machine. The filezilla client application will be used to connect to the FTP server on the EC2 instance to download the files.

ubuntu@ubuntu:~$ sudo apt install filezilla

Before downloading the file from the EC2 instance to the local machine using the FTP server, make sure the file is inside the home directory on the EC2 instance.
Open the filezilla on your local system, provide the credentials and click on the ‘Quickconnect’ to connect to the FTP server

Go to the desired directory on the remote side, select the file you want to download, and download the file.

Conclusion

Transferring the important files from the EC2 instance to the local machine is a daily routine for a system administrator. This article explains different ways to transfer files from an EC2 instance to the local system. After going through this blog, it will be much easier for you to transfer files from the EC2 instance to the local system using different ways.

About the author

Zain Abideen

A DevOps Engineer with expertise in provisioning and managing servers on AWS and Software delivery lifecycle (SDLC) automation. I'm from Gujranwala, Pakistan and currently working as a DevOps engineer.