Short answer

“Permission denied (publickey)” means the server refused every key your computer offered, and it accepts no passwords. The client never says why; the server does. Run sudo journalctl -u ssh -n 50 --no-pager -o cat | grep <user> on the server (or ask someone with access): the line names the cause, such as Invalid user or bad ownership or modes.

Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04, every step is the same. Everything that differed

Step 1: what the server allows, and where it writes the reason

The message has two halves. Permission denied means the login failed, and (publickey) is the list of methods the server still accepts: keys only. Ubuntu’s cloud images turn password login off, so there is no password prompt to fall back on. Check it on the server (from the provider’s web console if SSH is your only way in):

Server
$ sudo sshd -T | grep -E '^(pubkeyauthentication|passwordauthentication|strictmodes|authorizedkeysfile) 'pubkeyauthentication yespasswordauthentication nostrictmodes yesauthorizedkeysfile .ssh/authorized_keys .ssh/authorized_keys2
Output hidden (4 lines)

authorizedkeysfile is where sshd looks for your public key, in the home folder of the user you log in as: .ssh/authorized_keys. strictmodes yes means it refuses that file when the permissions are unsafe (cause 4).

In the test, the client and the server are the same machine: the sudo user deploy logs in to the account webadmin on localhost with the key ~/.ssh/web_key. Your own computer prints the same lines. Each cause below was created on purpose, then found and fixed.

Cause 1: the wrong user name

Client
$ ssh -i ~/.ssh/web_key admin@localhostadmin@localhost: Permission denied (publickey).
Output hidden (1 line)
Server
$ sudo journalctl -u ssh -n 50 --no-pager -o cat | grep 'Invalid user'Invalid user admin from ::1 port 33812
Output hidden (1 line)

The client gets the same message as for any other cause, but the server says it plainly: there is no user admin. Use the account the key was added to. Cloud images name it after the system (ubuntu on Ubuntu, root on some providers); on a server you set up, it is the user you made.

Cause 2: your key is never offered

Without -i, ssh offers only keys with the default names (id_ed25519, id_rsa …). A key with its own name stays unused:

Client
$ ssh webadmin@localhostwebadmin@localhost: Permission denied (publickey).
Output hidden (1 line)
Client
$ ssh -v webadmin@localhost 2>&1 | grep -E 'Will attempt key|Offering public key|Authentications that can continue'debug1: Authentications that can continue: publickeydebug1: Will attempt key: /home/deploy/.ssh/id_rsadebug1: Will attempt key: /home/deploy/.ssh/id_ecdsadebug1: Will attempt key: /home/deploy/.ssh/id_ecdsa_skdebug1: Will attempt key: /home/deploy/.ssh/id_ed25519debug1: Will attempt key: /home/deploy/.ssh/id_ed25519_sk
Output hidden (6 lines)

-v shows what the client did: it would try the default names, none of them exists, and no line says Offering public key. Name the key with -i:

Client
$ ssh -i ~/.ssh/web_key webadmin@localhost whoamiwebadmin
Output hidden (1 line)

So you never have to type -i again, give the server a short name in ~/.ssh/config on your computer. IdentitiesOnly yes makes ssh offer only that key, which also avoids “Too many authentication failures” when you have many keys:

Client
$ tee ~/.ssh/config > /dev/null <<'EOF'Host web    HostName localhost    User webadmin    IdentityFile ~/.ssh/web_key    IdentitiesOnly yesEOF
Client
$ ssh web whoamiwebadmin
Output hidden (1 line)

Cause 3: the server does not have this public key

This time the client offers its key, and the server says no. You made a new key, or added the public key to another user or another server:

Client
$ ssh -v -i ~/.ssh/old_key webadmin@localhost 2>&1 | grep -E 'Offering public key|Authentications that can continue|Permission denied'debug1: Authentications that can continue: publickeydebug1: Offering public key: /home/deploy/.ssh/old_key ED25519 SHA256:+GVARSoaNdkPxNdNFoEYiMY0ZxcUagHvMreoVV2683Q explicitdebug1: Authentications that can continue: publickeywebadmin@localhost: Permission denied (publickey).
Output hidden (4 lines)
Server
$ sudo journalctl -u ssh -n 50 --no-pager -o cat | grep webadmin | tail -n 2pam_unix(sshd:session): session closed for user webadminConnection closed by authenticating user webadmin ::1 port 37606 [preauth]
Output hidden (2 lines)

Offering public key followed by Authentications that can continue means the key was sent and refused. The server’s line only says the client gave up. To be sure it is the wrong key, compare fingerprints: yours, and the ones in the user’s authorized_keys:

Client
$ ssh-keygen -lf ~/.ssh/old_key.pub256 SHA256:+GVARSoaNdkPxNdNFoEYiMY0ZxcUagHvMreoVV2683Q deploy@web-server (ED25519)
Output hidden (1 line)
Server
$ sudo ssh-keygen -lf /home/webadmin/.ssh/authorized_keys256 SHA256:S3INwHif+ZgvyEwgKKbQfFV2MZPQDEyr6x1YA/bb2/Q deploy@web-server (ED25519)
Output hidden (1 line)

Different fingerprints: the server does not know this key. Add the public key (the .pub file, never the private one) to the end of the file. Here both ends are one machine; on two machines, copy the one line from your .pub file and append it the same way:

Server
$ cat ~/.ssh/old_key.pub | sudo tee -a /home/webadmin/.ssh/authorized_keysssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAINQCZg6V4sp9fEiGYZ2Pfuce/E2YvBJ/75nMRiFB9a31 deploy@web-server
Output hidden (1 line)
Client
$ ssh -i ~/.ssh/old_key webadmin@localhost whoamiwebadmin
Output hidden (1 line)

tee -a adds a line and keeps the keys already there. The file can stay owned by root: sshd accepts files owned by the user or by root, as long as no one else may write to them.

Cause 4: others may write to the home folder or authorized_keys

sshd refuses an authorized_keys file that another user could change, because they could add their own key. That covers the file and every folder above it. On Debian and Ubuntu one case is allowed: write access for the user’s own private group, which holds only that user. So 775 on the home folder still works:

Server
$ sudo chmod 775 /home/webadmin
Client
$ ssh web whoamiwebadmin
Output hidden (1 line)

A home folder anyone may write to (777, sometimes set while “fixing” a permissions problem) locks the key out:

Server
$ sudo chmod 777 /home/webadmin
Client
$ ssh web whoamiwebadmin@localhost: Permission denied (publickey).
Output hidden (1 line)
Server
$ sudo journalctl -u ssh -n 50 --no-pager -o cat | grep webadmin | tail -n 2Authentication refused: bad ownership or modes for directory /home/webadminConnection closed by authenticating user webadmin ::1 port 37648 [preauth]
Output hidden (2 lines)
Server
$ sudo ls -ld /home/webadmin /home/webadmin/.ssh /home/webadmin/.ssh/authorized_keysdrwxrwxrwx 4 webadmin webadmin 4096 Nov 19 17:06 /home/webadmindrwx------ 2 webadmin webadmin 4096 Nov 19 17:06 /home/webadmin/.ssh-rw------- 1 webadmin webadmin  198 Nov 19 17:07 /home/webadmin/.ssh/authorized_keys
Output hidden (3 lines)

The log names the folder. Set it back to Ubuntu’s default for home folders, 750:

Server
$ sudo chmod 750 /home/webadmin

The same happens when the file itself may be written by anyone (666):

Client
$ ssh web whoamiwebadmin@localhost: Permission denied (publickey).
Output hidden (1 line)
Server
$ sudo journalctl -u ssh -n 50 --no-pager -o cat | grep webadmin | tail -n 2Authentication refused: bad ownership or modes for file /home/webadmin/.ssh/authorized_keysConnection closed by authenticating user webadmin ::1 port 58370 [preauth]
Output hidden (2 lines)

The safe modes are 700 for .ssh and 600 for authorized_keys:

Server
$ sudo chmod 700 /home/webadmin/.ssh && sudo chmod 600 /home/webadmin/.ssh/authorized_keys
Client
$ ssh web whoamiwebadmin
Output hidden (1 line)

Cause 5: “UNPROTECTED PRIVATE KEY FILE” on your computer

This one is on the client. The private key was copied or unpacked with permissions that let others read it (644 here), so ssh will not use it:

Client
$ ssh web whoami@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@         WARNING: UNPROTECTED PRIVATE KEY FILE!          @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@Permissions 0644 for '/home/deploy/.ssh/web_key' are too open.It is required that your private key files are NOT accessible by others.This private key will be ignored.Load key "/home/deploy/.ssh/web_key": bad permissionswebadmin@localhost: Permission denied (publickey).
Output hidden (8 lines)
Client
$ chmod 600 ~/.ssh/web_key
Client
$ ssh web whoamiwebadmin
Output hidden (1 line)

Cause 6: AllowUsers leaves the user out

Client
$ ssh web whoamiwebadmin@localhost: Permission denied (publickey).
Output hidden (1 line)
Server
$ sudo journalctl -u ssh -n 50 --no-pager -o cat | grep webadmin | tail -n 2User webadmin from ::1 not allowed because not listed in AllowUsersConnection closed by invalid user webadmin ::1 port 58392 [preauth]
Output hidden (2 lines)

An AllowUsers line lets in only the users it names; on the test server an earlier change allowed only deploy and root. The line is often in a file under sshd_config.d/, not in sshd_config itself, so search the whole folder:

Server
$ sudo grep -Rn AllowUsers /etc/ssh//etc/ssh/sshd_config.d/50-allow.conf:1:AllowUsers deploy root
Output hidden (1 line)
Server
$ sudo sed -i 's/^AllowUsers .*/& webadmin/' /etc/ssh/sshd_config.d/50-allow.conf
Server
$ sudo sshd -t && sudo systemctl reload ssh
Client
$ ssh web whoamiwebadmin
Output hidden (1 line)

sshd -t prints nothing when the config is valid. The test then removed its 50-allow.conf file again.

Troubleshooting

  • Invalid user … in the journal: wrong user name. Details.
  • No Offering public key line in ssh -v: the key was never sent; use -i or a Host entry. Details.
  • Offering public key and then Authentications that can continue: the server does not have that public key, or refused the file. Read the server’s journal line. Details.
  • bad ownership or modes for directory or for file: others may write there. Details.
  • Permissions 0644 … are too open: chmod 600 the private key on your computer. Details.
  • not listed in AllowUsers: add the user to the line, test with sshd -t, reload. Details.

What these steps changed on the server

Both runs recorded the server before the first step and after the last (see the Tested on box below): the test account and its key, the second key in its authorized_keys, and the modes set back to the safe ones.

Ubuntu 26.04 vs 24.04

The same check ran on a fresh 26.04 server (OpenSSH 10.2) and on a fresh 24.04 server (OpenSSH 9.6), each with its own run log. Every cause printed the same client message and the same journal line on both. One difference showed in ssh -v:

WhatUbuntu 26.04Ubuntu 24.04
SystemUbuntu 26.04.1 LTS7.0.0-43-genericUbuntu 24.04.5 LTS6.8.0-147-generic
openssh-server1:10.2p1-2ubuntu3.81:9.6p1-3ubuntu13.20
openssh-client1:10.2p1-2ubuntu3.81:9.6p1-3ubuntu13.20
Default key names the client tries (ssh -v)id_rsa, id_ecdsa, id_ecdsa_sk, id_ed25519, id_ed25519_skthe same, plus id_xmss and id_dsa
ProofRun log, 38 checks passedRun log, 38 checks passed