Permission denied (publickey,gssapi-keyex,gssapi-with-mic) error with SSH connection to Amazon EC2 Instance

Problem: I tried accessing my EC2 instance and got the error as below:

Avkash-Machine:~ avkash$ ssh -i MyUtils/SSHAccessKey.pem ec2_user@xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

Lets try to have verbose SSH connection to see what could be the issue:

Avkash-Machine:~ avkash$ ssh -v -i MyUtils/SSHAccessKey.pem ec2_user@xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com
OpenSSH_5.9p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: /etc/ssh_config line 53: Applying options for *
debug1: Connecting to xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com [xx.xxx.xxx.xx] port 22.
debug1: Connection established.
debug1: identity file MyUtils/SSHAccessKey.pem type -1
debug1: identity file MyUtils/SSHAccessKey.pem-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 0f:ce:27:19:18:ee:40:86:df:db:f0:95:79:29:49:05
debug1: Host ‘xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com’ is known and matches the RSA host key.
debug1: Found key in /Users/avkash/.ssh/known_hosts:15
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
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: MyUtils/SSHAccessKey.pem
debug1: read PEM private key done: type RSA
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: No more authentication methods to try.
Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

Above you can see that the SSH key is valid and readable and the connection did try various auth methods however connection still failed.

As you can see below that SSH Key SSHAccessKey.pem have proper access mode configured:

Avkash-Machine:~ avkash$ ls -l MyUtils/SSHAccessKey.pem
-rwx——@ 1 avkash staff 1692 May 20 14:38 MyUtils/SSHAccessKey.pem
Now the connection is working here as shown in the following full verbose log:

Avkash-Machine:~ avkash$ ssh -v -i MyUtils/SSHAccessKey.pem ec2-user@xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com
OpenSSH_5.9p1, OpenSSL 0.9.8r 8 Feb 2011
debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: /etc/ssh_config line 53: Applying options for *
debug1: Connecting to xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com [xx.xxx.xxx.xx] port 22.
debug1: Connection established.
debug1: identity file MyUtils/SSHAccessKey.pem type -1
debug1: identity file MyUtils/SSHAccessKey.pem-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3
debug1: match: OpenSSH_5.3 pat OpenSSH*
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_5.9
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr hmac-md5 none
debug1: kex: client->server aes128-ctr hmac-md5 none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<1024<8192) sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: expecting SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: RSA 0f:ce:27:19:18:ee:40:86:df:db:f0:95:79:29:49:05
debug1: Host ‘xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com’ is known and matches the RSA host key.
debug1: Found key in /Users/avkash/.ssh/known_hosts:15
debug1: ssh_rsa_verify: signature correct
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
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: MyUtils/SSHAccessKey.pem
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).
Authenticated to xxx-xxx-xxx-xxx.us-west-2.compute.amazonaws.com ([xx.xxx.xxx.xx]:22).
debug1: channel 0: new [client-session]
debug1: Requesting no-more-sessions@openssh.com
debug1: Entering interactive session.
debug1: Sending environment.
debug1: Sending env LC_MONETARY = en_US.utf-8
debug1: Sending env LC_NUMERIC = en_US.utf-8
debug1: Sending env LC_MESSAGES = en_US.utf-8
debug1: Sending env LC_COLLATE = en_US.utf-8
debug1: Sending env LANG = ru_RU.UTF-8
debug1: Sending env LC_CTYPE = en_US.utf-8
debug1: Sending env LC_TIME = en_US.utf-8

Verification done:

  1.  I checked that my EC2 Instance have proper SSH key
  2. Verified that EC2 instance have ssh port open in firewall (Security Policy)

Solution: I was using incorrect user Id (i.e. ec2_user) instead of the correct one (i.e. ec2-user)

 

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s