Bobcares

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 *

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

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF