Last updated: Feb 26, 2024
Reading time·2 min
There are 2 main reasons the "Permission denied (publickey)" error occurs when trying to SSH into an AWS EC2 instance:
To solve the error when trying to SSH into an EC2 instance:
chmod 600 ec2-private-key.pem
Actions
and select Connect
. Click on the SSH client
tab
and copy the ssh command example.ec2-user
. However, the connection string in the console is not always correct. If you are launching an Ubuntu instance, the username will be Ubuntu
, for bitnami instances it is bitnami
.An easy way to 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
ec2-user
.Therefore our SSH command should look as follows:
ssh -i "ec2-private-key.pem" ec2-user@YOUR_EC2_PUBLIC_DNS
Once I updated the username to ec2-user
, the connection was established
successfully:
If you are still getting the error after correcting the username, run the ssh command in verbose mode:
ssh -v -i "ec2-private-key.pem" ec2-user@YOUR_EC2_PUBLIC_DNS
The -v
flag, stands for verbose and logs the ssh
command's output to the
terminal. The flag is very helpful for debugging.
ec2-private-key.pem
file is located. Otherwise, you might get a permission denied error because the file could not be found.I've also written a detailed guide on how to provision and configure an EC2 instance in AWS CDK.
You can learn more about the related topics by checking out the following tutorials: