Copy file | directory to & from EC2 using SCP (Secure copy)

Written by: Chirag (Srce Cde)


While we are working with EC2 Linux instances we might require to transfer files from the local system to the EC2 instance quickly. Obviously, there are a couple of ways to achieve this but one of the ways that can come in handy is to use the Secure copy protocol.

Secure Copy protocol allows you to transfer files securely between two hosts. It uses same underlying protocol as SSH. Assuming that you have the instance up and running and have the identity file (.pem) handy since we will need the identity file to successfully copy file/directory to & from EC2 instance. Make sure you have port 22 open.


To copy the file from local machine to EC2 instance:

  • Open local terminal
  • scp -i identity_file.pem source_file.extention username@public_ipv4_dns:/remote_path

Ex: scp -i access.pem ~/Documents/temp_file.txt ubuntu@0.0.0.0:/home/ubuntu/destination_dir

  • scp: Secure copy protocol
  • -i: Identity file
  • source_file.extension: The file that you want to copy
  • username: Username of the remote system (ubuntu for Ubuntu AMI & ec2-user for Linux AMI)
  • public_ipv4_dns: DNS/IPv4 address of an instance
  • remote_path: Destination path

To copy the file from EC2 instance to local machine:

  • Open local terminal
  • scp -i identity_file.pem username@public_ipv4_dns:/remote_path/source_file.extension ~/destination_local_path

Ex: scp -i access.pem ubuntu@0.0.0.0:/home/ubuntu/remote_path/temp.txt ~/Documents/destination_dir

To copy the directory from local machine to EC2 instance:

  • Open local terminal
  • scp -i identity_file.pem -r source_dir username@public_ipv4_dns:/remote_dir_path

Ex: scp -i access.pem -r ~/Documents/directory1 ubuntu@0.0.0.0:/home/ubuntu/

  • -r: Recursively copy entire directory

To copy the directory from EC2 instance to local machine:

  • Open local terminal
  • scp -i identity_file.pem -r username@public_ipv4_dns:/remote_dir_path destination_dir
Ex: scp -i access.pem -r ubuntu@0.0.0.0:/home/ubuntu/source_dir ~/Documents/directory1


You can optionally refer this video tutorial on the same.