Anime-Sanctuary

Secure SSH: turn off passwords

Your server still accepts a password, so anyone can try to guess it. Here is how to turn off SSH password login on Debian and let in only your key, without locking yourself out.

Course
Tutorial 11 of 35
Time
25 min to read
Tested with
Debian 13.7 (trixie), OpenSSH 10.0p1
What you will be able to do
  • Send your public key to the server with ssh-copy-id.
  • Prove with a second connection that the key is enough.
  • Forbid passwords and root login over SSH.
  • Reload the service without closing your session, and know how to roll back.

Before you start: What is SSH?, Users, sudo and file permissions.

Contents
  1. Why turn passwords off
  2. Plan your way out
  3. 1. Send your key to the server
  4. 2. Test a second connection
  5. 3. Write the rule
  6. 4. Reload without closing the session
  7. 5. Check that the door is shut
  8. If it fails
  9. What's next
  10. Sources

How do you turn off SSH password login without getting locked out? In two steps: first prove that your key opens the door, and only then board up the password entrance. You know how to make a key thanks to What is SSH?, and you created the alice account with sudo in Users, sudo and file permissions. What's left is to close the old door, the one anybody can try to break open.

Why turn passwords off

A password can be guessed, a key can't. As soon as a server has a public address, automatic programs try passwords on it, day and night, without ever tiring. They aim first at the root account (the administrator), because it exists on every Linux machine.

An SSH key comes in two pieces, like a padlock and its key. The public key is the padlock: you leave it on the server, and it's useless to anyone else. The private key is the padlock's key: it stays on your computer, and nobody can guess it. So we'll do two things:

  • forbid passwords for SSH login;
  • forbid root from logging in over SSH, since alice and sudo are enough.

The risk is simple: if the key doesn't work when you turn the password off, nobody gets in. So, one precaution first.

Plan your way out

Before touching anything, find your hosting provider's remote console: a virtual screen and keyboard shown on its website, described in VPS or dedicated server: which one to pick?. It doesn't go through SSH, so it stays open even if the SSH door is boarded up.

Open it now and log in with alice and her password. If you get in, you have your emergency exit, and alice's password will always work there. Keep it open in a tab for the whole article.

The emergency exit is ready. Now let\'s put the key on the server.

1Send your key to the server

The ssh-copy-id command puts your public key on the server's alice account. Run it from your computer: it's the last time the password gets used. If you already have a key (see What is SSH?), keep it. If not, create one: ed25519 is a modern key type, so keep that choice.

On your computer
ssh-keygen -t ed25519 -C "alice@mon-ordinateur"
Expected output
Generating public/private ed25519 key pair.
Enter file in which to save the key (/home/alice/.ssh/id_ed25519):
Enter passphrase for "/home/alice/.ssh/id_ed25519" (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/alice/.ssh/id_ed25519
Your public key has been saved in /home/alice/.ssh/id_ed25519.pub
The key fingerprint is:
SHA256:F4eAKXsG+VNWMIJcr1I+PZg1dundOnVVv0FN/AvcaWU alice@mon-ordinateur

If the program says the file already exists, you already have a key: type n to keep it. Then send the public key to the server, replacing 203.0.113.10 (an example address) with your server's.

On your computer
ssh-copy-id alice@203.0.113.10
Expected output
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/alice/.ssh/id_ed25519.pub"
/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 keys
alice@203.0.113.10's password:

Number of key(s) added: 1

Now try logging into the machine, with: "ssh 'alice@203.0.113.10'"
and check to make sure that only the key(s) you wanted were added.

Type alice's password when it's asked for. "Number of key(s) added: 1" confirms the key is in place. But in place doesn't mean it works, so let's try it for real.

2Test a second connection

Open a second connection with the key alone, and leave the first one open. The -o PasswordAuthentication=no option stops your computer from offering a password. If the connection succeeds, the key is what opened the door.

On your computer, in a new terminal
ssh -o PasswordAuthentication=no alice@203.0.113.10 whoami
Expected output
alice

Getting alice back proves the key is enough. If your key is protected by a passphrase (a password that guards the key itself), your computer asks for it first: that's normal. If you read Permission denied (publickey) instead, stop right there and go to "If it fails": turning the password off now would lock you out.

The key works, so we can board up the password. That takes a rule written in the server's configuration.

3Write the rule

The program that welcomes SSH connections is called sshd (the "d" stands for daemon: a program that runs all the time in the background). Its settings live in /etc/ssh/sshd_config and in the /etc/ssh/sshd_config.d/ folder. We leave the main file alone and add our own small file in that folder. It survives updates, and deleting it is enough to go back.

In the first session (alice's, on the server), type the next command. It writes two lines into the 10-securite.conf file. tee writes them and prints them, and sudo gives it the right to write in /etc.

On the server
printf "PasswordAuthentication no\nPermitRootLogin no\n" | sudo tee /etc/ssh/sshd_config.d/10-securite.conf
Expected output
PasswordAuthentication no
PermitRootLogin no

Those two lines are two rules:

  • PasswordAuthentication no: no passwords over SSH;
  • PermitRootLogin no: root can't come in over SSH anymore, even with a key.
Why the name starts with 10

sshd reads the files in alphabetical order and keeps the first value it finds for each setting. A name starting with 10 therefore comes before a vendor file called 50-something.conf. This is a quirk of sshd: other programs do the opposite.

Nothing has changed yet: sshd only rereads its settings when asked. Before that, let's have it check what we wrote.

On the server
sudo sshd -t

The -t option ("test", lowercase) checks the syntax without applying anything. No answer means everything is correct. A typo would be reported with the file and the line: see "If it fails". Let's also look at what sshd understood.

On the server
sudo sshd -T | grep -E "passwordauthentication|permitrootlogin"
Expected output
permitrootlogin no
passwordauthentication no

The -T option ("show everything", uppercase) prints all the settings in force, and grep keeps only our two lines. Two no lines: the rule is right. A yes means another file wins, so see "If it fails". Now ask sshd to reread its settings.

4Reload without closing the session

The reload command asks the service to reread its settings without stopping. Connections that are already open carry on, yours included; only new ones follow the new rule. On Debian, this service is called ssh.

On the server
sudo systemctl reload ssh

It prints nothing when it works. Don't type restart instead: a restart is rougher, and reload is enough. Your session stays open, and that's deliberate: it's your safety net if the next part goes wrong. Let's see whether the door is really shut.

5Check that the door is shut

Open a fresh connection with the key, in a third terminal. It must get through, because the rule only targets the password.

On your computer, in a new terminal
ssh alice@203.0.113.10 hostname
Expected output
mon-serveur

Now let's try the old method. The -o PubkeyAuthentication=no option stops your computer from offering the key: only the password is left, and the door must stay shut.

On your computer
ssh -o PubkeyAuthentication=no alice@203.0.113.10
Expected output
alice@203.0.113.10: Permission denied (publickey).

The server no longer even asks for a password: it answers at once that only a key gets in. With root@203.0.113.10, the answer is the same. The bot that guessed passwords now hits a wall.

The door is shut and the key opens it. If something went wrong along the way, here is what to do.

If it fails

Four cases come up most often. To see the first one, the BatchMode=yes option tells ssh to try only the key and never ask for a password.

On your computer
ssh -o BatchMode=yes alice@203.0.113.10 true
Expected output
alice@203.0.113.10: Permission denied (publickey).
  • Permission denied (publickey): the server rejects your key. Often, the server's ~/.ssh folder is too open and sshd distrusts it. From an open session or the console, type chmod 700 ~/.ssh then chmod 600 ~/.ssh/authorized_keys. Otherwise, run ssh-copy-id again (step 1).
  • A typo in the rule, for example PasswordAuthentification instead of PasswordAuthentication. sudo sshd -t gives the file and the line. Fix it, run it again, and only reload once it prints nothing.
  • sshd -T still shows yes: a file read before yours already sets the value. List the folder with ls /etc/ssh/sshd_config.d/ and rename yours with a smaller number, like 00-securite.conf.
  • You're already locked out. Open the remote console, log in with alice and her password, then undo the rule (command below).

This command deletes the file, then reloads the service. The password works over SSH again, and you can hunt for the mistake in peace.

On the server (in the console)
sudo rm /etc/ssh/sshd_config.d/10-securite.conf
sudo systemctl reload ssh

What's next

Your server now opens its SSH door only to your key: a bot can no longer try passwords on it. But other doors are still open: as long as no guard checks who knocks, every service that listens can be reached from anywhere. The next article, A firewall with ufw, puts that guard in place. The timing is good: you must allow SSH before turning it on, and you just checked that SSH works.