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 console 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
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
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
-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.pemfile 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: