Ubuntu – When user in server is not logged in ssh gives: Permission denied (public key)

public-keysshUbuntu

I'm setting up a ssh server using public key authentication. For that I used this tutorial: http://www.g-loaded.eu/2005/11/10/ssh-with-keys/

I was able to login via ssh. But this works only if I keep the user logged into the server (meaning I have to type my username and password on the server machine and keep it logged in). If I logout the user in the server, when I try to login through ssh it gives: "Permission denied (public key)".

My question is… is there really a need to keep the user logged in the server to use ssh with public key authetntication? Or maybe I've done something wrong?

Thanks!

Additional info:
using

ssh usrname@10.0.1.200 -v

I get the following output (when there is no user logged in to the server).

debug1: Reading configuration data /etc/ssh_config
debug1: /etc/ssh_config line 20: Applying options for *
debug1: Connecting to 10.0.1.200 [10.0.1.200] port 22.
debug1: Connection established.
debug1: identity file /Users/thecommodore/.ssh/id_rsa type -1
debug1: identity file /Users/thecommodore/.ssh/id_rsa-cert type -1
debug1: identity file /Users/thecommodore/.ssh/id_dsa type -1
debug1: identity file /Users/thecommodore/.ssh/id_dsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.9p1 Debian-5ubuntu1
debug1: match: OpenSSH_5.9p1 Debian-5ubuntu1 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: DSA 2e:ca:e6:66:d1:61:35:7c:98:bb:cb:1f:49:aa:24:81
debug1: Host '[10.0.1.200]:22' is known and matches the DSA host key.
debug1: Found key in /Users/thecommodore/.ssh/known_hosts:7
debug1: ssh_dss_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 DSA public key: /Users/thecommodore/.ssh/id_dsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/thecommodore/.ssh/id_rsa
debug1: Trying private key: /Users/thecommodore/.ssh/id_dsa
debug1: key_parse_private_pem: PEM_read_PrivateKey failed
debug1: read PEM private key done: type <unknown>
debug1: read PEM private key done: type DSA
Identity added: /Users/thecommodore/.ssh/id_dsa (/Users/thecommodore/.ssh/id_dsa)
debug1: read PEM private key done: type DSA
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
Permission denied (publickey).

Best Answer

This is likely the result of an encrypted home directory. This prevents the SSH daemon from reading the key files and will cause key-based login to fail. You have basically two options to mitigate this problem:

  • Disable home directory encryption. See this tutorial (provided by the OP).
  • Configure the SSH daemon to read the key files from another, unencrypted, location.

For the second option, you could create a directory /sshkeys/ with subdirectories for every user. Then set the AuthorizedKeysFile parameter in /etc/ssh/sshd_config to /sshkeys/%u/authorized_keys and place the files in the appropriate subdirectories and make sure that every user has read/execute rights for /sshkeys and that the permissions for the user specific directory belongs to the respective user and is set to permissions 700

Another option, which is more advanced, would be to use the AuthorizedKeysCommand parameter and write a lookup-script for the key, e.g. from an LDAP directory. This would help with the NFS4/Kerberos problem outlined by Thorsten as well. This would require that you allow your users to put the key into the relevant LDAP field, e.g. with some additional helper scripts.