If you get this error in your logs when trying to set up public key authenticated automatic logins, or while trying to SSH into your account, after setting up the public key the issue is related to permissions.
Tailing /var/log/secure on the target machine will be useful:
tail -f /var/log/secure Dec 26 12:30:38 server sshd[3503454]: Authentication refused: bad ownership or modes for directory /home/user/.ssh
As you can see – bad ownership or modes for directory /home/user/.ssh.
SSH doesn’t like it if your home or ~/.ssh directories have group write permissions. Your home directory should be writable only by you, ~/.ssh should be 700, and authorized_keys should be 600 :
chmod go-w /home/user chmod 700 /home/user/.ssh chmod 600 /home/user/.ssh/authorized_keys
So fixing permissions is the way to go and have this error resolved.
Tired of hassling with troubleshooting? ChemiCloud is the managed hosting solution designed to save you time and money! 🤓 Check out our web hosting plans!
Can’t fucking do it… all the time the same crap message.
Did something change in a recent version of ssh? I’ve been having a bugger of a time keeping authorized keys working without it suddenly stopping from working without anything changing in the config files or directory permissions.
Thank you! Was googling for an hour before finding this.
Me too ! I haved the error Permission denied (publickey) after each tentative to log with ssh with my server and finally I have seen the following logs “bad ownership or modes for directory /home/user” with the command : $ journalctl -b -u ssh.service
And it solved the problem for me too after spending my afternoon on it today!
It means the error message od openssh is badly formed.
Thank you! chmod go-w ~/ fixed it for me
This solved my issue – after spending so much time debugging and trying out other solutions.
Thanks buddy!
awesome! this fixed my issue for root logins with keys
Thank you! chmod go-w ~/ fixed it for me after 1-2 hrs of changing .ssh/ and file permissions
This solved the issue that made me lose an afternoon yesterday.