Sat Sep 25 2021·2 min read
Photo by Kalen Emsley
There are 2 main reasons the "Permission denied (publickey)" error occurs when trying to SSH into an AWS EC2 instance:
To solve the "Permission denied (publickey)" error when trying to SSH into an EC2 instance:
chmod 600 ec2-private-key.pem
Connect. Click on the
SSH clienttab and copy the ssh command example.
ec2-user. However the connection string in the example is not always correct. If you are launching an ubuntu instance the username will be
ubuntu, for bitnami instances it is
An easy way for find the username for your AMI is to try to log in as root and read the error message:
ssh -i "ec2-private-key.pem" root@YOUR_EC2_PUBLIC_DNS
The error message shows the correct username we should be specifying when trying
to ssh into the EC2 instance - in this case
Therefore our ssh command should look like:
ssh -i "ec2-private-key.pem" ec2-user@YOUR_EC2_PUBLIC_DNS
Once I updated the username to be
ec2-user the connection was established
If you are still getting the error even after correcting the username, run the ssh command in verbose mode:
ssh -v -i "ec2-private-key.pem" ec2-user@YOUR_EC2_PUBLIC_DNS
-v flag, stands for verbose and logs the
ssh command's output to the
terminal and is very helpful for debugging.
ec2-private-key.pemfile is located, otherwise you might get a permission denied error, because the file could not be found.
I'll send you 1 email a week with links to all of the articles I've written that week