Fix: SSH Permission Denied Issue | Real Solution
1. Verify SSH Command and Key Usage
Use the correct private key with -i
:
ssh -i ~/.ssh/your_private_key user@host
Ensure the key is not passphrase-protected (or enter the passphrase correctly).
2. Check Key Generation and Deployment
Regenerate Keys (if unsure):
ssh-keygen -t ed25519 -C "your_email@example.com" # Prefer ed25519
Ed25519 uses elliptic curve cryptography (Edwards-curve Digital Signature Algorithm) and provides 128-bit security (equivalent to RSA-3072 or RSA-4096) with a 256-bit key.
Copy the Public Key to the Server:
ssh-copy-id -i ~/.ssh/your_public_key user@host
If ssh-copy-id
isn’t available, manually append the public key to ~/.ssh/authorized_keys
on the server.
3. Fix File/Directory Permissions
On the Server:
chmod 700 ~/.ssh chmod 600 ~/.ssh/authorized_keys chmod go-w ~ # Home directory should not be world-writable
On the Client (Local Machine):
chmod 600 ~/.ssh/your_private_key
4. Check SSH Server Configuration
Edit /etc/ssh/sshd_config
on the server:
PubkeyAuthentication yes AuthorizedKeysFile .ssh/authorized_keys PasswordAuthentication no # Optional but ensure keys work first
Restart SSH service:
sudo systemctl restart sshd
5. Inspect Logs and Debug Output
Client-Side Debugging:
ssh -vvv user@host # Look for "Offering public key" or errors
Server Logs:
tail -f /var/log/auth.log # Ubuntu/Debian tail -f /var/log/secure # CentOS/RHEL
6. Check for SELinux Issues
Temporarily disable SELinux:
setenforce 0 # Revert with setenforce 1
Restore file contexts:
restorecon -Rv ~/.ssh
7. Miscellaneous Checks
- Ensure the server has disk space (
df -h
). - Confirm
AllowUsers
orDenyUsers
in/etc/ssh/sshd_config
includes your user. - Verify the correct user owns the
.ssh
directory and files:
chown -R user:user ~/.ssh
8. Test Password Authentication (Temporarily)
Enable PasswordAuthentication yes
in /etc/ssh/sshd_config
, restart SSH, and test login:
ssh user@host # Use password to log in and fix key issues
Comments
Post a Comment