wesupport

25% off on first invoice for all services*

SPRING SALE

Use coupon

*Offer valid for new customers only

25% off on first invoice for all services*

SPRING SALE

Use coupon

*Offer valid for new customers only

Need help?

Our experts have had an average response time of 11.43 minutes in March 2024 to fix urgent issues.

We will keep your servers stable, secure, and fast at all times for one fixed price.

ec2 key permission denied – “Authentication failed, permission denied”

by | May 27, 2021

We may come across the ec2 key permission denied error due to different reasons.

It can occur if we try to connect using the wrong user name for the AMI. It can also happen if the permissions are incorrect on the instance.

Another scenario is an incorrect SSH public key (.pub) file in the authorized_keys file.

As part of our AWS Support Services, we assist our customers with several such errors.

Today, let us see how we can fix it.

 

ec2 key permission denied

“Permission denied (publickey)” and “Authentication failed, permission denied”

In order to fix this error, we verify that we use the correct user name for the AMI.

Similarly, we verify that the instance’s permissions are correct and the correct SSH public key is in the authorized_keys file

To perform these tasks, there are four methods. Moving ahead, let us discuss how our Support Techs do that.

Method 1:

Use the EC2 Serial Console

It is possible to troubleshoot Nitro-based instance types if we enable EC2 Serial Console for Linux. It helps to troubleshoot boot issues, network configuration, and SSH configuration issues.

W can access it via the Amazon EC2 console or the AWS Command Line Interface (AWS CLI).

Prior to using the serial console, we grant access to it at the account level. Then we create AWS Identity and Access Management (IAM) policies granting access to the IAM users.

Also, every instance using the serial console must include at least one password-based user.

While we run the AWS CLI commands, we may confront errors. In such a case, we should make sure to use the most recent version of the AWS CLI.

Method 2:

Use AWS Systems Manager Session Manager to log in to the instance and make corrections

This method updates permissions and injects the SSH public key into the authorized_keys file.

We need to install the SSM Agent to use this method.

1. We open the AWS Systems Manager console.

2. Then we start a session.

3. The ls -ld command makes sure the permissions of the files under the home directory are correct.

For example, here, ec2-user is the user name. We can change the user name according to the specific AMI.

$ ls -ld /home/ec2-user/
drwx------ 3 ec2-user ec2-user 4096 Apr  1 08:31 /home/ec2-user/

4. Then on the local computer, we verify the SSH public key.

5. If there is no sign of the SSH public key in the output, we update the authorized_keys file to allow the SSH key.

For example, here we replace the example key with the SSH public key.

$ echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVogCW5eZogRp+vF6Ut360b0bYyTmqgYaCXOyiW77I916AS5jFL3zsCtONbGn4hnG/UGGWXpLfUV85qpVJb38fskPZNuyZtjGjXM2W7qqbCZ1N9HBb6IPBaL97tmqBi+8rD7mSkoHc40sIV+KxkQSvD6AAFjQruCjxzfGIApnOvuj6IMsVEuFHBx4QhkbCzafxo02D9BZT4+dMy7tmyuC+UiNEQpgfFoszl+4VNFTIPlQQyn6CpUiV/rFXIadXsHqc+UOdVnfEXP+30YL75RHabze/1F5MY6t94AEcmcb05Dq4vwN9IjcxKmwgvxLOXzryytepvHQU+PobBEXAMPLE' >> /home/ec2-user/.ssh/authorized_keys

6. To correct permissions, on the EC2 instance we run:

$ sudo chown root:root /home
$ sudo chmod 755 /home
$ sudo chown ec2-user:ec2-user /home/ec2-user -R
$ sudo chmod 700 /home/ec2-user /home/ec2-user/.ssh
$ sudo chmod 600 /home/ec2-user/.ssh/authorized_keys

7. Eventually, we end the session.

8. Finally, we can connect to the instance via SSH.

Method 3:

Run the AWSSupport-TroubleshootSSH Automation procedure

AWSSupport-TroubleshootSSH installs the Amazon EC2Rescue tool.

Then the tool checks for and corrects issues that cause remote connection errors while connecting to a Linux machine through SSH.

Method 4:

Use a user-data script to repair SSH permissions and add the correct SSH public key to the authorized_keys file

  • If the instance is instance store-backed or has instance store volumes containing data if we stop the instance the data is lost.
  • If the instance is part of an Amazon EC2 Auto Scaling group, it might terminate if we stop. In case the instance is part of an Auto Scaling group, we temporarily remove the instance from the Auto Scaling group before the resolution steps.
  • Stopping and starting the instance changes the public IP address of your instance. It’s a best practice to use an Elastic IP address instead of a public IP address when routing external traffic to your instance.

1. Initially, we open the Amazon EC2 console.

2. Then we choose Instances from the navigation pane, then select the instance to launch.

3. We stop the instance.

4. Later, we choose ActionsInstance SettingsEdit User Data.

5. Then we copy the following user data script into the Edit User Data dialog box and Save:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type:
    text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
chown root:root /home
chmod 755 /home
chown ec2-user:ec2-user /home/ec2-user -R
chmod 700 /home/ec2-user /home/ec2-user/.ssh
chmod 600 /home/ec2-user/.ssh/authorized_keys
--//

6. Eventually, we verify the SSH public key in the local computer.

7. If in the output, the signature of the SSH public key isn’t present, then we append the correct key to the user data script in step 5. If the signature matches, we skip this step.

For example, we append the SSH public key to the user data script:

Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0

--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"

#cloud-config
cloud_final_modules:
- [scripts-user, always]

--//
Content-Type:
    text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"

#!/bin/bash
chown root:root /home
chmod 755 /home
chmod 700 /home/ec2-user /home/ec2-user/.ssh
chmod 600 /home/ec2-user/.ssh/authorized_keys
echo 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDVogCW5eZogRp+vF6Ut360b0bYyTmqgYaCXOyiW77I916AS5jFL3zsCtONbGn4hnG/UGGWXpLfUV85qpVJb38fskPZNuyZtjGjXM2W7qqbCZ1N9HBb6IPBaL97tmqBi+8rD7mSkoHc40sIV+KxkQSvD6AAFjQruCjxzfGIApnOvuj6IMsVEuFHBx4QhkbCzafxo02D9BZT4+dMy7tmyuC+UiNEQpgfFoszl+4VNFTIPlQQyn6CpUiV/rFXIadXsHqc+UOdVnfEXP+30YL75RHabze/1F5MY6t94AEcmcb05Dq4vwN9IjcxKmwgvxLOXzryytepvHQU+PobBEXAMPLE' >> /home/ec2-user/.ssh/authorized_keys
chown ec2-user:ec2-user /home/ec2-user -R
--//

8. Then we start the instance.

The preceding user data script is set to run on every reboot of the instance. After regaining access, we remove the user data script.

To do so, we:

  • Complete steps 1-4 in this section.
  • Delete the user data script in the Edit User Data dialog box.

[Need help with the process? We can help you

 

Conclusion

In short, we saw how our Support Techs fix the error, ec2 key permission denied.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

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

Categories

Tags