Hardening SSH on a server
Lock sshd down to keys only, no root and named users, prove the effective config with sshd -T, and add fail2ban without locking yourself out.
sudo and the sudoers filejournald and journalctlPacket filtering with nftables
Hardening SSH on a server
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Any SSH server on a public address gets constant automated password guessing. The fix is to make passwords useless: `PasswordAuthentication no` and `KbdInteractiveAuthentication no`, with key-based logins only.
- `PermitRootLogin no` makes people log in as themselves and use sudo, which leaves an audit trail. `AllowUsers` or `AllowGroups` turns SSH access into an explicit list.
- In `sshd_config`, the first value for a keyword wins. A drop-in in `/etc/ssh/sshd_config.d/` read early can silently override what you write later in the main file.
- `sshd -t` checks the syntax. `sshd -T` prints the effective configuration, and `-C user=...` evaluates `Match` blocks for a given login. Read it before you reload.
- Change SSH safely: confirm a key login works, keep a second session open, test the config, reload, and try a fresh login before closing anything.
- fail2ban watches the logs for failed logins and adds temporary firewall bans. Upstream defaults ban for 10 minutes after 5 failures within 10 minutes. It cuts noise, but it does not replace keys-only.
Port 22 is never quiet
Put a fresh server on a public address, wait an hour, and read the journal for the SSH service. You will find failed logins for root, admin, ubuntu, oracle and a long list of others, from addresses all over the world. Nobody targeted you. Bots sweep the whole IPv4 space looking for any SSH server that accepts a weak password.
SSH itself is not the weakness. The weakness is letting it accept guessable credentials. Hardening SSH means making the guessing pointless, then reducing who can even try. How keys work and how to use them through a bastion is covered in SSH keys, bastions and remote access. Day-to-day client use is in remote work with ssh, scp and rsync. This lesson is the server side: sshd.
The settings that matter
From the sshd_config manual (OpenBSD’s copy, which tracks upstream OpenSSH, 10.6 being current as of October 2026):
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowGroups ssh-users
PasswordAuthenticationdefaults toyes. Turn it off, and every guessing bot is reduced to noise.KbdInteractiveAuthenticationalso defaults toyes. It is a second way to type a password, through PAM, and it is easy to forget. If you leave it on, passwords may still work.PermitRootLogindefaults toprohibit-password, which still allows root with a key. Settingnomakes everyone log in as themselves and escalate with sudo, so the logs say who did something.AllowUsersorAllowGroupsturns SSH into an allow-list. The manual says a Deny match takes precedence over the matching Allow list. A group is easier to manage than a list of names.
Two defaults worth knowing: MaxAuthTries is 6 and LoginGraceTime is 120 seconds.
The first value wins
Here is the trap that catches experienced people. The manual says: “Unless noted otherwise, for each keyword, the first obtained value will be used.” Most distributions now ship an Include /etc/ssh/sshd_config.d/*.conf line near the top of the main file, and cloud images often put a file in there. If that file says PasswordAuthentication yes, your no further down the main file is ignored with no warning.
I tested this with the sshd that ships on macOS, which is the same upstream OpenSSH (10.5p1). This is a real run with a throwaway config and a scratch host key, so the server never started. Only the host key path is shortened:
$ cat t.conf
HostKey /path/to/scratch/k
PasswordAuthentication no
PermitRootLogin no
AllowUsers deploy
PasswordAuthentication yes
$ /usr/sbin/sshd -T -f t.conf | grep -iE '^(passwordauthentication|permitrootlogin|allowusers|kbdinteractiveauthentication|maxauthtries|logingracetime) '
LoginGraceTime 120
MaxAuthTries 6
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication yes
AllowUsers deploy
The second PasswordAuthentication yes lost, because the first one was read first. And KbdInteractiveAuthentication is still yes, because I did not set it. So never trust what a file says. Trust sshd -T, which the sshd manual describes as checking the config and printing the effective configuration. Add -C user=alice,addr=192.0.2.5 to see what applies to one login after Match blocks are evaluated. Put your own settings in a drop-in with a low number, such as /etc/ssh/sshd_config.d/00-hardening.conf, so it is read first.
Changing SSH without locking yourself out
You are editing the door you came in through. Work in this order:
- Confirm a key login works for an account in the allowed group.
- Keep your current session open. It survives a reload.
sudo sshd -tfor syntax, thensudo sshd -Tto read the result.sudo systemctl reload sshon Debian and Ubuntu,sshdon the RHEL family.- Open a new terminal and log in. Only then close the old one.
Since Ubuntu 24.04, sshd is started through socket activation, and the socket’s port is generated from sshd_config. Ubuntu’s own bug tracker (LP #2069041) settled that a Port change needs systemctl daemon-reload and systemctl restart ssh.socket, not just a reload of the service. Confirm with ss -tlnp before you close anything. Moving off port 22 cuts log noise, but it is not a defence on its own. On SELinux systems the new port also needs a label, as the SELinux lesson shows.
fail2ban
With passwords off, the bots cannot get in, but they still fill your logs. fail2ban reads the logs or the journal (see journald), counts failures per address, and adds a temporary firewall ban. In the upstream jail.conf (fail2ban 1.1.1), the defaults are bantime = 10m, findtime = 10m and maxretry = 5, and the file itself says not to edit it: put changes in jail.local or jail.d/*.local.
# /etc/fail2ban/jail.d/sshd.local
[sshd]
enabled = true
maxretry = 4
bantime = 1h
sudo fail2ban-client status sshd lists current bans. Treat fail2ban as noise control and a speed bump. It does nothing against an attacker with a valid key, and a careless ban list can lock out your own office’s address.
Apart from the macOS sshd -T run above, the commands here are written from the OpenSSH manuals and fail2ban’s upstream config, checked 2026-11-28, and were not run on Linux.
About the video
The embed is all of The Lazy SysAdmin’s “6 SSH Security Settings You Should Change! | Complete SSH Hardening Guide”, November 2026, about 13 minutes. Its chapters follow this lesson closely: editing sshd_config safely, key setup, disabling passwords and root login, AllowUsers, opening the firewall on both ufw and firewalld, changing the port with the SELinux label, fail2ban, and testing changes without locking yourself out. As far as the chapter list shows, it does not cover the first-value-wins rule or sshd -T, which is why this lesson spends a section on them. I judged it from the title, chapters and description, and English from the English auto-caption track.
Where this fits
SSH is the one door you must leave open. Next, managing the rest of the firewall without writing raw nftables: firewall front ends: ufw and firewalld.
Check yourself
You add `PasswordAuthentication no` at the bottom of `/etc/ssh/sshd_config` and reload, but password logins still work. What should you check?
Whether an earlier line or an included drop-in in `/etc/ssh/sshd_config.d/` already sets `PasswordAuthentication yes`. For each keyword the first value wins. `sshd -T | grep -i passwordauthentication` shows what is in effect.
Why disable root login over SSH if root has a strong key?
Every admin logging in as root leaves no record of who did what, and one leaked key gives full control at once. Logging in as a named user and using sudo keeps accountability and adds a second step.
What is the safe order of steps when changing sshd on a remote server?
Confirm your key login works, keep an existing session open, run `sshd -t` (and `sshd -T` to review), reload the service, then open a new session to prove you can still get in before closing the old one.
Does moving SSH to port 2222 make the server more secure?
Only slightly. It cuts log noise from untargeted scanners but stops nobody who scans all ports. Keys-only authentication is what actually blocks guessing.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.