26

How to use scp command to upload file to aws server

  • I have .pem file in /Downloads in local machine
  • I am trying to copy file to /images folder in AWS server

    What command can i use ?

Thanks,

6 Answers 6

48

You can use plain scp:

scp -i ~/Downloads/file.pem local_image_file user@ec2_elastic_ip:/home/user/images/

You need to put an Elastic IP to the EC2 instance, open port 22 to your local machine IP in the EC2 instance security group, and use the right user (it can be ec2-user, admin or ubuntu (look at the AMI documentation)).

5

Diego's answer works.. However, if you're unaware of your elastic IP, then you can simply scp using following command (check the order of arguments)

scp -i path-to-your-identifier.pem file-to-be-copied ubuntu@public-IP:/required-path

just for reference, here ubuntu is your AWS user and public-IP is somewhat like 54.2xx.xxx.xxx e.g. 54.200.100.100 or such (If order is messed up: filename before identifier, then you'll get a Permission denied (publickey).lost connection error)

Also, keep in mind the permissions of .pem file.. Should be 400 or 600. Not public to all. Hope it helps!

0
3

there are number of ways to achieve what you want

  1. use s3cmd http://s3tools.org/s3cmd

  2. or use cyberduck http://cyberduck.ch/

  3. or write a tool using amazon Java API

3
  • Anything like cyberduck for Linux ?
    – user2609157
    Aug 11, 2013 at 6:51
  • 1
    not that I have used, i prefer s3cmd, i just googled and this one came up - dragondisk.com , looks like they have stopped developing it Aug 11, 2013 at 6:55
  • Coincidentally, I just wrote up a blog post about this a couple of days ago. blog.ryanparman.com/2013/08/10/… Aug 12, 2013 at 7:43
3

This is the correct way uploading from local to remote.

scp -i "zeus_aws.pem" ~/Downloads/retail_market_db-07-01-2021.sql ub[email protected]:/var/www/

Could be a better approach

3

You can try kitten utility which is a wrapper around boto3. You can easily upload/download files and run commands on EC2 server or on multiple servers at once for that matter.

kitten put -i ~/.ssh/key.pem cat.jpg /tmp [SERVER NAME][SERVER IP]

Where server name is e.g ubuntu or ec2-user etc.

This will upload cat.jpg file to /tmp directory of server

1

Another alternative way to scp is rsync.

Some of the benefits of rsync

The rsync cmd

rsync -ravze "ssh -i /home/your-user/your-key.pem "  --exclude '.env'  --exclude '.git/' /var/www/your-folder-to-upload/*  [email protected]:/var/www/your-remote-folder

Now, in case you find this syntax a little bit verbose you can use aws-upload which does all the above but you just tabbing.

3
  • 2
    rsync is also recommended when you need to transfer huge data (say more than couple gig) because it does support "resume" with no additional effort (just add --partial argument to keep partially transferred files)! You can Ctrl-C operation at any time (to emulate connection loss) and run rsync again - it will resume (compare to scp which will start all over again). Also you can use --bwlimit to trafficshape the transfer (if you don't want to take all available bandwidth and leave say 50% to your wife for some onging series;)). Jul 5, 2017 at 17:07
  • Thanks, @DmitryShevkoplyas . I've updated the answer. For the 'resume' option shouldn't be --append --append-verify? see here. Jul 6, 2017 at 20:47
  • Nah, I never used --append nor --append-verify and interrupted the transfer so many times just to make sure next attempt will not start all over again and it perfectly worked, but now, since you asked, I found this answer and I'm no longer 100% sure if it is ok to use it without any --appendXXX key(s): unix.stackexchange.com/a/165417/227338 Here's "the meat" of my script: rsync --partial --progress ${BWLIMIT} --rsh=ssh $1 $2 (and it works great for last 10+ years : ) Jul 7, 2017 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.