GrepBoard/Manuals
605 manuals ·

Check sshd_config with sshd -t before you reload

·Tested on Ubuntu 24.04.5 · OpenSSH 9.6p1 (openssh-server 1:9.6p1-3ubuntu13.20)·Test scope ↓·7 steps

Catch sshd_config typos with sshd -t before you reload. Covers real error messages, exit codes, Match block checks, and -T for the effective values.

After editing /etc/ssh/sshd_config, run sshd -t before you reload. On a remote server, restarting sshd with a broken config can leave you with no way back in. The output below comes from a test copy, not a live config.

[1] Point -f at the edited file. A clean file prints nothing and exits 0. The live file is /etc/ssh/sshd_config; the test copy here is /srv/app/sshd_config.

console
# cat /srv/app/sshd_configPort 2222PermitRootLogin noPasswordAuthentication no# sshd -t -f /srv/app/sshd_config; echo "exit=$?"exit=0

[2] A misspelled option is reported with its line number. Here PermitRootLogin became PermitRootLogn.

output
/srv/app/bad_config: line 3: Bad configuration option: PermitRootLogn/srv/app/bad_config: terminating, 1 bad configuration options

The exit code was 255. In a script, sshd -t && systemctl reload ssh skips the reload when the check fails.

[3] A bad value produces a different message, with no colon after the line number.

output
/srv/app/bad_val line 4: unsupported option "maybe"./srv/app/bad_port line 2: Badly formatted port number.

The first came from PasswordAuthentication maybe, the second from Port 99999. Both exit 255.

[4] Typos inside a Match block are caught too. A directive that is not allowed there produces two lines.

console
# printf 'Port 2222\nMatch User deploy\n    PermitRootLogn no\n' > /srv/app/m# sshd -t -f /srv/app/m; echo "exit=$?"/srv/app/m: line 3: Bad configuration option: PermitRootLogn/srv/app/m line 3: Directive 'PermitRootLogn' is not allowed within a Match blockexit=255

[5] A valid syntax can still hold the wrong value. -T runs the check and then prints the effective settings, with lowercase keys. Pass a Match condition with -C to see the values for that connection.

console
# sshd -T -f /srv/app/sshd_config | grep -Ei '^(port|permitrootlogin|passwordauthentication) 'port 2222permitrootlogin nopasswordauthentication no

On another copy with PermitRootLogin yes inside Match User deploy, adding -C user=deploy,host=server1,addr=203.0.113.5 printed permitrootlogin yes. -C needs user, host and addr together.

[6] File and directory problems give their own errors.

SituationOutputExit code
-f file does not exist/srv/app/nope: No such file or directory1
HostKey file missingUnable to load host key: /srv/none_key · sshd: no hostkeys available -- exiting.1
/run/sshd missingMissing privilege separation directory: /run/sshd255

The /run/sshd error shows up when you run sshd by hand rather than as a service. tmpfiles creates the directory at boot, and the unit file has RuntimeDirectory=sshd. You can create it yourself with mkdir -p /run/sshd, but it disappears on reboot.

[7] The service runs the same check. On Ubuntu 24.04, ssh.service has ExecStartPre=/usr/sbin/sshd -t and ExecReload=/usr/sbin/sshd -t.

console
# grep -n 'Exec' /usr/lib/systemd/system/ssh.service9:ExecStartPre=/usr/sbin/sshd -t10:ExecStart=/usr/sbin/sshd -D $SSHD_OPTS11:ExecReload=/usr/sbin/sshd -t12:ExecReload=/bin/kill -HUP $MAINPID

Because reload runs the test first, a broken config should not reach the running daemon; the reload fails and the old settings stay. If you are changing the port, ssh.socket gets involved, so read this post first.

Analyze your auth.log (SSH) log in the browser ↗ — nothing is uploaded.

Check it with the ssh_config generator tool ↗ — calculated in your browser.

#ssh#sshd#config