'MAC OS and AWS ec2 Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

Yesterday, I created new aws ece instance and i was working properly. However, today morning, i got a error messeage when i tried to connect aws ec2. (Permission denied (publickey,gssapi-keyex,gssapi-with-mic)

for your information, i am using mac os.

I was create ec2-intance with amazon-linux2. then I tried as follow.

ssh -i xxx.pem [email protected]

I also tried to enter follow code.

chmod 400 xxx.pem

Please see below debug log.

OpenSSH_8.1p1, LibreSSL 2.7.3
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 47: Applying options for *
debug1: Connecting to 54.180.146.106 [54.180.146.106] port 22.
debug1: Connection established.
debug1: identity file /Users/user1/.ssh/id_rsa type -1
debug1: identity file /Users/user1/.ssh/id_rsa-cert type -1
debug1: identity file /Users/user1/.ssh/id_dsa type -1
debug1: identity file /Users/user1/.ssh/id_dsa-cert type -1
debug1: identity file /Users/user1/.ssh/id_ecdsa type -1
debug1: identity file /Users/user1/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/user1/.ssh/id_ed25519 type -1
debug1: identity file /Users/user1/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/user1/.ssh/id_xmss type -1
debug1: identity file /Users/user1/.ssh/id_xmss-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_8.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_7.4
debug1: match: OpenSSH_7.4 pat OpenSSH_7.0*,OpenSSH_7.1*,OpenSSH_7.2*,OpenSSH_7.3*,OpenSSH_7.4*,OpenSSH_7.5*,OpenSSH_7.6*,OpenSSH_7.7* compat 0x04000002
debug1: Authenticating to 54.180.146.106:22 as 'ec2-user'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256
debug1: kex: host key algorithm: ecdsa-sha2-nistp256
debug1: kex: server->client cipher: [email protected] MAC: <implicit> compression: none
debug1: kex: client->server cipher: [email protected] MAC: <implicit> compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ecdsa-sha2-nistp256 SHA256:+b+E64s1bsV8cVtNGxqWFVQKDuo/+SuT88N4hQOcmMs
debug1: Host '54.180.146.106' is known and matches the ECDSA host key.
debug1: Found key in /Users/user1/.ssh/known_hosts:45
debug1: rekey out after 134217728 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey in after 134217728 blocks
debug1: Will attempt key: /Users/user1/.ssh/id_rsa 
debug1: Will attempt key: /Users/user1/.ssh/id_dsa 
debug1: Will attempt key: /Users/user1/.ssh/id_ecdsa 
debug1: Will attempt key: /Users/user1/.ssh/id_ed25519 
debug1: Will attempt key: /Users/user1/.ssh/id_xmss 
debug1: SSH2_MSG_EXT_INFO received
debug1: kex_input_ext_info: server-sig-algs=<rsa-sha2-256,rsa-sha2-512>
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Trying private key: /Users/user1/.ssh/id_rsa
debug1: Trying private key: /Users/user1/.ssh/id_dsa
debug1: Trying private key: /Users/user1/.ssh/id_ecdsa
debug1: Trying private key: /Users/user1/.ssh/id_ed25519
debug1: Trying private key: /Users/user1/.ssh/id_xmss
debug1: No more authentication methods to try.
[email protected]: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

Please kindly look my..error and I really appreciate if you guys help me. :)

Thanks.



Solution 1:[1]

Are you sure it's the right key? Can you try the same key from AWS Web Console and see if the key works?

Reference document

Solution 2:[2]

I have experienced the same issue trying to log in to AWS instance using my Mac terminal.

There are two options, that I have tried and they should work after much research:

  • Change the permissions: Instead of chmod 400 <my-key-pair.pem>, use chmod 600 .pem, then try again using ssh -i <my-key-pair.pem> ec2-user@public-ip

  • The second option is to generate the hash of the public key from a , you can create a new one. ssh-keygen -y -f /path_to_key_pair/new-key-pair.pem

  1. use cloud-config to add the public key to the instance as user data, after shutting down the instance; change the username to your username and the public key to the one generated by the ssh-keygen command>. Be sure to enter the entire public key, starting with ssh-rsa.

Below is the Cloud-config command.

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: ?- [users-groups, once] ?users: ?  - name: username ?    ssh-authorized-keys: ?    - PublicKeypair

  1. Save
  2. Start your instance.
  3. After the cloud-init phase is complete, validate that the public key was replaced. Important: Because the script contains a key pair, remove the script from the User Data field.
  4. Stop your instance.
  5. Choose Actions, Instance Settings, Edit user data.
  6. Delete all the text in the Edit user data dialog box, and then choose Save.
  7. Start your instance. Login using the new key pair - ssh -I <new_key_pair> username@public_ip  

Solution 3:[3]

I think this should solve your problem.

I assume your belongs to column to country is country_id in cities table:

$res = City::query()->select("*",
            \DB::raw('(SELECT name FROM countries WHERE countries.id = cities.country_id) as countryname'))
            ->orderBy('countryname', 'DESC')
            ->paginate($records_per_page, ['*'], 'page', $page_number);

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Jay
Solution 2
Solution 3 Behzad