Guides / Linux servers

Switch SSH to key-only login without locking yourself out

beginnerUpdated 26 Nov 2026948 words

With key-only login, a guessed or leaked password does not get anyone into the server. The order of the steps below keeps a working login at every point: you test the key before you turn passwords off.

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 you type it once per login. The private key stays on your machine. Only the .pub file goes to servers.

With 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 user keeps the password: sudo asks for it. SSH stops accepting it at step 5.

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 other users can write to the file, to ~/.ssh or to the home directory (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 sshd would ignore a file there. 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- because sshd keeps the first value it reads for each setting and reads the files in /etc/ssh/sshd_config.d/ 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 asks for 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 of a user with their fingerprints: ssh-keygen -lf ~/.ssh/authorized_keys.
  • Limit keys for scripts and backups in authorized_keys, for example from="198.51.100.7",command="/usr/local/bin/backup",restrict ssh-ed25519 AAAA....

The SSH hardening guide covers fewer authentication tries, X11 forwarding and the algorithms.

Sources

  • ssh-keygen(1): key types, -l fingerprints
  • sshd(8): AUTHORIZED_KEYS FILE FORMAT: from=, command=, restrict
  • sshd_config(5): StrictModes, first obtained value wins, Include
  • ssh-copy-id of OpenSSH (read in openssh-clients 9.9p1 on Rocky Linux 9.9) runs restorecon -F on ~/.ssh and authorized_keys when the command exists
  • RHEL 8 tested in rockylinux/rockylinux:8.10.20240528 (openssh-server 8.0p1-35.el8_10) on 2026-11-26: sshd_config without Include, the Include line at the top and the drop-in file of step 5
  • OpenSSH 8.2 release notes: FIDO/U2F key types ecdsa-sk and ed25519-sk

Check your servers every day

The Vulnpocalypse agent reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.

Start free