83

scp -r /Applications/XAMPP/htdocs/keypairfile.pem uploads ec2-user@publicdns:/var/www/html

where uploads is a directory returns Permission denied (publickey).

However

scp -i /Applications/XAMPP/htdocs/keypairfile.pem footer.php ec2-user@publicdns:/var/www/html

works (notice the flag change).

uploads is an empty folder

These are the file permissions for the uploads directory

drwxrwxrwx 3 geoffreysangston admin 102 Nov 15 01:40 uploads

These are the file permissions for /var/www/html

drwxr-x--- 2 ec2-user ec2-user 4096 Jan 5 20:45 html

I've tried changing html to 777 and that doesn't work either.

DJo
  • 2,133
  • 4
  • 30
  • 46
user3015797
  • 847
  • 1
  • 6
  • 8

5 Answers5

179

The -i flag specifies the private key (.pem file) to use. If you don't specify that flag (as in your first command) it will use your default ssh key (usually under ~/.ssh/).

So in your first command, you are actually asking scp to upload the .pem file itself using your default ssh key. I don't think that is what you want.

Try instead with:

scp -r -i /Applications/XAMPP/htdocs/keypairfile.pem uploads/* ec2-user@publicdns:/var/www/html/uploads
David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • 2
    This now gives me "scp: /var/www/html/uploads: Permission denied" so I think I'm getting somewhere. It probably now has to do with the file permissions so I'll mess around with them some more. Thank you. I started learning about working with unix / the console just a little while ago with a jump in approach. How would you say is the best way at going about it or is it really just a learn by doing deal? Thanks. – user3015797 Jan 06 '14 at 18:27
  • Your permissions seem ok. I think the problem is with the target directory. I've edited the command in my answer (2 changes), you can try with that. As for how to learn unix/linux, I prefer "learn by doing", but it depends on people. Reading a few tutorials on the Web cannot hurt. – David Levesque Jan 06 '14 at 19:01
  • 3
    Since this was the top answer when I had a similar problem (on an RPi), I'll just add that in my case I ran it with the debug flag `vvv` and noticed that scp was trying `/root/.ssh/id_rsa` and failing. I simply added `-i /home/pi/.ssh/id_rsa` so that it was pointing to the correct user's keyfile and it worked. Debug output is almost always useful. – flith Oct 30 '17 at 11:59
46

Even if above solutions don't work, check permissions to destination file of aws ec2 instance. May be you can try with- sudo chmod 777 -R destinationFolder/*

  • 1
    Wordpress codex suggests 755 or 750. But 777 will do fine as well. https://codex.wordpress.org/Changing_File_Permissions – Gus Oct 25 '17 at 18:34
  • 3
    what if someone's destinationFolder is home ? it's not good way to give permission to home – Beyhan Gul Jun 10 '19 at 13:31
  • 1
    @Beyhan Yes, I agree. Typically you copy your content to a public directory then move it to appropriate directory. – Visvendra Singh Rajpoot Jun 12 '19 at 07:25
  • Do sudo chmod 777 -R folder/file_you_want_to_copy.whatever_extension. I did "sudo chmod 777 -R ~/" in my ec2 instance instead and ended up getting "permission denied (publickey gssapi-keyex gssapi-with-mic)" every time i tried to ssh back into my ec2 instance. I had to use the aws managment console to connect to my instance again and then did "sudo chmod 750 -R ~/" so I could ssh back into my ec2 instance again without the "permission denied(publickkey...) error above. – pgs1 May 19 '22 at 20:45
24

transferring file from local to remote host

scp -i (path of your key) (path for your file to be transferred) (username@ip):(path where file to be copied)

e.g scp -i aws.pem /home/user1/Desktop/testFile   ec2-user@someipAddress:/home/ec2-user/

P.S. - ec2-user@someipAddress of this ip address should have access to the destination folder in my case /home/ec2-user/

GKV
  • 864
  • 1
  • 12
  • 25
  • Thanks - I didn't realize I had to use the -i flag in combination with the pem file and then my source file followed by destination. – andrewcar Feb 09 '18 at 20:21
  • This worked for me. I use the -i flag to access the instance so not sure why I didn't realize I would still need to use the -i flag with key to access the instance in order to move files. Thanks – BoyArmy_89 Sep 15 '21 at 14:10
  • what should I do if I want to transfer a directory from a remote host to local? – yash7696 Jan 25 '23 at 02:33
2

If you want to upload the file /Applications/XAMPP/htdocs/keypairfile.pem to ec2-user@publicdns:/var/www/html, you can simply do:

scp -Cr /Applications/XAMPP/htdocs/keypairfile.pem/uploads/ ec2-user@publicdns:/var/www/html/

Where:

  • -C - Compress data
  • -r - Recursive
Amal Murali
  • 75,622
  • 18
  • 128
  • 150
archetipo
  • 579
  • 4
  • 10
  • The path to uploads is /Applications/XAMPP/htdocs/projectname/uploads I was just using "uploads" cause I was calling it from inside the projectname directory I tried scp -Cr /Applications/XAMPP/htdocs/keypairfile.pem /Applications/XAMPP/htdocs/projectname/uploads/ ec2-user@ec2-54-200-81-203.us-west-2.compute.amazonaws.com:/var/www/html/ that also didn't work – user3015797 Jan 06 '14 at 04:06
1

answer for newbies (like me):

I had this error when trying to copy the files while being on the server.

So my answer is: exit, or open another terminal

asma
  • 599
  • 1
  • 7
  • 19