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.

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

Or if the password-based authentication is enabled.

[email protected]:~$ 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.

Or if the password-based authentication is enabled.

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/1-49.jpg" data-lazy- height="167" src="data:image/svg xml,” width=”732″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/2-47.jpg" data-lazy- height="81" src="data:image/svg xml,” width=”731″>

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

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/3-46.jpg" data-lazy- height="167" src="data:image/svg xml,” width=”756″>

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.

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/4-43.jpg" data-lazy- height="78" src="data:image/svg xml,” width=”642″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/5-42.jpg" data-lazy- height="235" src="data:image/svg xml,” width=”369″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/6-38.jpg" data-lazy- height="225" src="data:image/svg xml,” width=”459″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/7-37.jpg" data-lazy- height="270" src="data:image/svg xml,” width=”714″>

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.

Start and enable the vsftpd service on your EC2 instance.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/8-31.jpg" data-lazy- height="129" src="data:image/svg xml,” width=”642″>

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.

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/9-27.jpg" data-lazy- height="112" src="data:image/svg xml,” width=”642″>

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.

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/10-25.jpg" data-lazy- height="171" src="data:image/svg xml,” width=”801″>

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

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/11-21.jpg" data-lazy- height="212" src="data:image/svg xml,” width=”630″>

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

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2021/09/echo/Zain-150×150.jpg6133dc6d86f71.jpg" height="112" src="data:image/svg xml,” width=”112″>

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.