Short answer
On your computer, run ssh-keygen -t ed25519, then ssh-copy-id deploy@your-server-ip, and log in with the key. On an Ubuntu 26.04 or 24.04 cloud server, password login is usually turned on in 50-cloud-init.conf, which sshd reads first, so editing sshd_config does nothing. Put PasswordAuthentication no in /etc/ssh/sshd_config.d/00-keys-only.conf and run sudo systemctl reload ssh.
Tested on Ubuntu 26.04 and 24.04, two separate runs. On 24.04? One section differs, each step marked “On Ubuntu 24.04” right under it: Why editing sshd_config does nothing here. Everything that differed
Before you start
- An Ubuntu 26.04 or 24.04 server you can log in to with a password, as a sudo user. Here the user is
deploy. - A computer with the OpenSSH client: Linux and macOS have it. This answer was run from an Ubuntu 24.04 computer.
- Your server’s IP address. The commands below use
203.0.113.10, an address kept for examples. Type yours instead.
Step 1: Make a key on your computer
Run this on your own computer, not on the server. Press Enter to keep the file name. Then type a passphrase twice: it protects the key if the file is ever copied. The passphrase is not shown as you type.
$ ssh-keygen -t ed25519Generating public/private ed25519 key pair.Enter file in which to save the key (/home/you/.ssh/id_ed25519):Created directory '/home/you/.ssh'.Enter passphrase (empty for no passphrase):Enter same passphrase again:Your identification has been saved in /home/you/.ssh/id_ed25519Your public key has been saved in /home/you/.ssh/id_ed25519.pubThe key fingerprint is:SHA256:Qsz50/WtZJ/a9SENzjsD9cUny1YOR8KxBDlY7SOkAZI you@laptopThe key's randomart image is:+--[ED25519 256]--+| .... oo=o. || oEo o +.+..|| = +.o.+ || . . ....o=.*|| . S . .+=O+|| . . .oo*+o|| .=.+o|| o= +|| oo..|+----[SHA256]-----+This makes two files. id_ed25519 is the private key: it never leaves your computer. id_ed25519.pub is the public key: that is the one you copy to servers. ssh-keygen sets the private key so only you can read it:
$ ls -l ~/.ssh/total 8-rw------- 1 you you 444 Nov 18 21:52 id_ed25519-rw-r--r-- 1 you you 92 Nov 18 21:52 id_ed25519.pubStep 2: Copy the public key to the server
ssh-copy-id logs in with your password one last time and adds the public key to ~/.ssh/authorized_keys on the server. The first time, ssh asks whether you trust the server’s host key: type yes. Then type the server password of deploy.
$ ssh-copy-id deploy@203.0.113.10/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/you/.ssh/id_ed25519.pub"The authenticity of host '203.0.113.10 ([172.17.0.1]:2297)' can't be established.ED25519 key fingerprint is SHA256:CnM6qqfPfWEQuR3MG8IVvzb5uZwSrCi0diQNEQufpq0.This key is not known by any other names.Are you sure you want to continue connecting (yes/no/[fingerprint])? yes/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keysdeploy@203.0.113.10's password: Number of key(s) added: 1 Now try logging into the machine, with: "ssh 'deploy@203.0.113.10'"and check to make sure that only the key(s) you wanted were added.Look for Number of key(s) added: 1. Your fingerprints will be different from the ones above: every key and every server has its own.
Step 3: Log in with the key
Now ssh asks for the passphrase of your KEY, not for the server password. That is how you know the key was used.
$ ssh deploy@203.0.113.10Enter passphrase for key '/home/you/.ssh/id_ed25519':Welcome to Ubuntu 26.04.1 LTS (GNU/Linux 7.0.0-43-generic x86_64)[… 27 lines not shown …]Last login: Wed Nov 18 21:51:33 2026 from 203.0.113.1deploy@web-server:~$ exitlogoutConnection to 172.17.0.1 closed.Step 4: See what turns password login on
Still logged in to the server, ask sshd which value it really uses. sshd -T prints the settings it would run with:
$ sudo sshd -T | grep -i "^passwordauthentication"passwordauthentication yesIt is yes. To see which file says so, search every SSH server config file:
$ sudo grep -ri "^passwordauthentication" /etc/ssh/sshd_config /etc/ssh/sshd_config.d//etc/ssh/sshd_config.d/60-cloudimg-settings.conf:PasswordAuthentication no/etc/ssh/sshd_config.d/50-cloud-init.conf:PasswordAuthentication yesTwo files disagree. 60-cloudimg-settings.conf comes with the Ubuntu cloud image and says no. 50-cloud-init.conf was written when the server was created and says yes, because the provider set up password login. sshd reads 50 before 60, and the first value it reads wins. sshd_config itself only has a commented-out line.
Step 5: Turn off password login
Add your own file whose name sorts before 50-cloud-init.conf, so sshd reads it first:
$ echo "PasswordAuthentication no" | sudo tee /etc/ssh/sshd_config.d/00-keys-only.confPasswordAuthentication noCheck the config for mistakes before you reload. No output means no errors:
$ sudo sshd -tThe running sshd still uses its old settings until you reload it (in the test, a password still worked at this point). Reload, then check again:
$ sudo systemctl reload ssh$ sudo sshd -T | grep -i "^passwordauthentication"passwordauthentication noStep 6: Check it from a new terminal
Keep your server session open. In a NEW terminal on your computer, try to log in with a password only. The server must refuse:
$ ssh -o PubkeyAuthentication=no deploy@203.0.113.10deploy@172.17.0.1: Permission denied (publickey).Permission denied (publickey). means the server now offers only one way in, a key. Then check the key still works:
$ ssh deploy@203.0.113.10 "sudo sshd -T | grep -i ^passwordauthentication"Enter passphrase for key '/home/you/.ssh/id_ed25519':passwordauthentication noIn the test, the server was then rebooted, and password login was still refused afterwards: cloud-init does not rewrite your file on a normal reboot.
Why editing sshd_config does nothing here
Most guides say to change PasswordAuthentication in /etc/ssh/sshd_config. On this server that changes nothing, even after a reload:
$ sudo sed -i 's/^#PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config && sudo systemctl reload ssh$ sudo sshd -T | grep -i "^passwordauthentication"passwordauthentication yes- Ubuntu’s
sshd_configincludes every file insshd_config.dbefore its first setting, so those files are read before the rest ofsshd_config(the command below shows it: line 24 on 26.04, line 12 on 24.04). - sshd keeps the first value it reads for each setting and ignores the later ones (see the sources in the Tested on box).
- cloud-init writes
PasswordAuthentication yesinto50-cloud-init.confwhen the provider turns password login on.
$ grep -n "^Include" /etc/ssh/sshd_config24:Include /etc/ssh/sshd_config.d/*.confOn Ubuntu 24.04
The same line, earlier in the file:
$ grep -n "^Include" /etc/ssh/sshd_config12:Include /etc/ssh/sshd_config.d/*.confSo the value in 50-cloud-init.conf wins over anything later. A file named 00-… is read first, so its value wins instead. That is also why a file named 99-…, as some guides suggest, does nothing on a server where 50-cloud-init.conf says yes. You could edit 50-cloud-init.conf itself, but that file belongs to cloud-init: your own file is clearer and stays yours.
On Ubuntu 26.04 and 24.04, SSH runs through ssh.socket, so both units below are active. systemctl reload ssh is the right command with that setup, as step 5 showed.
$ systemctl is-active ssh.socket ssh.serviceactiveactiveWhat a security audit says about this change
Both runs audited the whole server with Lynis, a free security auditing tool, before the first step and after the last. Its hardening index did not move (see the Tested on box below). That is expected: Lynis’s SSH test (SSH-7408 in its include/tests_ssh) checks a fixed list of 20 options, and PasswordAuthentication is not one of them. The step still matters: it is the one that stops password guessing, as step 6 proved.
The same box lists what the steps changed on the server: one new file, /etc/ssh/sshd_config.d/00-keys-only.conf, and no new package, service or open port.
Ubuntu 26.04 vs 24.04: what changed
| What | Ubuntu 26.04 | Ubuntu 24.04 |
|---|---|---|
| System | Ubuntu 26.04.1 LTS7.0.0-43-generic | Ubuntu 24.04.5 LTS6.8.0-147-generic |
| openssh-server | 1:10.2p1-2ubuntu3.8 | 1:9.6p1-3ubuntu13.20 |
| cloud-init | 26.2-0ubuntu1~26.04.1 | 26.1-0ubuntu1~24.04.1 |
Line of the Include for sshd_config.d in sshd_config | 24 | 12 |
| The new sudo user’s first command prints Ubuntu’s “To run a command as administrator” hint | No | Yes |
| Documentation link in the login banner | docs.ubuntu.com | help.ubuntu.com |
| Proof | Run log, 24 checks passed | Run log, 24 checks passed |
Undo: turn password login back on
Remove your file and reload. If you are locked out, do this from your provider’s web console:
$ sudo rm /etc/ssh/sshd_config.d/00-keys-only.conf && sudo systemctl reload ssh && sudo sshd -T | grep -i "^passwordauthentication"passwordauthentication yesIf you see “Permission denied (publickey)”
After step 5 this is the message for any login that does not offer a key the server accepts. Check you are using the right user name (deploy@…) and the computer that holds the key. If you need a password again, use the undo above from your provider’s web console.
