How To resolve the SSH AWS Permission denied (publickey) ERROR

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@
Code 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.

ec2-ubuntu@ Permission denied (publickey). lost connection
Code 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.pem
Code 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.

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 ec2-ubuntu@

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

ec2-ubuntu - Username @ - The IP address of the machine :/tmp - The directory where I want to copy the file to
Code 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.

Suddenly... With these 10 golden resources, anyone can become an IT ENGINEER [FREE]

Write your name and email now and get the list PLUS a bonus test to discover if engineering is for you [FREE]. (636 others like you have already signed up).

* indicates required

Avatar photo

By Igor Jovanovic

15+ successful years in the IT industry. During this time, I worked at some of today’s largest banks (e.g. Credit Suisse - Switzerland) and investment funds (APG, ABN AMRO - The Netherlands) where I was leading the engineering teams. In the last 4 years, I am present in the startup scene and have been working with unicorn database providers such as Redis (valuation of over $2 billion) and MarkLogic (valuation of around $1 billion). In my spare time, I am helping others start/improve their IT career by writing this blog.
LinedIn | Facebook | Instagram

Leave a Reply

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