Securely Transferring Files with SCP: From Local to EC2 and EC2 to Local

Securely Transferring Files with SCP: From Local to EC2 and EC2 to Local

The Secure Copy Protocol (SCP) is a powerful command-line utility that allows for the secure transfer of files and directories between hosts on a network.

SCP is built on SSH (Secure Shell) and provides a secure way to move data over the network.

In this blog, we'll guide you through the process of using SCP to:

  1. Transfer files from your local machine to an EC2 instance.

  2. Transfer files from an EC2 instance to your local machine.

Step-by-Step Guide

1. Launch an EC2 Instance

First, launch an EC2 instance in AWS and note its public IP address.

2. Configure Security Group

Make sure the security group associated with your EC2 instance allows SSH (port 22) traffic. This setting is necessary for SCP to establish a connection with the instance.

3. Set Permissions on the Key Pair File

Ensure your key pair file has the correct permissions.

On your local machine:

chmod 400 path/to/your-key-pair.pem

4. Transfer File from Local Machine to EC2 Instance

Use the SCP command to transfer a file from your local machine to the EC2 instance.

Command Syntax:

scp -i path/to/your-key-pair.pem /path/to/local/file ec2-user@ec2-public-ip:/path/to/remote/directory

Example: Suppose you have a file named example.txt in your local directory that you want to transfer to the ~/ directory on your EC2 instance.

scp -i ~/Downloads/my-key-pair.pem ~/Documents/example.txt ec2-user@3.92.183.121:~/

Replace the placeholders with your actual file paths and the public IP address of your EC2 instance.

5. Transfer File from EC2 Instance to Local Machine

To transfer a file from your EC2 instance back to your local machine, use the SCP command with the source and destination paths reversed.

Command Syntax:

scp -i path/to/your-key-pair.pem ec2-user@ec2-public-ip:/path/to/remote/file /path/to/local/directory

Example: Suppose you want to transfer a file named example.txt from the ~/ directory on your EC2 instance to your local Documents directory.

scp -i ~/Downloads/my-key-pair.pem ec2-user@3.92.183.121:~/example.txt ~/Documents/

Conclusion

Using SCP to transfer files between your local machine and an EC2 instance is simple and secure. This blog provides clear steps and screenshots to guide you through the process, ensuring smooth and secure file transfers.