Harden OpenSSH on Ubuntu 26.04

– min read

I logged into a freshly deployed Ubuntu 26.04 server last week and ran sshd -T | grep password. The answer: passwordauthentication yes. That is the default on every Ubuntu release going back years — and frankly, if you expose that to the open internet you are asking for a brute-force script to find you. This guide walks through a practical hardening config using OpenSSH drop-in files, tests every setting, and shows how to verify nothing breaks before you reload.

Official site: openssh.com | GitHub: openssh/openssh-portable


Step 1: Read your current defaults
Before touching anything, dump the effective config. The Include directive lives on line 24 of /etc/ssh/sshd_config on Ubuntu 26.04 — the drop-in files are parsed before the rest of the file, so first-match-wins means a drop-in overrides the default sshd_config body. Focus on the settings that matter for security:

# Check current active defaults
sshd -T | grep -E "password|pubkey|permitrootlogin|maxauthtries|\
    logingracetime|clientalive|x11forwarding|allowtcp|allowagent"


passwordauthentication yes
pubkeyauthentication yes
permitrootlogin prohibit-password
maxauthtries 6
logingracetime 120
clientaliveinterval 0
clientalivecountmax 3
x11forwarding yes
allowtcpforwarding yes
allowagentforwarding yes
authenticationmethods any

Yep — passwordauthentication yes, x11forwarding yes, maxauthtries 6. These are all fine on a trusted LAN but risky on any public-facing box. (I covered firewall rules separately in the nftables guide.)


Step 2: Write the hardening drop-in
Create a single file in /etc/ssh/sshd_config.d/. Everything lives in one place — no need to edit the main config and risk a syntax error at line 145. I use 99-hardening.conf so the filename sorts last:

# /etc/ssh/sshd_config.d/99-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
ChallengeResponseAuthentication no
PermitRootLogin prohibit-password
MaxAuthTries 3
MaxSessions 10
LoginGraceTime 30
ClientAliveInterval 300
ClientAliveCountMax 0
PubkeyAuthentication yes
AuthenticationMethods publickey
AllowUsers myuser
Banner /etc/ssh/banner.txt
X11Forwarding no
AllowTcpForwarding no
AllowAgentForwarding no
PermitTTY no
PermitTunnel no
GatewayPorts no
Ciphers [email protected],[email protected]
MACs [email protected],[email protected]
KexAlgorithms [email protected],curve25519-sha256
HostKeyAlgorithms ssh-ed25519,[email protected]

The Include at the top of sshd_config means these values are parsed first — they win over anything set later in the main config. That is why you do not need to touch the original file at all.


Step 3: Syntax check before reload
Always run sshd -t first. No output means the config is valid. I also dump the effective config with sshd -T to confirm every setting landed correctly:

# Syntax check — no output = OK
sshd -t
echo "Exit code: $?"

# Verify effective config
sshd -T | grep -E "passwordauth|pubkeyauth|permitrootlogin|\
    maxauthtries|logingracetime|clientalive|x11forwarding|\
    allowtcp|authenticationmethods"
logingracetime 30
maxauthtries 3
clientaliveinterval 300
clientalivecountmax 0
permitrootlogin prohibit-password
pubkeyauthentication yes
passwordauthentication no
kbdinteractiveauthentication no
x11forwarding no
permitemptypasswords no
allowtcpforwarding no
allowagentforwarding no
authenticationmethods publickey

Every target setting shows the hardened value. passwordauthentication no, maxauthtries 3, x11forwarding no — all confirmed before the reload reaches a single running session.


Step 4: Reload without disconnecting
OpenSSH reloads its config on SIGHUP without dropping existing connections. Keep your current session alive — open a second terminal to verify the new config actually works before you close the first one:

# Reload (safe — running sessions stay connected)
systemctl reload sshd

# Confirm process accepted the new config
journalctl -u ssh -n 5 --no-pager | tail -3


SSH hardening upgrade workflow — safe drop-in config process. Author’s own diagram.


Step 5: Verify with real connections
I spun up a sandbox sshd on port 2222 with the same drop-in to test both authentication methods. Here is what happens when you request password auth:

# Password auth attempt — must fail
ssh -o PreferredAuthentications=password \
    -o StrictHostKeyChecking=no \
    -p 2222 [email protected] "echo OK"
[email protected]: Permission denied (publickey).

The server returns Permission denied (publickey) — password auth is not even offered as an option. Now with an SSH key:

# Key auth — must succeed
ssh -i ~/.ssh/id_ed25519 \
    -o StrictHostKeyChecking=no \
    -p 2222 [email protected] \
    "echo 'SSH HARDENING verified'"

# Also check the auth method from client side
ssh -G -p 2222 [email protected] | grep authentication
SSH HARDENING verified

# ssh -G shows client defaults (not server constraints):
kbdinteractiveauthentication yes
passwordauthentication yes
pubkeyauthentication true

The ssh -G output shows client-side defaults, not what the server enforces. To see the server-side effective config, run sshd -T on the server itself. The practical test — password rejected, key accepted — is the real proof. I wrote a companion script to audit sshd_config gaps that checks your running config against a baseline.


OpenSSH defaults vs hardened settings from sshd -T. Author’s own diagram.


Step 6: Network-aware rules with Match blocks
Drop-in files support OpenSSH Match blocks too. For example, allow password auth only from your management subnet while keeping the rest of the world on publickey-only:

# At bottom of the same drop-in:
Match Address 10.0.0.0/8
    PasswordAuthentication yes
    AuthenticationMethods publickey,password

Match all
    PasswordAuthentication no

The Match all at the end resets the default to no-password for everyone else. This is handy when you manage servers across multiple subnets. If you maintain authorised_keys across users, the authorized keys audit script helps track who has access.


That covers the essential OpenSSH hardening I apply to every Ubuntu 26.04 server I set up. Drop-in configs, syntax check, reload without disconnecting, and verify with a real test connection. The whole process takes about five minutes and the payoff is not waking up to a SSH brute-force alert at 3 AM.

Mohamad ZulfahmyCybersecurity · OffSec · Entrepreneur
About me ↗
ON THIS PAGE
On this page