- Newest
- Most votes
- Most comments
Hello.
Permission denied (publickey).
Check Key Permissions Ensure your private key file has the correct permissions.
chmod 400 path-to-your-key.pem
https://repost.aws/knowledge-center/ec2-linux-ssh-troubleshooting
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/TroubleshootingInstancesConnecting.html
Using -o PubkeyAuthentication=no literally tells it not to let you with an SSH key, so remove that from test.
You say newly created key. Did you create the EC2 instance after creating the key and specify the new key? The SSH key is only injected into the instance once when it is created (on first boot). Instances are not updated to use new keys that you create later.
Are you sure about the username for the image. I see you are using ubuntu which is the default user for Ubuntu published images. Just to double check, most other images use the user ec2-user. Make sure you are trying the connection with the correct usernam.
Hope some of this helps!
Oh... are you sure saying that: "The SSH key is only injected into the instance once when it is created (on first boot). Instances are not updated to use new keys that you create later.The SSH key is only injected into the instance once when it is created (on first boot). Instances are not updated to use new keys that you create later."
I believe you are not correct above. Granted, I have no rich experience using AWS but I have created Keypairs a couple of times after I started my instance. Only thing I had to concern was to make sure the instances had a inbound rule for the port 22 with my ip addresses.
I did use them with no trouble at all without doing anything. Can anyone please confirm if iBehr is right?
https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/replacing-key-pair.html
You can change the key on the instance if you choose, but it is not re-injected when you add keypairs in the console.
To add or replace a key pair
- Create a new key pair using the Amazon EC2 console or a third-party tool.
- Retrieve the public key from your new key pair. For more information, see Retrieve the public key material.
- Connect to your instance using your existing private key.
- Using a text editor of your choice, open the .ssh/authorized_keys file on the instance. Paste the public key information from your new key pair underneath the existing public key information. Save the file.
- Disconnect from your instance, and test that you can connect to your instance using the new private key file.
- (Optional) If you're replacing an existing key pair, connect to your instance and delete the public key information for the original key pair from the .ssh/authorized_keys file.
Relevant content
- asked 3 years ago
- asked 2 years ago

Yes, this is what I have done probably a dozen times. But it did not make any change. I meant it in the item 3 above. My ".pem" file shows the permission as follows:
-r-------- 1 fubar002 fubar002 1678 Sep 24 16:36 /home/fubar002/FooGen20240924.pem