SCP Permission denied (publickey). on EC2 only when using -r flag on directories

UnixAmazon Ec2File PermissionsScp

Unix Problem Overview


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.

Unix Solutions


Solution 1 - Unix

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

Solution 2 - Unix

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/*

Solution 3 - Unix

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/

Solution 4 - Unix

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

Solution 5 - Unix

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser3015797View Question on Stackoverflow
Solution 1 - UnixDavid LevesqueView Answer on Stackoverflow
Solution 2 - UnixVisvendra Singh RajpootView Answer on Stackoverflow
Solution 3 - UnixGKVView Answer on Stackoverflow
Solution 4 - UnixarchetipoView Answer on Stackoverflow
Solution 5 - UnixasmaView Answer on Stackoverflow