Interview Prep/Linux/SSH, sudo and authentication

`ssh deploy@server01` fails with `deploy@server01: Permission denied (publickey).`

BeginnerTroubleshootingSSH, sudo and authentication
Answer

Likely causes → The client offers the wrong key, the public key isn't in the right authorized_keys, or permissions on ~, ~/.ssh or authorized_keys are too open so sshd ignores them.

Diagnose →

ssh -v deploy@server01 2>&1 | grep -E 'Offering|Authentications|identity file'
# on the server (console or another account)
sudo ls -ld /home/deploy /home/deploy/.ssh /home/deploy/.ssh/authorized_keys
sudo journalctl -u sshd -n 30 | grep -i deploy

Fix →

sudo chmod go-w /home/deploy
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys
sudo chown -R deploy:deploy /home/deploy/.ssh

Use the correct key: ssh -i ~/.ssh/deploy_ed25519 deploy@server01.

Prevent → Create keys with automation (Ansible authorized_key) which sets modes correctly.

All Linux questions Roadmap: Linux

More on SSH, sudo and authentication