Categories
Blog Tech

How To resolve the SSH AWS Permission denied (publickey) ERROR2 min read

You are probably here because you are getting frustrated by the error you are getting from your SSH client.

Earlier today, I was in your shoes. I needed to copy the Redis Enterprise Software package from my local computer to an Ubuntu-based AWS EC2 instance.

Here is the command that uses the scp (Secure Copy) package to copy your file over the internet securely.

scp -i "igor-jov-a-a.pem"  redislabs-6.2.18-65-bionic-amd64.tar ec2-ubuntu@172.31.0.194:/tmpCode language: JavaScript (javascript)

💡Most Unix-based machines, such as macOS, and Ubuntu, already have scp installed. In Windows, you must download an SSH client that supports scp such as PuTTY. (To learn more about the secure connection, click here).

To my surprise, the command returned this error.

[email protected]: Permission denied (publickey).

lost connectionCode language: HTTP (http)

Here are the usual suspects you should control to fix the Permission denied (publickey) ERROR.

Make sure your .pem file has the needed permissions

Enter the command below in your terminal to make your private key file visible enough.

chmod 600 igor-jov-a-a.pemCode language: CSS (css)

Control if your .pem file has the right path

I have changed the directory to the folder that contains my .pem file. That is why my path of the .pem file looks like this.

"igor-jov-a-a.pem"Code language: JSON / JSON with Comments (json)

If you are using a mac and your .pem file is in the /Downloads directory, you can easily change the directory by using this code.

cd /Users/<YOUR_MAC_USERNAME>/Downloads Code language: HTML, XML (xml)
See if the remote server has the right username & IP

In my case, this is the remote AWS EC2 instance [email protected]:/tmp.

If you break it down, you will see several factors.

ec2-ubuntu - Username
@172.31.0.194 - The IP address of the machine 
:/tmp - The directory where I want to copy the file toCode language: CSS (css)

Control each of these carefully. In my case, this was the catch. My username was ubuntu rather than ec2-ubuntu.

Nevertheless, the machine will give you the Permission denied (publickey) ERROR even though your .pem file is 100% correct (but in this case, it is created for a different username).

I wish you an error-free day in the Terminal. Enjoy coding.

P.S. If you want to learn more about AWS, read my AWS For Beginners post here.


Avatar photo

By Igor Jovanovic

Founder & Full Stack Tech Consultant

Experienced tech professional with a strong track record in web services and fintech. Collaborating with Silicon Valley's multi-billion tech giants and offering a range of services committed to excellence. Check the Services page for how I can help you too.

Leave a Reply

Your email address will not be published. Required fields are marked *