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?


Additional info: using

ssh usrname@ -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 [] 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 '[]: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_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).
  • 4
    Do you use homedir encryption? This might prevent the SSH daemon from reading your key files if you are not logged in. – Sven Dec 11 '12 at 17:16
  • That was exactly it! To disable the homedir encryption I used this tutorial. virtually-a-machine.blogspot.com.br/2010/08/… Hey @SvenW, can you post your comment as an answer so I can accept it as a solution? – Ken Dec 12 '12 at 16:52

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.


Getting your home from a network share that uses strong authentication (kerberized NFSv4, CIFS) might cause the same issue. PAM will use your password to mount the share. No password -> no home -> no authorized_keys

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.