There are 2 main reasons the “Permission denied (publickey)” error occurs when trying to SSH into an AWS EC2 instance:
- The username in the ssh connection URL is incorrect. The username is different for the different Amazon Machine Images.
- The permissions of the private key are incorrect.
To solve the “Permission denied (publickey)” error when trying to SSH into an EC2 instance:
1. Open your terminal in the directory where your private key is located and change its permissions to only be readable by the current user.
chmod 400 /path_to_downloaded_key-pair.pem
2. In the AWS EC2 console, click on the checkbox next to your instance’s name, then click on Actions
and select Connect
. Click on the SSH client
tab and copy the ssh command example.
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
YOUR_EC2_PUBLIC_DNS from SSH Command
Reference: Click here