Q: permission denied (publickey,gssapi-keyex,gssapi-with-mic) - AWS 2 days ago I was able to connect to my EC2 instance using the private_key.pem with ssh protocol but today when I tried to do the connection the terminal gives me:
permission denied (publickey,gssapi-keyex,gssapi-with-mic)
I removed the user called jhon and all the home of that user, then I created again and do the same steps for create the public and private key:


*adduser jhon2 | su jhon2

*cd /home/jhon2/

*ssh-keygen -t rsa -m PEM (all the questions ENTER by default)

*cd .ssh/ | cat id_rsa.pub > authorized_keys

*chmod 600 authorized_keys

-- I create a newest .pem file called jhon2.pem which contains the id_rsa content (private_key)
And from my laptop I tried again the ssh -i /home/macos/pems/jhon2.pem jhon2@instance-id, but nothing again...

*

*The SELINUX was disabled

*The sshd service status was OK

*I can connect to root and ec2-user

*command: telnet {instance-id} 22 : OK

I don´t know what is going on :s

A: To reproduce your situation, I launched a new Amazon Linux 2 instance and did the following:
On the EC2 instance:
[ec2-user@ip-172-31-0-96 ~]$ sudo adduser jhon4
[ec2-user@ip-172-31-0-96 ~]$ sudo su jhon4
[jhon4@ip-172-31-0-96 ec2-user]$ cd /home/jhon4
[jhon4@ip-172-31-0-96 ~]$ ssh-keygen -t rsa -m PEM
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jhon4/.ssh/id_rsa): 
Created directory '/home/jhon4/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/jhon4/.ssh/id_rsa.
Your public key has been saved in /home/jhon4/.ssh/id_rsa.pub.
[jhon4@ip-172-31-0-96 ~]$ cd .ssh
[jhon4@ip-172-31-0-96 .ssh]$ cat id_rsa.pub >authorized_keys
[jhon4@ip-172-31-0-96 .ssh]$ chmod 600 authorized_keys 
[jhon4@ip-172-31-0-96 .ssh]$ cat id_rsa
-----BEGIN RSA PRIVATE KEY-----
...
-----END RSA PRIVATE KEY-----

I then copied the private key to my local computer and used:
$ ssh -i jhon.pem jhon4@13.211.211.54
Last login: Tue Dec  1 09:57:15 2020

       __|  __|_  )
       _|  (     /   Amazon Linux 2 AMI
      ___|\___|___|

https://aws.amazon.com/amazon-linux-2/
[jhon4@ip-172-31-0-96 ~]$ 

So, it worked fine for me. (I had to use sudo for some of the commands.)
