
How to Copy File from EC2 to Local Machine
Copying files from an EC2 instance to your local machine is a common task for developers and system administrators. Whether you need to transfer configuration files, application data, or any other type of file, this guide will walk you through the process step by step.
Choose the Right Method
There are several methods to copy files from an EC2 instance to your local machine. The most common methods include using SSH, SCP, SFTP, and AWS CLI. Each method has its own advantages and use cases. Let’s explore them briefly:
- SSH: The Secure Shell (SSH) protocol is widely used for remote login and file transfer. It provides a secure and encrypted connection between your local machine and the EC2 instance.
- SCP: Secure Copy (SCP) is a protocol that uses SSH for secure file transfer. It is simple to use and supports file transfer over an encrypted connection.
- SFTP: Secure File Transfer Protocol (SFTP) is another protocol that provides secure file transfer capabilities. It is similar to FTP but offers more security features.
- AWS CLI: The AWS Command Line Interface (CLI) is a powerful tool that allows you to manage your AWS resources from the command line. It supports various file transfer operations, including copying files from an EC2 instance to your local machine.
For this guide, we will focus on using SSH and SCP, as they are the most commonly used methods for copying files from an EC2 instance to your local machine.
Set Up SSH Access
Before you can copy files from your EC2 instance, you need to set up SSH access. Here’s how to do it:
- Log in to your EC2 instance using the SSH client of your choice (e.g., PuTTY, OpenSSH, etc.).
- Generate an SSH key pair if you haven’t already. You can do this using the following command:
ssh-keygen -t rsa -b 4096
- Copy the public key to your EC2 instance. You can do this by appending the public key to the
~/.ssh/authorized_keys
file on the instance. You can use the following command to do this:
cat ~/.ssh/id_rsa.pub | ssh user@ec2-instance-ip 'cat >> ~/.ssh/authorized_keys'
- Configure your SSH client to use the private key. You can do this by specifying the path to the private key file in the SSH client’s configuration file (e.g.,
~/.ssh/config
for OpenSSH). For example:
Host ec2-instance-ip HostName ec2-instance-ip User user IdentityFile ~/.ssh/id_rsa
Copy Files Using SCP
Once you have set up SSH access, you can use SCP to copy files from your EC2 instance to your local machine. Here’s how to do it:
- Open a terminal on your local machine.
- Use the following SCP command to copy a file from your EC2 instance to your local machine:
scp user@ec2-instance-ip:/path/to/file /path/to/local/directory
For example, to copy a file named example.txt
from the home directory of your EC2 instance to the current directory on your local machine, you would use the following command:
scp user@ec2-instance-ip:/home/user/example.txt .
Copy Files Using SSH
Another method to copy files from your EC2 instance to your local machine is by using SSH. Here’s how to do it:
- Open a terminal on your local machine.
- Use the following SSH command to copy a file from your EC2 instance to your local machine:
ssh user@ec2-instance-ip 'cat /path/to/file' > /path/to/local/directory/file
For example, to copy a file named example.txt
from the home directory of your EC2