Files exchanging between AWS EC2 and your local machine

Sikandar
2 min readFeb 5, 2018

--

Copying files from local to EC2

  • Your private key must not be publicly visible. Run the following command so that only the root user can read the file.
    chmod 400 yourPublicKeyFile.pem
  • To copy files between your computer and your instance you can use an FTP service like FileZilla or the command scp. “scp” means “secure copy”, which can copy files between computers on a network. You can use this tool in a Terminal on a Unix/Linux/Mac system.
  • To use scp with a key pair use the following command:

scp -i /directory/to/abc.pem /your/local/file/to/copy user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:path/to/file

Ref: Screenshot from terminal
  • You need to specify the correct Linux user. From Amazon:
    For Amazon Linux, the user name is ec2-user.
    For RHEL, the user name is ec2-user or root.
    For Ubuntu, the user name is ubuntu or root.
    For Centos, the user name is centos.
    For Fedora, the user name is ec2-user.
    For SUSE, the user name is ec2-user or root.
    Otherwise, if ec2-user and root don’t work, check with your AMI provider.
  • To use it without a key pair, just omit the flag -i and type in the password of the user when prompted.

Note: You need to make sure that the user “user” has the permission to write in the target directory. In this example, if ~/path/to/file was created by user “user”, it should be fine.

Copying files from EC2 to local

  • To use scp with a key pair use the following command:

scp -i /directory/to/abc.pem user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:path/to/file /your/local/directory/files/to/download

Reference: Screenshot from terminal

#Hack 1: While downloading file from EC2, download folder by archiving it.
zip -r squash.zip /your/ec2/directory/

#Hack 2 : You can download all archived files from ec2 to just by below command.

scp -i /directory/to/abc.pem user@ec2-xx-xx-xxx-xxx.compute-1.amazonaws.com:~/* /your/local/directory/files/to/download

Happy exploration.

--

--