Fix: SSH Permission denied (publickey,password)
Quick Answer
How to fix SSH permission denied publickey password error caused by wrong SSH key, incorrect server config, disabled password auth, wrong username, and file permission issues.
The Error
You try to SSH into a server and get:
Permission denied (publickey,password).Or variations:
Permission denied (publickey).Permission denied (publickey,gssapi-keyex,gssapi-with-mic).user@server: Permission denied (publickey,password).The SSH server rejected your authentication. None of the authentication methods you offered (public key, password, or others) were accepted.
Why This Happens
SSH authentication works in stages. The client offers credentials (key or password), and the server checks them. When all methods fail, the server returns “Permission denied” with the list of methods it supports.
Common causes:
- Wrong SSH key. The key you are offering does not match any authorized key on the server.
- Wrong username. You are connecting as the wrong user.
- SSH key not added to the agent. Your key exists but the SSH agent is not using it.
- Incorrect file permissions. The
~/.sshdirectory or key files have wrong permissions. - Password authentication disabled. The server only accepts key-based authentication.
- Key not authorized on the server. Your public key is not in
~/.ssh/authorized_keys. - Server configuration issue.
sshd_configrestricts which users or keys can connect.
Fix 1: Check the Username
The most overlooked fix. Make sure you are connecting as the correct user:
ssh ubuntu@server.example.com # Ubuntu EC2
ssh ec2-user@server.example.com # Amazon Linux
ssh root@server.example.com # Root (if allowed)
ssh admin@server.example.com # Debian
ssh myuser@server.example.com # Custom userCommon default users by platform:
| Platform | Default User |
|---|---|
| Ubuntu EC2 | ubuntu |
| Amazon Linux | ec2-user |
| Debian | admin |
| CentOS/RHEL | centos or ec2-user |
| Fedora | fedora |
| SUSE | ec2-user |
| macOS | Your macOS username |
| DigitalOcean | root |
If you SSH without specifying a user (ssh server.example.com), SSH uses your local username, which might not exist on the server.
Fix 2: Specify the Correct SSH Key
If you have multiple SSH keys, specify which one to use:
ssh -i ~/.ssh/my-key.pem user@server.example.comCheck which keys are available:
ls -la ~/.ssh/Common key files:
id_rsa/id_rsa.pub— RSA key pairid_ed25519/id_ed25519.pub— Ed25519 key pair (recommended)*.pem— AWS EC2 key pairs
Check which key SSH is trying to use:
ssh -v user@server.example.comLook for lines like:
debug1: Offering public key: /home/user/.ssh/id_ed25519
debug1: Server accepts key: /home/user/.ssh/id_ed25519Or:
debug1: Trying private key: /home/user/.ssh/id_rsa
debug1: No more authentication methods to try.If none of your keys are accepted, the correct key is not being offered.
Pro Tip: Use
~/.ssh/configto map hostnames to keys:Host myserver HostName server.example.com User ubuntu IdentityFile ~/.ssh/my-key.pemThen simply:
ssh myserver
Fix 3: Add Your Key to the SSH Agent
The SSH agent manages your keys. If your key is not loaded, SSH does not offer it:
# Start the agent (if not running)
eval "$(ssh-agent -s)"
# Add your key
ssh-add ~/.ssh/id_ed25519
# List loaded keys
ssh-add -lFor AWS .pem keys:
chmod 400 ~/.ssh/my-key.pem
ssh-add ~/.ssh/my-key.pemmacOS Keychain:
ssh-add --apple-use-keychain ~/.ssh/id_ed25519Add to ~/.ssh/config to persist across reboots:
Host *
AddKeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_ed25519Fix 4: Fix File Permissions
SSH is strict about permissions. Wrong permissions cause silent authentication failure:
# Fix directory permissions
chmod 700 ~/.ssh
# Fix private key permissions
chmod 600 ~/.ssh/id_ed25519
chmod 600 ~/.ssh/id_rsa
chmod 400 ~/.ssh/*.pem
# Fix public key permissions
chmod 644 ~/.ssh/id_ed25519.pub
# Fix authorized_keys permissions
chmod 600 ~/.ssh/authorized_keys
# Fix ownership
chown -R $(whoami):$(whoami) ~/.sshOn the server side too:
chmod 700 /home/user/.ssh
chmod 600 /home/user/.ssh/authorized_keys
chown -R user:user /home/user/.sshIf the .ssh directory or key files are world-readable, SSH refuses to use them. This is a security feature.
Common Mistake: Setting permissions to
777or666on SSH files. SSH intentionally rejects keys with overly permissive permissions. Private keys must be600(owner read/write only) and the.sshdirectory must be700.
Fix 5: Add Your Public Key to the Server
Your public key must be in ~/.ssh/authorized_keys on the server:
Copy your public key to the server:
ssh-copy-id user@server.example.comIf ssh-copy-id is not available or password auth is disabled, copy manually:
# On your local machine
cat ~/.ssh/id_ed25519.pub
# Copy the output
# On the server (via console access)
mkdir -p ~/.ssh
echo "ssh-ed25519 AAAA... your@email.com" >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
chmod 700 ~/.sshFor cloud instances: Upload your public key through the cloud provider’s console:
- AWS: Key pairs in EC2 console
- GCP: Metadata → SSH Keys
- Azure: Reset password/SSH key in VM settings
- DigitalOcean: Settings → Security → SSH Keys
Fix 6: Check Server SSH Configuration
The server’s /etc/ssh/sshd_config controls authentication methods:
# On the server
sudo grep -E "PasswordAuthentication|PubkeyAuthentication|PermitRootLogin|AuthorizedKeysFile" /etc/ssh/sshd_configEnable public key authentication:
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keysEnable password authentication (if needed):
PasswordAuthentication yesAllow root login (not recommended for production):
PermitRootLogin yesAfter changes, restart SSH:
sudo systemctl restart sshdWarning: Be careful editing sshd_config while connected via SSH. A misconfiguration can lock you out. Always keep a second SSH session or console access open while testing changes.
Fix 7: Generate a New SSH Key Pair
If you do not have an SSH key or need a new one:
ssh-keygen -t ed25519 -C "your@email.com"Follow the prompts. The key pair is saved to ~/.ssh/id_ed25519 (private) and ~/.ssh/id_ed25519.pub (public).
For systems that require RSA:
ssh-keygen -t rsa -b 4096 -C "your@email.com"Then add the public key to the server (Fix 5).
For Git-specific SSH key issues, see Fix: git permission denied publickey.
Fix 8: Debug SSH Connection
Use verbose mode to see exactly what happens during connection:
ssh -vvv user@server.example.comKey things to look for in the output:
debug1: Authentications that can continue: publickey,passwordThis shows which methods the server accepts.
debug1: Offering public key: /home/user/.ssh/id_ed25519
debug1: Authentications that can continue: publickey,passwordThe key was offered but not accepted — wrong key or not in authorized_keys.
debug1: No more authentication methods to try.All methods exhausted — none worked.
Check the server logs:
sudo journalctl -u sshd -f
# or
sudo tail -f /var/log/auth.logServer logs show why authentication failed (wrong key, permission issues, user not allowed).
Still Not Working?
Check for SELinux. On RHEL/CentOS, SELinux can block SSH even with correct permissions:
restorecon -R ~/.sshCheck for firewall rules. If you cannot connect at all, it might be a network issue rather than authentication. See Fix: SSH connection timed out.
Check for AllowUsers/AllowGroups. sshd_config might restrict which users can SSH:
AllowUsers ubuntu admin
AllowGroups ssh-usersIf your user is not in the list, authentication fails even with correct credentials.
Check for PAM issues. PAM (Pluggable Authentication Modules) might impose additional restrictions:
grep -r "pam" /etc/ssh/sshd_configCheck for fail2ban. If you tried too many failed logins, fail2ban might have banned your IP:
sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip YOUR_IPTry from a different network. Some networks (corporate, public WiFi) block SSH port 22. Try connecting on port 443 if the server supports it, or use a VPN.
For permission denied errors in bash scripts (not SSH), see Fix: bash permission denied.
Solo developer based in Japan. Every solution is cross-referenced with official documentation and tested before publishing.
Was this article helpful?
Related Articles
Fix: SSH Connection Timed Out or Connection Refused
How to fix SSH errors like 'Connection timed out', 'Connection refused', or 'No route to host' when connecting to remote servers.
Fix: Cannot Connect to the Docker Daemon. Is the Docker Daemon Running?
How to fix the 'Cannot connect to the Docker daemon' error on Linux, macOS, and Windows, including Docker Desktop, systemctl, WSL2, and Docker context issues.
Fix: bash: command not found
How to fix bash command not found error caused by missing PATH, uninstalled packages, wrong shell, typos, missing aliases, and broken symlinks on Linux and macOS.
Fix: Nginx 504 Gateway Timeout
How to fix the Nginx 504 Gateway Timeout error by tuning proxy timeout settings, fixing unresponsive upstream servers, adjusting PHP-FPM timeouts, and debugging with error logs.