Available: permanent, freelance, remoteMorocco

ENFR
Contact me
Topics

NewsSecurity

Change sshd_config without locking yourself out

Backup session, scheduled rollback, sshd -t, sshd -T and Match: the method for touching SSH on a remote server.

El Mehdi Jouan

El Mehdi Jouan · Systems & support engineer

· 5 min read

Changing the SSH configuration of a remote server is sawing off the branch you sit on. One typo, one restart, and nobody can log in any more. On a machine in a distant data centre or in a cloud without a console, the mistake is expensive. Here is the method I apply to every change, even a single line.

The tests were done with OpenSSH 10.2 on Ubuntu 26.04, on test configuration files, without touching the running service. The options used have existed for a long time and also work on RHEL, Oracle Linux and Debian. The service is called sshd on the RHEL family and ssh on Debian and Ubuntu: adapt the commands.

1. Keep a door open

Before touching anything, I open a second SSH session to the server, and I do not close it until the end. Reloading or restarting sshd does not cut sessions that are already open: each one lives in its own process. As long as that session stays open, I can fix a mistake.

Then, a copy of the configuration, main file and the directory of included files (see step 3):

sudo mkdir -p /root/ssh-before
sudo cp -a /etc/ssh/sshd_config /root/ssh-before/
sudo cp -a /etc/ssh/sshd_config.d /root/ssh-before/

The copy goes to /root, not into /etc/ssh/sshd_config.d, where sshd would read it as a configuration file. On a version without a sshd_config.d directory, the third line fails harmlessly.

2. Plan the automatic rollback

For a risky change (port, authentication, AllowUsers), I schedule the restore before I even edit. If I lock myself out, the machine goes back to the old configuration on its own after ten minutes:

sudo systemd-run --unit=sshd-rollback --on-active=10min /bin/sh -c 'cp -a /root/ssh-before/sshd_config /etc/ssh/sshd_config && rm -f /etc/ssh/sshd_config.d/*.conf && cp -a /root/ssh-before/sshd_config.d/. /etc/ssh/sshd_config.d/ && systemctl restart sshd'

The command puts the main file back, removes the .conf files added since, puts the previous ones back, then restarts sshd. restart rather than reload: if the change brought the service down, reload would fail, while restart starts it again. Sessions already open are not cut. Replace sshd with ssh on Debian or Ubuntu.

I ran this restore on a copy of the tree, with a 3-second delay and without the final restart: the main file went back to the original, the added file disappeared, the original file stayed. The timer is visible and can be cancelled once the new configuration is validated:

systemctl list-timers sshd-rollback.timer
sudo systemctl stop sshd-rollback.timer

I checked both: the timer shows up with its due time, and after stop it disappears without having run the command.

3. Where to write the change

Recent versions load extra files:

grep -n '^Include' /etc/ssh/sshd_config
ls /etc/ssh/sshd_config.d/

If the line Include /etc/ssh/sshd_config.d/*.conf exists, I prefer a file of my own in that directory, for example 50-hardening.conf, rather than editing the main file. It survives package updates better.

One rule surprises almost everyone: for most options, sshd keeps the first value it meets, not the last. Since the Include usually sits at the top of the main file, the files in the directory win over what follows. I checked it: with PasswordAuthentication yes in the main file after the Include, and PasswordAuthentication no in an included file, the effective value is no. Included files are read in alphabetical order, hence the numeric prefixes.

4. Test the syntax

sudo sshd -t
echo $?

No message and exit code 0: the syntax is fine. A typo in an option name gives exit code 255 and the faulty line. On my test files:

/tmp/sshtest/conf.d/20-typo.conf: line 1: Bad configuration option: PasswordAuthenticaton
/tmp/sshtest/conf.d/20-typo.conf: terminating, 1 bad configuration options

An impossible value is also refused, again with code 255: unsupported option "peut-etre". for a PermitRootLogin peut-etre.

sshd -t tells you one thing only: the file can be read. It does not tell you whether the configuration does what you want.

5. Read the effective configuration

sudo sshd -T | grep -Ei '^(port|permitrootlogin|passwordauthentication|pubkeyauthentication|allowusers|allowgroups) '

-T prints every option with the value sshd will really use, after reading all the files, in lower case. It is the only reliable way to know whether your line won.

Match blocks change the configuration depending on the user, host or address. To see what one specific connection will get, describe it with -C:

sudo sshd -T -C user=deploy,host=laptop.example.org,addr=192.0.2.10 | grep -i '^passwordauthentication'
sudo sshd -T -C user=alice,host=laptop.example.org,addr=192.0.2.10 | grep -i '^passwordauthentication'

In my test, with a Match User deploy block that allows passwords again, the first command answers passwordauthentication yes and the second passwordauthentication no. I always give all three fields user, host and addr: older OpenSSH versions require them.

6. Apply, then test from a new connection

sudo sshd -t && sudo systemctl reload sshd

The && guarantees you never reload an invalid configuration. Then, from the workstation, without closing the backup session, I open a new connection:

ssh -l user -o PreferredAuthentications=publickey server.example.org

If it works, I cancel the rollback timer (step 2). If it fails, the backup session lets me put the copy back, and failing that the timer will.

These last two commands, the reload and the new connection, are the only ones I did not run for this article: the test machine provides SSH access to other tools, and I did not reload it.

A special case on Ubuntu: since 22.10, ssh can be started by a systemd socket. systemctl is-enabled ssh.socket tells you. In that case the listening port is driven by ssh.socket: after changing Port, you need sudo systemctl daemon-reload then sudo systemctl restart ssh.socket, otherwise the old port stays open. On the test machine, ssh.socket was disabled, so I did not reproduce this case.

On the client: the effective configuration too

The client has its own equivalent of -T, useful when a connection goes out with the wrong user or port:

ssh -G server.example.org | grep -E '^(user|port|hostname|identityfile) '

The checklist, before every change

sudo mkdir -p /root/ssh-before && sudo cp -a /etc/ssh/sshd_config /etc/ssh/sshd_config.d /root/ssh-before/
sudo sshd -t
sudo sshd -T | grep -Ei '^(port|permitrootlogin|passwordauthentication) '
sudo sshd -t && sudo systemctl reload sshd

A backup session open, a copy, a scheduled rollback, a syntax test, a read of the effective configuration, then a new connection before closing anything. It takes two minutes, and saves a trip or a console access request.

  • linux
  • ssh
  • sshd
  • openssh
  • systemd-run