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/userorchmod 777used as a "quick fix"- Copying
.sshas root, so the files end up owned byroot - 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#
- 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"
- Check the current ownership and modes:
namei -l /home/user/.ssh/authorized_keys
- Fix the ownership (replace
userwith the account name):
sudo chown user:user /home/user
sudo chown -R user:user /home/user/.ssh
- 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
- On SELinux systems (RHEL family), restore the file contexts as well. This matters if
.sshwas copied or moved from another location:
sudo restorecon -Rv /home/user/.ssh
Tip: Don't set
StrictModes noinsshd_configto 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.