You’ve just spun up a fresh VPS on the best cloud storage provider, fired up your terminal, typed SSH user@server, and… Permission denied (publickey). That “SSH authentication failed” message hits harder than realizing your VPN premium subscription just expired. But before you start Googling “laptop best for sysadmin” out of frustration—take a breath. This isn’t rocket science. We’ve fixed this exact issue hundreds of times, and by the end of this guide, you’ll know exactly how to troubleshoot and fix it—no magic required.
Let’s get one thing straight: SSH is stubborn by design. It won’t let you in unless everything lines up perfectly. And 90% of the time? The problem isn’t the server—it’s something small you overlooked. Here’s what typically goes wrong:
- Wrong key: You generated a key, but it’s not the one loaded in your agent.
- Permissions too open: Your ~/.SSH directory or private key has group/world read permissions.
- server mismatch: The public key on the server got overwritten or moved.
- Config file chaos: Your ~/.SSH/config has typos, wrong Host entries, or incorrect paths.
- Agent neglect: SSH-agent isn’t running, or it doesn’t have your key loaded.
- Firewall blocking: UFW/iptables is silently dropping port 22.
Fun fact: We once spent two hours debugging an “authentication failed” error only to realize we were trying to SSH into a Raspberry Pi running Windows IoT Core. Yeah. Check your target first.
Before diving into debug logs, run these quick checks:
- Username correct? Double-check
user@host. Is it root? ubuntu? admin? Some hosting best providers use non-standard default users. - Host reachable? Run
ping server_ip. If it fails, your DNS might be misconfigured—or worse, the host is down. - Port open?
$ nc -zv server_ip 22Connection to server_ip 22 port [tcp/SSH] succeeded!
If not, check if SSH is listening on a non-standard port (like 2222). Some providers do this to reduce bot attacks.
- Avoid copy-paste disasters:
“I once copied my public key but accidentally included an extra space at the end. Took me 30 minutes to spot it.” — A very tired DevOps engineer
Use-vfor verbose output:$ SSH -v user@server_ipLook for lines like:
debug1: Offering public key: /home/user/.SSH/id_rsa RSA SHA256:abc123 explicitdebug1: server refused our key
This tells you exactly which key was rejected.
If you’re locked out completely, try this as a last resort:
$ SSH -o PreferredAuthentications=Password -o PubkeyAuthentication=no user@server_ip(You’ll need sudo access on the server to allow Password auth temporarily.)
A word of caution: This works—but don’t leave PasswordAuthentication enabled long-term. Bots scan port 22 24/7, trying common passwords like “admin:admin”. Use it just long enough to fix your keys, then disable it again in “Permission denied”? Check these first: $ chmod 600 ~/.SSH/* # For private keys $ chmod 644 ~/.SSH/*.pub # For public keys $ chown $USER:$USER ~/.SSH -R /etc/SSH/sshd_config. .SSH directory permissions:$ chmod 700 ~/.SSH
$ sudo chmod 700 /home/user/.SSH $ sudo chmod 600 /home/user/.SSH/authorized_keys
$ sudo chown user:user /home/user/.SSH -R
The golden rule? Private keys should be readable only by you—no group or world permissions allowed. SSH enforces this strictly for security reasons. <tr} Learn more about 7 Fixes for VPN Keeps Disconnecting. For a similar approach, check out SSH WebSocket tutorial for Beginners. <tr} } table (Yes, even one wrong permission can break SSH.)}</p} <h2}3. SSH-agent Is Your Friend—Use It Right}</h2} "I’m sure I added my key!" Well… maybe SSH-agent disagrees.} SSH-agent holds your keys in memory so you don’t have to type passphrases repeatedly—but only if you tell it what to do.} Here’s how to manage it properly:</p} eval $(SSH-agent) # Add your private key (default location) SSH-add ~/.SSH/id_rsa # Add a non-default key explicitly SSH-add /path/to/custom_key # List currently loaded keys SSH-add -l # Remove all loaded keys (use with care) SSH-add -D </pre} (Pick one depending on whether you use Bash or Zsh.)}</blockquote} <pre}eval $(keychain --eval --quiet id_rsa) # Using keychain helper eval $(gpg-connect-agent updatestartuptty /bye > /dev/null) # GPG agent integration } </pre} Pro tip from our team}: If you're managing multiple servers across different cloud storage providers—, consider using separate identities per account.} Create config entries like: <pre}<Host aws-server-* } For more on this, see SSH Connection Refused fix. You can also find free accounts at Step-by-Step HTTP injector setup guide. User ubuntu } IdentityFile ~/.SSH/aws_key } Host digitalocean-server-* } User admin } IdentityFile ~/.SSH/do_key } </pre}} <h2}4. Rebuild authorized_keys—Because Sometimes It Just Disappears}</h2} <p}You’ve checked permissions—. You’ve verified keys—. But SSH still says "authentication failed." The next step? Rebuild authorized_keys manually.} This happens surprisingly often—, especially after system upgrades or accidental deletions.} Here’s how we fix it every time:</p} <pre}}# On YOUR local machine: cat ~/.SSH/id_rsa.pub # Copy this output # On THE server: mkdir -p ~/.SSH # Create.SSH if missing echo "your_public_key_here" >> ~/.SSH/authorized_keys chmod 700 ~/.SSH # Critical! chmod 644 ~/.SSH/authorized_keys } </pre} <h3}}Avoid These Common authorized_keys Mistakes}}</h3} {h{{{ h{{{ h{{{{ h{{ {h{{ h{{{ Keep Calm & Debug Verbosely }} Still stuck? Time for maximum verbosity:. Use these debug commands when nothing else works}}}: {Pre}< SSH-VVV user@server ip — Triple verbose mode shows negotiation details }} {Pre}< SUDO tail-F {var/log/auth.log == View real-time authentication logs on Ubuntu }} {Pre}< journalctL-U SSHD == Same purpose-on SYStemd-based systems-Red Hat/Fedora CentOS }} }} Here What Look For In Logs}}: If this server is full, RouterSSH Hysteria2 is another option with 6,500+ servers across 196 countries.File/Directory Permission (octal) Command Example }/home/user/.SSH/authorized_keys*644 (rw-r--r--) } }$ chmod 644 authorized_keys* }/home/user/.SSH/id_rsa*600 (rw-------) } }$ chmod 600 id_rsa* }/home/user/.SSH/known_hosts*644 (rw-r--r--) } }$ chmod 644 known_hosts*.SSH directory itself* }700 (drwx------) } }$ chmod 700 ~/.SSH*# Start SSH-agent and set environment variables
}}If SSH-agent still gives you trouble—, force-load keys at login by adding this line to your shell profile (.bashrc},.zshrc}):
Warning}: Never edit authorized_keys directly with nano/vim unless absolutely necessary.} Use echo >> instead—-it prevents accidental format corruption that breaks SSH completely.} Trust us on this one.} </blockquote}}
