Hardening a server
Reduce the attack surface: updates, SSH, firewall and least privilege.
Hardening means removing what attackers could use. Start with the few steps that give the most protection.
- Patch: enable automatic security updates (
unattended-upgradeson Debian and Ubuntu,dnf-automaticon Fedora and RHEL). - SSH: keys only, no root login, a short list of allowed users.
- Firewall: deny by default, open only the ports you serve.
- Least privilege: services run as their own non-root users.
- Remove unused packages and services.
PermitRootLogin no
PasswordAuthentication no
AllowUsers ana deploy
MaxAuthTries 3sudo sshd -t && sudo systemctl reload ssh # test the config, then reloadCarefulKeep your current SSH session open while you change SSH settings, and test a second login before you close it. A mistake can lock you out.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 443/tcp
sudo ufw enableCheck what is exposed
ss -tlnp # what listens
sudo ufw status verbose # what is allowedTools like fail2ban ban IP addresses that fail to log in repeatedly. They help against noise but are not a replacement for key-only login.
Test yourself
Answer all the questions, then check them. Finish with every answer right to mark the lesson as done.