The Error

bash
Permission denied (publickey).

The server accepts only public key authentication, and your key isn't being accepted.

Check If Your Key Is Being Offered

Run SSH with verbose output to see which keys are being tried:

bash
ssh -v user@server

Look for lines like:

bash
debug1: Offering public key: /home/you/.ssh/id_rsa
debug1: Authentications that can continue: publickey
debug1: Trying private key: /home/you/.ssh/id_ed25519

If no key is offered, SSH isn't finding your key file. Specify it explicitly:

bash
ssh -i ~/.ssh/my_key user@server

Verify the Public Key Is on the Server

On the server, check that your public key exists in the authorized_keys file:

bash
cat ~/.ssh/authorized_keys

Your public key should be on a single line, starting with ssh-rsa, ssh-ed25519, or ecdsa-sha2-.

To add your key:

```bash # On your local machine, copy your public key cat ~/.ssh/id_rsa.pub

# On the server, add it to authorized_keys echo "ssh-rsa AAAA...your-key-here... user@host" >> ~/.ssh/authorized_keys ```

Or use ssh-copy-id:

bash
ssh-copy-id -i ~/.ssh/id_rsa.pub user@server

Fix Permission Issues

Wrong permissions are the most common cause. SSH is strict about this.

On the server:

bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

On your local machine:

bash
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa

Also check ownership:

bash
# On the server
ls -la ~/.ssh/
# Should show your user as owner
chown -R $USER:$USER ~/.ssh

Check sshd Configuration

On the server, verify /etc/ssh/sshd_config allows public key auth:

bash
PubkeyAuthentication yes
AuthorizedKeysFile .ssh/authorized_keys

If you change the config, restart sshd:

bash
sudo systemctl restart sshd

Strict Modes

If sshd has StrictModes yes (the default), it will reject keys if permissions are too open.

Check the logs for clues:

bash
sudo tail -f /var/log/auth.log
# Or on RHEL/CentOS
sudo journalctl -u sshd -f

Test with Verbose Logging

On the server, enable debug logging temporarily:

bash
sudo /usr/sbin/sshd -d -p 2222

Then connect to port 2222:

bash
ssh -p 2222 user@server

The server will print detailed debug info to help identify the issue.