appzasGamesToolsDevLearnCheat sheetsQuick calcsConvertersReferenceNetworkTimeCalculatorsCompareLLM prices

Hardening a server

Lesson 3 of 412 minAdvanced

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.

  1. Patch: enable automatic security updates (unattended-upgrades on Debian and Ubuntu, dnf-automatic on Fedora and RHEL).
  2. SSH: keys only, no root login, a short list of allowed users.
  3. Firewall: deny by default, open only the ports you serve.
  4. Least privilege: services run as their own non-root users.
  5. Remove unused packages and services.
/etc/ssh/sshd_config (excerpt)
PermitRootLogin no
PasswordAuthentication no
AllowUsers ana deploy
MaxAuthTries 3
sudo sshd -t && sudo systemctl reload ssh   # test the config, then reload

CarefulKeep 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.

Firewall with ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw allow 443/tcp
sudo ufw enable

Check what is exposed

ss -tlnp                 # what listens
sudo ufw status verbose  # what is allowed

Tools 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.

1. Which SSH setting stops password guessing entirely?
2. What is the safest firewall default for incoming traffic?
Hintsshd with the test flag.

Key terms