Ubuntu – ‘ssh localhost’ gives ‘Permission denied (publickey).’

12.04localhostssh

I'll mention some of many things I tried and give a couple of diagnostics. I would appreciate any clues on how to debug this:

me@myubuntu:~/.ssh$ ssh localhost
Permission denied (publickey).

Previously I ran

sudo ufw allow ssh/tcp

and set up port forwarding for port 22 in my router, and verified that is open here (but maybe not necessary — does ssh even go out as far as the router when I ssh to myself?).

In my home/.ssh directory I did cat id_rsa.pub >> authorized_keys and set up the permissions with chmod 600 authorized_keys.

Running ssh -v localhost gives lots of output, and here is the last few lines where I think the problem happens:

debug1: Host 'localhost' is known and matches the ECDSA host key.
debug1: Found key in /home/me/.ssh/known_hosts:3
debug1: ssh_ecdsa_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
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/me/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Offering RSA public key: me@myubuntu
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/me/.ssh/id_dsa
debug1: Trying private key: /home/me/.ssh/id_ecdsa
debug1: No more authentication methods to try.
Permission denied (publickey).

I succeeded in ssh-ing to my bitbucket repository, so I think this implies that my id_rsa key set is valid and has the correct permissions.

Update 1:

> ~/.ssh$ ls -l ~/.ssh/authorized_keys
-rw------- 1 me me 747 Oct 25 08:27 /home/me/.ssh/authorized_keys

(looks good)

Update 2:

~/.ssh$ ls -ld ~/.ssh
drwx------ 2 me me 4096 Oct 25 08:27 /home/me/.ssh

(looks good)

Update 3: Last few lines of /var/log/auth.log right after I run ssh localhost:

Oct 25 09:49:47 me-myubuntu sshd[16442]: Connection from 127.0.0.1 port 36944
Oct 25 09:49:47 me-myubuntu sshd[16442]: User me from localhost not allowed because not listed in AllowUsers
Oct 25 09:49:47 me-myubuntu sshd[16442]: input_userauth_request: invalid user me [preauth]
Oct 25 09:49:47 me-myubuntu sshd[16442]: Connection closed by 127.0.0.1 [preauth]

Well look at that! I previously messed with the sshd_config file and forgot to correctly specify my AllowUsers option.

SOLUTION: Include localhost in the AllowUsers line in sudo gedit /etc/ssh/sshd_config

Best Answer

Please, check /var/log/auth.log for possible errors during connection. This line

Oct 25 09:49:47 me-myubuntu sshd[16442]: User me from localhost not allowed because not listed in AllowUsers

indicates the problem.