Linux Server Hardening: The 20% That Stops 80% of Attacks
Effective Linux hardening isn't a security checklist that makes the machine unusable. It's SSH config, patching, least privilege, and logging.
Most Linux server compromise stories read the same: default SSH credentials, an open firewall, an unpinned package, and a year of no patches. Here is the hardening that stops the common paths, in order of impact.
1. Lock down SSH
SSH is the front door, and attackers know it.
# /etc/ssh/sshd_config
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
AllowUsers deploy admin
MaxAuthTries 3
LoginGraceTime 30
Disable root login before you remove password auth, or you can lock yourself out. Generate a key locally, ssh-copy-id, verify it, then disable passwords. Add UseDNS no and a non-standard but not-obscure port if you want quieter logs.
2. Patch. Promptly. Automatically.
Unpatched software is the #1 root cause in breach reports. On Ubuntu, unattended-upgrades for security classes, with a daily check and notification. On enterprise hosts, feed your vulnerability scanner into a ticketing queue.
apt-get update && apt-get upgrade -y
apt autoremove -y
A server that can't be patched is a server you don't own.
3. Least privilege everywhere
- Root only via
sudo, with a separate admin group, andsudofailures logged. - Services run as their own users, not
nobody, not root. - Database and app users get the minimum grants. Service accounts rotate.
chmodaggressively on config files containing secrets;chmod 600on SSH keys.
useradd -r -m -s /usr/sbin/nologin website
chown -R website:website /srv/website
4. The firewall that's actually enabled
Install ufw, set defaults to deny, and allow only what's needed:
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp # your ssh (or custom port)
ufw allow 80,443/tcp
ufw enable
ufw status verbose
Review the rule set: list listening ports with ss -tlnp and match them against the firewall. Any port listening but not allowed is a finding.
5. Watch the logs, off the box
Fail2ban for SSH brute force, and ship the logs somewhere off-box:
apt install fail2ban
systemctl enable --now fail2ban
Ship journald to a central destination (Loki, Elasticsearch, whatever you already run) instead of a local disk that silently fills. Logs on a machine an attacker controls are not evidence of anything, and a full disk is its own outage.
Verify it, because a config file is not a control
Every item above has a command that proves whether it actually took effect. The ones worth running after any change, and again on a schedule:
sshd -T | grep -E 'permitrootlogin|passwordauthentication' # what sshd really uses
ufw status verbose # is it enabled at all
apt list --upgradable 2>/dev/null | wc -l # how far behind
journalctl -u ssh --since "-15 min" | grep Failed # live brute force?
That first command matters more than the file you edited: sshd -T prints the
effective configuration after includes and distro defaults apply, which is the
only way to know the line you wrote is the line being used. Config files lie
about this regularly.
The firewall one catches the classic failure: rules written carefully, then
never enabled because ufw enable was skipped. Correct but inactive gives you
the comfort without the protection.
Automate the checking, not just the fixing
A hardening posture drifts. New services get deployed, someone opens a port for a demo, a package stops receiving updates. The five checklist items are cheap to verify from outside the machine, so schedule it:
- Run the checks weekly against every host and diff the results.
- Alert when a listening port appears that is not in the firewall, rather than waiting for someone to read a report.
- Track patch age per host, not per fleet. One machine six months behind is invisible in an average.
Treat the result as a pull request. Drift gets a change that fixes it, or an explicit decision to accept it with a reason. Re-applying config every night silently hides the fact that something keeps changing it.
The 20% checklist
- Password auth disabled, root login disabled
- Security updates automated
- Services running as dedicated unprivileged users
- Deny-by-default firewall, verified with
ss -tlnp - Fail2ban + centralized logs
What to skip (mostly)
Mandatory full-disk encryption on cloud images, SELinux lockdowns on every box, and "hardening scanners" with 200 findings that nobody actioned. Start with the five items above; everything else is refinement on top of a sane base.
Summary
Attackers mostly knock on the door, not through the wall. Disable password login, patch promptly, run services unprivileged, deny by default at the firewall, and watch the logs. That's the 20% that stops 80% of the attacks.
Divyesh Parmar
Founder & editor, SDP Clouds
Writes SDP Clouds: hands-on DevOps, Kubernetes, CI/CD, and cloud engineering. Every article comes from work actually done in a terminal, not from rewriting documentation.
More about the author →