A Practical Guide to Hardening a Linux Server
From automatic updates and key-only SSH to a default-deny firewall, logging and tested backups: the baseline steps for securing a Linux server.
Key takeaways
- Three high-impact moves: regular patching, key-only SSH and a default-deny firewall.
- Disable every service you do not need; a closed port is not an attack surface.
- A backup whose restore has not been tested is not a backup.
A new server on the internet sees its first automated login attempts within minutes. Hardening means shrinking the attack surface through a set of simple, repeatable steps. The commands here target Debian and Ubuntu; other distributions differ slightly in package and service names. Always keep a second SSH session open while you work so you do not lock yourself out.
1. Patching and automatic updates
Most successful intrusions exploit known, unpatched vulnerabilities. Automate security updates and check on them periodically.
sudo apt update && sudo apt upgrade -y
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
sudo unattended-upgrade --dry-run --debug
2. Key-only SSH
First install your public key on the server (ssh-copy-id) and confirm that key login works. Only then disable passwords. On current Ubuntu and Debian, files in sshd_config.d are read early and the first value obtained for each option wins, so give your file a low number to stop files such as 50-cloud-init.conf from overriding it.
# /etc/ssh/sshd_config.d/10-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
AllowUsers deploy
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
sudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -E 'passwordauthentication|permitrootlogin'
3. A default-deny firewall
The principle is to close everything and open only what is needed. ufw is the simplest route; the important detail is to add the SSH rule before enabling it. The limit action throttles repeated connection attempts.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
If you prefer nftables, the equivalent looks like this:
# /etc/nftables.conf
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
iif lo accept
ct state established,related accept
ct state invalid drop
ip protocol icmp accept
ip6 nexthdr ipv6-icmp accept
tcp dport { 22, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
One caveat: Docker writes its own rules directly into iptables, so published container ports may bypass ufw. Audit them separately.
4. Rate-limiting login attempts
With key-only login, password guessing is pointless, but fail2ban still cuts log noise and server load, and it protects other services too.
# /etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
5. Least-privilege users and sudo
- Give every person a separate account; avoid shared logins.
- Disable direct root login and use sudo so actions are logged.
- Run services under dedicated users without a login shell.
- Write sudo rules precisely with visudo rather than granting ALL.
6. Minimize services, then watch
See what is listening on which port and disable what you do not need. Then monitor logs and changes to sensitive files.
sudo ss -tulpn
systemctl list-unit-files --state=enabled
sudo systemctl disable --now SERVICE_NAME
sudo journalctl -u ssh --since today
sudo apt install -y auditd
7. Backups and restore tests
Backups should live off the server, be encrypted and, where possible, immutable. A tool such as restic makes this easy. More important than taking backups is restoring them regularly into a scratch location.
restic -r /srv/backup/repo backup /etc /var/www
restic -r /srv/backup/repo check
restic -r /srv/backup/repo restore latest --target /tmp/restore-test
Verification checklist
- Automatic updates are enabled and the dry run reports no errors.
- sshd -T shows password and root login disabled.
- ufw or nftables has a default-deny policy with only required ports open.
- ss -tulpn shows no unknown listeners.
- Each admin has an individual account and sudo use is logged.
- The last successful test restore is recorded with a date.
This article is sample educational content; verify against official sources before making important decisions.