Switch SSH to key-only login without locking yourself out
A server that accepts SSH passwords gets password guesses from bots all day. With key-only login, a guessed or leaked password is not enough to get in. The switch takes a few minutes; the order of the steps is what keeps you from locking yourself out.
1. Create a key on your workstation
ssh-keygen -t ed25519 -C "alice@laptop"
This writes the private key ~/.ssh/id_ed25519 and the public key ~/.ssh/id_ed25519.pub. Give the key a
passphrase when asked; ssh-agent (started by most desktop sessions) remembers it so that you type it once per
login. The private key never leaves your machine. Only the .pub file goes to servers.
If you have a FIDO2 security key, ssh-keygen -t ed25519-sk creates a key that needs the device for every
login. The server needs OpenSSH 8.2 or later for that key type.
2. Have a user with sudo
Log in as a normal user and use sudo for administration. If you still log in as root, create the user now,
in the session you already have. As root, the commands below work without the sudo in front; install sudo
first if it is missing (apt install sudo or dnf install sudo).
Debian and Ubuntu:
sudo adduser deploy
sudo usermod -aG sudo deploy
RHEL, Rocky Linux and AlmaLinux:
sudo useradd -m deploy
sudo passwd deploy
sudo usermod -aG wheel deploy
Alpine (as root):
apk add sudo
adduser deploy
addgroup deploy wheel
echo '%wheel ALL=(ALL:ALL) ALL' > /etc/sudoers.d/wheel
chmod 440 /etc/sudoers.d/wheel
visudo -c
On Alpine, sudo is in the community repository. If apk add sudo does not find it, enable the community
line in /etc/apk/repositories and run apk update.
The password of the user stays: sudo asks for it. It is no longer used for SSH.
3. Copy the public key to the server
From the workstation, while password login still works:
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@203.0.113.10
ssh-copy-id asks for the password once and appends the key to ~/.ssh/authorized_keys of the user. Without
ssh-copy-id, do it by hand on the server, as the user deploy:
mkdir -p ~/.ssh
chmod 700 ~/.ssh
echo 'ssh-ed25519 AAAA... alice@laptop' >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
sshd ignores authorized_keys when the file, ~/.ssh or the home directory can be written by other users
(the StrictModes setting, on by default). On RHEL, Rocky and Alma, run restorecon -R ~/.ssh after creating
the files by hand, so that SELinux labels them correctly.
4. Test the key
Open a new terminal and keep the old session open:
ssh -o PreferredAuthentications=publickey deploy@203.0.113.10
sudo -v
The login must work without the account password (the key passphrase is fine), and sudo -v must accept the
password of the user. If the login fails, ssh -v shows which keys the client offered, and the log on the
server shows why sshd refused them:
sudo journalctl -u ssh -n 20 # Debian, Ubuntu
sudo journalctl -u sshd -n 20 # RHEL, Rocky, Alma
sudo tail -n 20 /var/log/messages # Alpine with syslog
5. Turn off passwords
Only after step 4 worked. On RHEL 8, Rocky Linux 8 and AlmaLinux 8, sshd_config does not read
/etc/ssh/sshd_config.d/, and a file there would be ignored. Add the Include line at the top first:
sudo mkdir -p /etc/ssh/sshd_config.d
sudo sed -i '1i Include /etc/ssh/sshd_config.d/*.conf' /etc/ssh/sshd_config
Then, on every system:
sudo tee /etc/ssh/sshd_config.d/10-hardening.conf >/dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
EOF
sudo sshd -t
sshd -t prints nothing when the file is valid. Then reload sshd: sudo systemctl reload ssh on Debian and
Ubuntu, sudo systemctl reload sshd on RHEL, Rocky and Alma, sudo rc-service sshd reload on Alpine. The
open session stays connected.
The file name starts with 10- on purpose. sshd keeps the first value it reads for each setting, and the
files in /etc/ssh/sshd_config.d/ are read in alphabetical order. A file such as 50-cloud-init.conf with
PasswordAuthentication yes would otherwise win. grep -i '^include' /etc/ssh/sshd_config shows whether
sshd_config reads the directory; if it prints nothing, add the line as shown above for RHEL 8.
6. Check the result
On the server:
sudo sshd -T | grep -E '^(passwordauthentication|kbdinteractiveauthentication|permitrootlogin) '
Expected output:
permitrootlogin no
passwordauthentication no
kbdinteractiveauthentication no
From the workstation, in a new terminal, force a password login:
ssh -o PubkeyAuthentication=no deploy@203.0.113.10
The answer must be deploy@203.0.113.10: Permission denied (publickey).
If you locked yourself out
Use the console of the hosting provider (web console, VNC, IPMI) and log in there with the password of the
user; the console does not go through SSH. Remove or fix /etc/ssh/sshd_config.d/10-hardening.conf and
reload sshd. Where the console needs a root password you do not have, boot the rescue system of the provider,
mount the disk and edit the file there.
Keep the keys in order
- One key per person and device. When a laptop is lost or someone leaves, remove that line from
authorized_keys; the other keys keep their access. - List the keys a user has, with their fingerprints:
ssh-keygen -lf ~/.ssh/authorized_keys. - Keys for scripts and backups can be limited in
authorized_keys, for examplefrom="198.51.100.7",command="/usr/local/bin/backup",restrict ssh-ed25519 AAAA....
Next steps are in the SSH hardening guide: fewer authentication tries, X11 forwarding off, and a check of the algorithms.
Sources
- ssh-keygen(1) — key types,
-lfingerprints - sshd(8): AUTHORIZED_KEYS FILE FORMAT —
from=,command=,restrict - sshd_config(5) —
StrictModes, first obtained value wins,Include ssh-copy-idof OpenSSH (read in openssh-clients 9.9p1 on Rocky Linux 9.9) runsrestorecon -Fon~/.sshandauthorized_keyswhen the command exists- RHEL 8 tested in
rockylinux/rockylinux:8.10.20240528(openssh-server 8.0p1-35.el8_10) on 2026-11-26:sshd_configwithoutInclude, theIncludeline at the top and the drop-in file of step 5 - OpenSSH 8.2 release notes — FIDO/U2F key types
ecdsa-skanded25519-sk
Check your servers every day
The agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.