SSH remote user need to be logged in?

I'm trying to set up an SSH server. I put my public key in the authorized_keys file, made sure the permissions were correct, etc.

When I restart the server (really just Ubuntu 12.04 desktop) and I ssh to it without first logging in on the server, I am asked for a password. If, however, I log into the server, I can ssh without being asked for a password.

auth.log has these lines when I have not logged in on the actual server:

mordor sshd[1605]: debug1: trying public key file /home/buck/.ssh/authorized_keys
mordor sshd[1605]: debug1: Could not open authorized keys '/home/buck/.ssh/authorized_keys': No such file or directory

The problem usually arises if your home directory is encrypted. The usual solution is to put your keys in a directory other than your home directory, and point your sshd_config file to it.

For example:

  1. Move your authorized_keys file on the server from /home/buck/.ssh/authorized_keys to something like /etc/ssh/keys/buck/authorized_keys
  2. set the permissions on this folder and the keys file: sudo chown -R buck:buck /etc/ssh/keys/buck/ and chmod 700 /etc/ssh/keys/buck/ and chmod 600 /etc/ssh/keys/buck/authorized_keys
  3. Edit /etc/ssh/sshd_config and change the line AuthorizedKeysFile %h/.ssh/authorized_keys to AuthorizedKeysFile /etc/ssh/keys/%u/authorized_keys
  4. sudo service ssh restart and you should be able to login without having to login to the server first.