Linux SSH Permission Denied (publickey) Fix
In this tutorial, you'll learn about Linux SSH Permission Denied (publickey) Fix. We cover key concepts, practical examples, and best practices.
SSH returns "Permission denied (publickey)" when the server rejects your key. This happens when key permissions are too open, the wrong key is used, the authorized_keys file is misconfigured, or SELinux/AppArmor blocks SSH key access.
The Problem
ssh user@server
Error:
user@server: Permission denied (publickey).
With verbose output:
ssh -vvv user@server
Shows:
debug1: Authentications that can continue: publickey
debug1: Offering public key: /home/user/.ssh/id_rsa
debug1: Server accepts key
debug1: Authentication succeeded (publickey).
But then:
debug1: reading /home/user/.ssh/authorized_keys
debug1: Authentication refused: bad permissions
Wrong Approach
# WRONG — setting loose permissions on the entire ~/.ssh
chmod 777 ~/.ssh
chmod 777 ~/.ssh/authorized_keys
Right Approach
# Correct permissions for SSH
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
Expected output:
$ ssh user@server
Welcome to Ubuntu 22.04 LTS (GNU/Linux 5.15.0-86-generic x86_64)
user@server:~$
Step-by-Step Fix
Step 1: Fix local SSH directory permissions
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub
Step 2: Fix server authorized_keys permissions
ssh user@server "chmod 700 ~/.ssh && chmod 600 ~/.ssh/authorized_keys"
Or if you can log in with a password:
ssh user@server
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Step 3: Verify the key was added to authorized_keys
ssh user@server "cat ~/.ssh/authorized_keys"
Step 4: Check the SSH config
grep -i "pubkeyauthentication" /etc/ssh/sshd_config
Expected:
PubkeyAuthentication yes
Step 5: Test with verbose output
ssh -vvv user@server 2>&1 | grep -i "authenticated\|permission\|key"
Step 6: Fix SELinux context for custom SSH directories
sudo restorecon -Rv ~/.ssh
Prevention Tips
- Always run
ssh-copy-id user@serverinstead of manually copying keys - Keep strict permissions on
~/.ssh(700) and its files (600) - Use
ssh-keygen -t ed25519for modern, secure keys - Never share private keys between servers
- Add keys to the SSH agent with
ssh-addfor convenience
Common Mistakes with ssh permission denied
- Overlapping type class instances that cause GHC to reject the program with ambiguous dispatch errors
- Non-exhaustive pattern matches that compile with warnings then crash at runtime
- Misunderstanding that
Stringis[Char]with poor performance for large text operations
These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro