ChatGPT解决这个技术问题 Extra ChatGPT

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

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.


D
David Levesque

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

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.
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.
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.
thanx @DavidLevesque
Old question but OP was asking how to learn UNIX / Linux, "a man page a day" is a good way
V
Visvendra Singh Rajpoot

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


Wordpress codex suggests 755 or 750. But 777 will do fine as well. codex.wordpress.org/Changing_File_Permissions
what if someone's destinationFolder is home ? it's not good way to give permission to home
@Beyhan Yes, I agree. Typically you copy your content to a public directory then move it to appropriate directory.
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.
G
GKV

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/


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.
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
A
Amal Murali

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


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
a
asma

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


关注公众号,不定期副业成功案例分享
Follow WeChat

Success story sharing

Want to stay one step ahead of the latest teleworks?

Subscribe Now