Issue#

You copied your public key to the server, but key login still fails and SSH falls back to a password prompt, or rejects you outright:

user@server: Permission denied (publickey).

The server's auth log shows the real reason:

sshd[2143]: Authentication refused: bad ownership or modes for directory /home/user

or

sshd[2143]: Authentication refused: bad ownership or modes for file /home/user/.ssh/authorized_keys

Cause#

OpenSSH has StrictModes yes enabled by default. Before it trusts authorized_keys, sshd checks that the file and every directory above it are owned by the user (or root) and are not writable by group or others.

This often breaks after:

  • chmod -R 775 /home/user or chmod 777 used as a "quick fix"
  • Copying .ssh as root, so the files end up owned by root
  • Restoring a home directory from a backup or tarball with the wrong UID

If another user could write to any of these paths, they could add their own key. So sshd refuses the key file.

Solution#

  1. Confirm the cause in the server logs:
# Debian/Ubuntu sudo journalctl -u ssh --since "10 min ago" | grep -i "authentication refused" # RHEL/Rocky/Fedora sudo journalctl -u sshd --since "10 min ago" | grep -i "authentication refused"
  1. Check the current ownership and modes:
namei -l /home/user/.ssh/authorized_keys
  1. Fix the ownership (replace user with the account name):
sudo chown user:user /home/user sudo chown -R user:user /home/user/.ssh
  1. Remove the group and world write bits, and set the recommended modes:
sudo chmod go-w /home/user sudo chmod 700 /home/user/.ssh sudo chmod 600 /home/user/.ssh/authorized_keys
  1. On SELinux systems (RHEL family), restore the file contexts as well. This matters if .ssh was copied or moved from another location:
sudo restorecon -Rv /home/user/.ssh

Tip: Don't set StrictModes no in sshd_config to work around this. It disables a real security check. Fix the permissions instead.

Verify#

Check that the paths now look correct. No w should appear in the group or other positions:

namei -l /home/user/.ssh/authorized_keys

Confirm that StrictModes is still active:

sudo sshd -T | grep -i strictmodes

From the client, test with key authentication only:

ssh -o PreferredAuthentications=publickey -o PasswordAuthentication=no user@server

If you still get denied, run ssh -v user@server. Make sure the client offers the key that matches a line in authorized_keys. You can compare them with ssh-keygen -lf ~/.ssh/id_ed25519.pub on the client and ssh-keygen -lf ~/.ssh/authorized_keys on the server.