Permission denied (publickey) means you successfully connected to the server over the network, but the server rejected your authentication. You are past the firewall. The SSH daemon is running. The problem is specifically that the server refused to accept your key.
This is different from Connection refused (which means SSH is not running or a firewall is blocking port 22) and different from Connection timed out (which means the server is unreachable). If you are getting permission denied, the network is fine — it is the authentication step that failed.
Here is how to find and fix the actual cause.
What the server is checking
Before getting to specific fixes, it helps to know exactly what the server verifies when you connect with a key:
- Does
~/.ssh/authorized_keysexist on the server for your user? - Does the file contain a line that matches your public key?
- Is the file permissions on
~/.ssh/set to700(not group or world writable)? - Is the file permissions on
~/.ssh/authorized_keysset to600(not group or world writable)? - Is the private key you are presenting the correct pair for one of those public keys?
The server silently ignores your authorized_keys file if any permission check fails, and falls back to password auth (or rejects entirely if password auth is disabled). This is the most common source of confusion: the file exists with the right key, but wrong permissions means the server never reads it.
Step 1: Run SSH with verbose output
Before doing anything else, add -vvv to your command:
ssh -vvv user@your-server
This prints every step of the connection attempt. Look for lines like:
debug1: Offering public key: /home/you/.ssh/id_ed25519 ED25519
debug1: Authentications that can continue: publickey
debug1: No more authentication methods to try.
If it says "Offering public key" but then moves on without success, the server received your key and rejected it. Keep reading.
If it says "No identities found" or does not offer any key at all, your SSH agent is not loaded or it is pointing to the wrong key file. Skip to Step 3.
Step 2: Check authorized_keys on the server
If you have another way to access the server (console access, password login from another machine, cloud web console), log in and check:
# Does the file exist?
ls -la ~/.ssh/
# What's in it?
cat ~/.ssh/authorized_keys
# Check permissions
stat ~/.ssh/
stat ~/.ssh/authorized_keys
The permissions need to be:
~/.ssh/ → 700 (drwx------)
authorized_keys → 600 (-rw-------)
If they are wrong, fix them:
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
Also verify the directory is owned by your user, not root:
ls -la ~ | grep .ssh
If .ssh is owned by root, that is the problem:
sudo chown -R youruser:youruser ~/.ssh
Step 3: Verify you are using the right key
Your local machine may have multiple keys. Check which ones are available:
ls ~/.ssh/
ssh-add -l
The ssh-add -l command shows what keys are loaded in your SSH agent. If the list is empty or does not include the key you want, add it:
ssh-add ~/.ssh/id_ed25519
Or specify the key file directly when connecting:
ssh -i ~/.ssh/my-specific-key.pem user@server
Now check the server's authorized_keys to confirm your public key is actually there. The public key should look like:
ssh-ed25519 AAAAC3Nz... your-comment
Compare the key in authorized_keys on the server to the contents of your local ~/.ssh/id_ed25519.pub. They need to match exactly, including the algorithm prefix. A common mistake: the private key and public key files were separated (copied one without the other, or generated on a different machine), so the pair does not match.
Step 4: Check that the public key was actually added to the server
The most common scenario for new servers: you generated a key pair and forgot to copy the public key to the server.
If you still have password access or console access:
ssh-copy-id user@server
This connects, asks for your password, and appends your local public key to ~/.ssh/authorized_keys on the server. Then try again with your key.
If ssh-copy-id is not available, do it manually:
cat ~/.ssh/id_ed25519.pub | ssh user@server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys && chmod 600 ~/.ssh/authorized_keys"
Step 5: AWS EC2-specific issues
AWS has several quirks that cause permission denied errors.
Wrong username. Each AMI uses a different default username. Using root or ubuntu on Amazon Linux (where the user is ec2-user) gives permission denied:
| AMI | Username |
|---|---|
| Amazon Linux 2 / 2023 | ec2-user |
| Ubuntu | ubuntu |
| Debian | admin |
| CentOS / RHEL | ec2-user or centos |
| Fedora | fedora |
Key pair mismatch. EC2 instances are configured at launch with one specific key pair. If you try to connect with a different key, it will never work regardless of what is in authorized_keys. The original key pair is set by EC2 at launch; you cannot change it through the console after the fact.
PEM key format. AWS gives you a .pem file. This is the private key in PEM format, directly usable with OpenSSH. Do not convert it unless you are using PuTTY (which needs .ppk). If you are using SSHDock, paste the .pem contents directly into the private key field.
New instance, wrong region. Verify the instance is in the region you are looking at in the console.
Step 6: Check if password authentication is disabled
If password auth is disabled on the server (which is the correct hardening step) and your key is not being accepted, you have no fallback. The error message looks the same: Permission denied (publickey).
Check the server config:
sudo grep -i passwordauthentication /etc/ssh/sshd_config
If it says no, you need key auth to work. Go back through steps 1-4 to find why the key is being rejected.
If you locked yourself out and have console access (most cloud providers offer a browser-based emergency console), you can temporarily re-enable password auth to fix things:
sudo sed -i 's/PasswordAuthentication no/PasswordAuthentication yes/' /etc/ssh/sshd_config
sudo systemctl reload sshd
Then connect with your password, fix the key setup, and re-disable password auth.
Step 7: SELinux and AppArmor
On RHEL, CentOS, Fedora, and Amazon Linux systems, SELinux can block SSH from reading the authorized_keys file even when file permissions look correct.
Check if SELinux is the issue:
sudo ausearch -m avc -ts today | grep ssh
Or check the audit log directly:
sudo tail -f /var/log/audit/audit.log | grep denied
If SELinux is denying access, restore the correct file context:
restorecon -Rv ~/.ssh
This is a common issue on fresh CentOS or Amazon Linux instances where the .ssh directory was created by copying files rather than through ssh-keygen or ssh-copy-id (which set the correct SELinux context automatically).
Step 8: Check sshd logs on the server
The server logs the exact reason for auth failures, which is much more informative than what the client prints. On the server:
sudo tail -f /var/log/auth.log # Ubuntu/Debian
sudo tail -f /var/log/secure # RHEL/CentOS/Amazon Linux
sudo journalctl -u sshd -f # Systemd systems
Watch the logs while making a connection attempt. You will see entries like:
Authentication refused: bad ownership or modes for directory /home/user/.ssh
or
error: Could not load host key: /etc/ssh/ssh_host_rsa_key
The server-side log almost always tells you exactly what failed.
Quick diagnostic checklist
If you want to run through everything fast:
# On your local machine:
ssh -vvv user@server 2>&1 | grep -E "(Offering|denied|refused|try)"
# Key available in agent?
ssh-add -l
# Using the right key?
ssh -i ~/.ssh/correct-key.pem user@server
# On the server (if you have access):
ls -la ~/.ssh/
stat ~/.ssh/authorized_keys
cat ~/.ssh/authorized_keys | grep "$(cat ~/.ssh/id_ed25519.pub | awk '{print $2}')"
That last command checks whether your public key is actually present in authorized_keys.
Using SSHDock
If you are connecting through SSHDock, paste the full contents of your private key file (starting with -----BEGIN ... PRIVATE KEY----- and ending with -----END ... PRIVATE KEY-----) into the private key field. If the key has a passphrase, enter it in the passphrase field. The most common mistake is pasting the public key (.pub file) instead of the private key — the private key file has no .pub extension and begins with -----BEGIN.
For more on setting up SSH key authentication from scratch, see our SSH key authentication guide.