Close-up of a server rack in a dim datacenter, one rack unit has a physical brass key inserted in a lock next to a glowing...

Logging in to Linux servers over SSH with a username and password is still common, and it is exactly what the bots sweeping the Internet for port 22 try around the clock. Red Hat’s and Canonical’s hardening guidance agrees on the fix: public key authentication with passwords turned off. The change is four lines of configuration, but done in a hurry it leaves you without access to the server. Here is what keys actually buy you and how to migrate step by step without locking yourself out.

Key takeaways

  • With a password, the secret reaches the server: it travels encrypted inside the SSH tunnel, but the server receives it and sees it. With a public key, only a signature bound to that session is sent.
  • The directives that matter are PubkeyAuthentication yes, PasswordAuthentication no, KbdInteractiveAuthentication no and PermitRootLogin no.
  • On Ubuntu and RHEL 9 the clean way is a file in /etc/ssh/sshd_config.d/, but sshd keeps the first value it reads: watch out for the 50-cloud-init.conf that ships with cloud images.
  • Before reloading: sshd -t, sshd -T and an open session you don’t close until everything checks out.

What really changes between a password and a key

A popular myth says the password crosses the network in readable form. It doesn’t. SSH encrypts the channel before asking for credentials, so nobody sniffing traffic sees it. The problem lies elsewhere. The password ends up on the server, which decrypts it to check it against /etc/shadow, so a compromised server (or a fake one you connect to after ignoring the host key warning) gets to keep it. On top of that, anyone on the Internet can keep guessing with no practical limit, and people reuse passwords across machines and share them over chat.

With a public key, the server stores only the public half in ~/.ssh/authorized_keys. When you connect, your machine signs that session’s data with the private key and the server verifies the signature. The private key never leaves your machine and the signature is useless for any other connection. Brute-forcing an Ed25519 key is not a realistic attack.

Diagram: with a password the secret travels to the server; with a public key only a session signature does
What the server receives in each case. Chart: systemadministration.net

One benefit shows up more in teams than in raw security: every admin has their own key, so you can remove one person’s access without touching anyone else’s, and the logs tell you which key logged in. Ed25519 is the sensible default; use RSA only to talk to old systems, and at 3072 bits or more. Protect the key with a passphrase and use it through ssh-agent. To go further, OpenSSH 8.2 and later support keys stored on FIDO2 security keys (ed25519-sk), which can’t be copied off a disk.

How to migrate without locking yourself out

The eight steps to move SSH to key-only authentication without losing access to the server
Order matters: don’t touch sshd until you have logged in with your key. Chart: systemadministration.net

1 and 2. Create the key and copy it. On your own machine, not on the server:

ssh-keygen -t ed25519 -C 'name@laptop'
ssh-copy-id -i ~/.ssh/id_ed25519.pub user@serverCode language: JavaScript (javascript)

3. Test from another terminal, forcing it not to use a password. If you get in and sudo -v works, the key is fine. Keep your existing session open until the very end.

ssh -o PasswordAuthentication=no -o KbdInteractiveAuthentication=no user@server
sudo -v

4. Put the settings in a separate file. First check that your sshd_config includes the drop-in directory and see what is already in it:

grep -n '^Include' /etc/ssh/sshd_config
ls /etc/ssh/sshd_config.d/Code language: JavaScript (javascript)

This is the trap that catches most people. sshd keeps the first value it finds for each directive, and the Include line sits at the top of the main file, so files in that directory override sshd_config and are read in alphabetical order. Many Ubuntu cloud images ship a 50-cloud-init.conf with PasswordAuthentication yes: if your file is called 60-something.conf, it does nothing. Give it a low number, or fix that file as well.

sudo tee /etc/ssh/sshd_config.d/00-keys-only.conf <<'EOF'
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
EOFCode language: JavaScript (javascript)

KbdInteractiveAuthentication no is not decoration: with PAM enabled, the server can still prompt for the password through keyboard-interactive auth even when PasswordAuthentication is no. Older OpenSSH releases called this directive ChallengeResponseAuthentication. As for PermitRootLogin, the default since OpenSSH 7.0 is already prohibit-password, which lets root in with a key only. no shuts it off completely and makes you log in as yourself and use sudo; if some automation logs in as root with a key, keep prohibit-password until you change it. If your system has no Include line (RHEL 8, for example), put the same lines directly in sshd_config.

5 and 6. Check the syntax and the effective configuration. sshd -t only tells you whether the file is valid; sshd -T prints the value that will actually apply, which is what exposes the cloud-init file problem.

sudo sshd -t
sudo sshd -T | grep -Ei 'passwordauthentication|kbdinteractive|pubkeyauthentication|permitrootlogin'Code language: JavaScript (javascript)

7. Reload the service. A reload does not drop open sessions. The unit name depends on the distribution:

# Ubuntu and Debian
sudo systemctl reload ssh
# RHEL, Rocky, Alma and Fedora
sudo systemctl reload sshdCode language: PHP (php)

8. Confirm passwords no longer work. From another machine, force password auth. The right answer is Permission denied (publickey). Then log in again with your key from a fresh terminal, and only then close your safety session.

ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password user@server

When it’s hundreds of servers

Across a fleet, push 00-keys-only.conf with Ansible, Puppet or whatever you already run, validate with sshd -t before reloading, and roll out in batches, never everywhere at once. Keep a way in that doesn’t depend on SSH: your cloud provider’s serial console, iDRAC or iLO on bare metal, or the Proxmox console. It’s also worth limiting who can log in with AllowGroups and keeping two authorized keys per person or service in case one is lost.

The weak spot of keys at scale is revoking them: removing someone means deleting their line from authorized_keys on every server. That is what SSH certificates with your own OpenSSH CA solve, since they expire on their own and don’t require touching each machine. Tools like fail2ban or sshguard are still useful, although with passwords off they mostly cut noise from the logs.

Frequently asked questions

Does SSH send the password in clear text?

No. It travels encrypted inside the SSH tunnel, but the server receives and decrypts it to check it, so a compromised or impersonated server can capture it.

Ed25519 or RSA?

Ed25519 for anything new. RSA only for old systems that don’t support it, at 3072 bits or more.

Is PasswordAuthentication no enough?

Not always. You also need to turn off KbdInteractiveAuthentication and check with sshd -T that no file in sshd_config.d, such as the cloud-init one, turns passwords back on.

What if I lose my private key?

Log in with another authorized key or through the provider or hardware console. That’s why it pays to keep two authorized keys and a way in that doesn’t depend on SSH.

Sources:

Scroll to Top