← All technical notes

Systems / Field notes

Linux server hardening

Published

Security starts with a small, understood attack surface. The goal is not to enable every hardening option, but to make the necessary services predictable and recoverable.

Inventory before changing anything

List active listeners, running services and failed units. Compare them with the server’s intended role. A service listening only on loopback is different from one exposed to every interface, but both still deserve review.

ss -lntup
systemctl --type=service --state=running
systemctl --failed
sudo ufw status verbose
Public network
Firewall
Required service
Local dependencies
A smaller public attack surface makes access policy easier to inspect.

Secure administrative access

Use unique SSH keys, protect the private key with a passphrase and test authentication in a separate session before disabling passwords. Preserve access to a recovery console. Inspect existing configuration fragments because the effective OpenSSH settings may differ from the main file.

sudo sshd -t
sudo sshd -T | grep -E "^(passwordauthentication|pubkeyauthentication|permitrootlogin) "

Prefer an explicit administrative policy, minimal privileges and narrow firewall rules. Tools such as Fail2Ban can reduce repeated abusive authentication attempts, but do not replace key-based authentication or security updates.

Keep updates and recovery boring

Apply security updates, review restart requirements and keep a tested recovery path. A backup is useful only if it can actually be restored. Avoid removing packages solely to reduce the package count: boot, networking and console dependencies deserve particular care.

Review kernel settings selectively

Check active sysctl values and their sources before editing configuration. Disable forwarding on a host that does not route packets, where appropriate. Keep distribution defaults unless the server’s role or a measured problem justifies a change. Routing policies may require different reverse-path filtering settings.

Never make an untested access-control change over your only remote session.

Further reading