Hardening SSH: the defaults worth changing
The sshd settings that most reduce the attack surface of a Linux host's front door, what each one actually does, and how to apply them without locking yourself out.
Contents
Where SSH came from
Early in 1995 Tatu Ylönen, a researcher at the Helsinki University of Technology, learned that a password sniffer had been running on the university’s network. Logins then travelled by Telnet and rlogin, in the clear. Ylönen wrote a replacement that encrypted the whole session, released it that July as SSH, and by the end of the year it had some 20,000 users in fifty countries. OpenSSH followed from the OpenBSD project in 1999.
Thirty years on, SSH is the primary administrative interface for most Linux systems, which makes it one of the more consequential services to get right. The defaults are functional but not particularly strict, and a few deliberate changes, none of them much effort, narrow the attack surface considerably. What follows are the steps applied to a set of personal systems, with the reasoning for each and the places where the obvious reading of a setting is wrong.
Disable password authentication
This is the most important one. With password authentication enabled, the server is permanently exposed to brute-force attacks. Fail2Ban helps, but the better fix is to accept no passwords at all.
In /etc/ssh/sshd_config:
PasswordAuthentication no
ChallengeResponseAuthentication no
The second line closes the other route by which a password can reach the server, keyboard-interactive authentication. Since OpenSSH 8.7 its proper name is KbdInteractiveAuthentication; ChallengeResponseAuthentication still works as a deprecated alias.
Before applying this, make absolutely sure key-based authentication works. Test it from a second terminal before closing the first. Losing the only access path to a remote server is an avoidable lesson.
Change the default port
Port 22 is scanned constantly. Moving SSH to a non-standard port (anything above 1024, something memorable and not commonly scanned) cuts the noise in the logs dramatically and takes the server out of most automated scan-and-attack pipelines.
Port 2222
This is security through obscurity and no substitute for real hardening. Combined with the rest, it makes the server effectively invisible to most opportunistic scanning, and the logs go quiet. A targeted attacker will find the port with one full scan.
Update the firewall rules to match, and put the port in the client config (~/.ssh/config) to avoid typing it every time. Two distributions need one more step. On Fedora and RHEL, SELinux has to be told the new port belongs to SSH, with sudo semanage port -a -t ssh_port_t -p tcp 2222. On Ubuntu 24.04, sshd is started by ssh.socket, which a generator configures from sshd_config, so a port change takes effect only after sudo systemctl daemon-reload and a restart of ssh.socket.
Restrict root login
Direct root login should be disabled. For elevated access, log in as a normal user and escalate with sudo, which also leaves a record of who did what.
PermitRootLogin no
If some automation needs direct root access (some backup agents, for instance), PermitRootLogin prohibit-password is the compromise: it allows key-based root login and blocks password attempts. It has been OpenSSH’s own default since version 7.0, so no is the stricter choice.
Limit which users can connect
AllowUsers yourusername
This creates an explicit allowlist. Even if another account exists on the system and has SSH access configured, it cannot connect unless it appears here. It is the principle of least privilege, applied at the login.
Disconnect dead sessions
Two settings make sshd notice when a client has gone away:
ClientAliveInterval 300
ClientAliveCountMax 2
After 300 seconds with nothing from the client, sshd sends a keepalive probe through the encrypted channel, and after 2 unanswered probes it disconnects. A client that has vanished, because a laptop went to sleep or a network dropped, is cut off after roughly 10 minutes, and its session stops holding resources on the server.
These settings do not end idle sessions, though they are often described as if they did. A connected client answers every probe automatically, whether or not anyone is at the keyboard, so a terminal left open on a desk stays open indefinitely. For a true idle timeout there are two tools. OpenSSH 9.2 added ChannelTimeout, which closes a channel after a period with no traffic:
ChannelTimeout session=10m
On older servers, setting the shell variable TMOUT (for example TMOUT=600, made read-only in a file under /etc/profile.d/) makes bash log out after ten minutes at an idle prompt.
Disable unused features
Several SSH features serve narrow purposes and widen the attack surface for everyone else:
X11Forwarding no
AllowTcpForwarding no
AllowAgentForwarding no
Upstream OpenSSH already defaults X11 forwarding to off, but several distributions, Debian and Ubuntu among them, ship a configuration that turns it on. TCP and agent forwarding are on by default upstream. If X11 forwarding or SSH tunnelling is in actual use, keep what is needed; otherwise, disable it. Features that are off cannot be exploited.
The man page adds a caution worth keeping in mind: “disabling TCP forwarding does not improve security unless users are also denied shell access,” because a user with a shell can always run a forwarder of their own. The setting removes a convenience; a determined user keeps the capability.
Use stronger key types
When generating new key pairs, prefer Ed25519 over the older RSA:
ssh-keygen -t ed25519 -C "your-label"
Ed25519 keys are shorter and faster, and they rest on more modern elliptic-curve cryptography, the scheme Daniel J. Bernstein and his colleagues published in 2011. OpenSSH added them in version 6.5, in 2014, and since 9.5 ssh-keygen generates them by default. Old RSA-2048 keys still lying around are worth rotating while you are at it.
Apply the changes
After editing /etc/ssh/sshd_config, validate the config before restarting:
sudo sshd -t
sudo systemctl restart sshd
sshd -t exits silently if the config is valid and prints the error if not. Restarting with a broken config file means the daemon does not come back, and every new login fails until someone reaches the console. Sessions already open survive the restart, which is why one should stay open until a fresh login has been tested.
There is one trap sshd -t does not catch. For each keyword, sshd uses the first value it reads, not the last. Debian and Ubuntu begin sshd_config with Include /etc/ssh/sshd_config.d/*.conf, and a drop-in there, such as one written by cloud-init, can set PasswordAuthentication yes before the main file is ever read. The way to know what sshd will actually do is to ask it for its effective configuration:
sudo sshd -T | grep -i passwordauthentication
Security relevance
In the CompTIA Security+ outline (SY0-701), this work sits in Domain 2, Threats, Vulnerabilities, and Mitigations: reducing the attack surface of the primary administrative interface directly limits the paths available for unauthorized access. It also sits in Domain 4, Security Operations, whose first objective covers hardening targets. Configuration hardening is a foundational control in every major security framework.
What it adds up to
None of these steps is novel or complicated, but together they are a clear improvement over the defaults. A system with password authentication off, a non-standard port, no root login, an explicit list of users and dead sessions cleaned up is substantially harder to compromise than one running SSH out of the box.
The cost is close to nothing, and the reason to pay it is the same as in 1995. Ylönen’s university was not attacked through some exotic flaw. Someone listened, and the passwords were there.
Sources
- Ylönen, Tatu. “SSH: Secure Login Connections over the Internet.” Proceedings of the 6th USENIX Security Symposium, 1996.
- OpenBSD. sshd_config(5) and sshd(8) manual pages.
- OpenSSH release notes for versions 7.0, 8.7, 9.2 and 9.5.
- Bernstein, Daniel J., Niels Duif, Tanja Lange, Peter Schwabe and Bo-Yin Yang. “High-speed high-security signatures.” CHES 2011.
- CompTIA. Security+ (SY0-701) Exam Objectives.