Skip to content
All posts

A practical Linux server hardening checklist (2026)

Lock down a new Ubuntu or Debian server in an hour: SSH keys only, a firewall that Docker can't bypass, fail2ban, automatic updates and an open-port audit.

Polin Khan

9 min read

A fresh VPS gets its first SSH login attempts within minutes of booting. Most of them are bots trying root with a list of common passwords, and most servers survive them. The ones that don't usually fell for one of a handful of mistakes: password logins left on, a database port open to the world, or a firewall that Docker quietly walked around.

This is the checklist I run on every new server before anything else goes on it. It's written for Ubuntu 24.04 and Debian 12, but the ideas carry over to any distribution. Do the steps in order: each one assumes the one before it worked.

1. Update everything first

Old packages are the cheapest way in. Update before you do anything else, and reboot if the kernel changed.

Update the systemBash
sudo apt update && sudo apt full-upgrade -y
# Reboot if a new kernel or core library was installed
[ -f /var/run/reboot-required ] && sudo reboot

2. Create a normal user and stop working as root

Day-to-day work should happen as a normal user that uses sudo when it needs to. If that account is ever compromised, the attacker still has to get past sudo, and every privileged command lands in the logs.

Create an admin userBash
sudo adduser deploy
sudo usermod -aG sudo deploy

Copy your SSH public key to the new user from your own machine:

On your laptopBash
ssh-copy-id -i ~/.ssh/id_ed25519.pub deploy@your-server

If you don't have a key yet, create one with ssh-keygen -t ed25519. Ed25519 keys are short, fast and supported everywhere that matters. Log in as deploy in a new terminal and confirm sudo -v works before moving on.

3. Lock down SSH

The goal is simple: only keys, never passwords, and never root. Put your settings in a drop-in file rather than editing the main config, so package upgrades never overwrite them.

/etc/ssh/sshd_config.d/00-hardening.confBash
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
AuthenticationMethods publickey
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deploy

Two details catch people out.

The first value wins. sshd uses the first value it reads for each setting, and the drop-in folder is included near the top of the main file. Cloud images often ship a file such as 50-cloud-init.conf that sets PasswordAuthentication yes. Naming your file 00-… makes it load first. Don't guess: ask sshd what it will actually use.

Check the effective settingsBash
sudo sshd -t   # syntax check; prints nothing when the config is valid
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication|kbdinteractive|allowusers'

Ubuntu 24.04 starts SSH from a socket. The listening port comes from ssh.socket, which is generated from your config. Restarting ssh.service alone does not pick up a port change. Reload systemd and restart the socket instead:

Apply the changesBash
sudo systemctl daemon-reload
sudo systemctl restart ssh.socket ssh.service

Now test from a new terminal: ssh deploy@your-server should work, and ssh root@your-server and a password login should both be refused.

4. Turn on a firewall, and know what Docker does to it

Allow only what the server needs to offer. For a typical web server, that's SSH, HTTP and HTTPS.

Basic ufw rulesBash
sudo apt install -y ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH        # allow SSH, but slow down repeated connections
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose

Add the SSH rule before ufw enable, or you'll cut off your own session.

Docker bypasses ufw. When you publish a container port with -p 5432:5432, Docker writes its own iptables rules that are evaluated before ufw's. The port is open to the whole internet even though ufw status doesn't list it. I've found exposed databases on servers this way more than once.

The simplest fix is to never publish internal services on all interfaces. Bind them to localhost and put a reverse proxy in front of anything public:

compose.yamlYAML
services:
  db:
    image: postgres:17
    ports:
      - "127.0.0.1:5432:5432" # reachable from this server only

Better still, don't publish the database port at all, and let the other containers reach it over the Compose network by its service name.

5. Ban brute-force attempts with fail2ban

Keys-only SSH already stops password guessing. fail2ban adds a second layer: it watches the logs and bans IPs that keep failing, which keeps logs readable and stops scanners from wasting your CPU.

Install fail2banBash
sudo apt install -y fail2ban

On Ubuntu 24.04 and Debian 12, the package already enables the sshd jail and reads the systemd journal. Don't copy all of jail.conf; put only your changes in jail.local:

/etc/fail2ban/jail.localini
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
# Never ban yourself: add your office or VPN address here
ignoreip = 127.0.0.1/8 ::1
 
[sshd]
enabled = true

Old tutorials often add backend = auto here. On current Ubuntu that overrides the working journal backend and the jail silently stops matching anything, so leave it out.

Check it worksBash
sudo fail2ban-client -t
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

6. Install security updates automatically

Most real-world break-ins use vulnerabilities that already had a patch. Automatic security updates close that gap without you having to remember.

Enable unattended upgradesBash
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

By default this installs security updates only, which is the right balance for a server. To reboot automatically when a kernel update needs it, set a quiet time in /etc/apt/apt.conf.d/50unattended-upgrades:

/etc/apt/apt.conf.d/50unattended-upgradestext
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";

On Fedora, RHEL and similar systems, dnf-automatic does the same job.

7. Audit what's actually listening

A firewall rule is only half the story. Check which processes are listening, and on which addresses:

List listening portsBash
sudo ss -tulpn

Read the Local Address column:

  • 127.0.0.1:… or [::1]:… is local only. Good for databases, caches and app servers behind a proxy.
  • 0.0.0.0:… or *:… or [::]:… listens on every interface. Each of these should be something you meant to expose.

Anything you don't recognise, look up with ps using the PID that ss shows, then remove it or bind it to localhost. Repeat this check after installing anything new. Then confirm from outside, from your laptop or another server:

Scan your own server from outsideBash
nmap -Pn your-server

Only run scans against servers you own or have permission to test.

8. Run apps with the least privilege they need

  • Give each app its own system user with no login shell, and never run it as root.
  • If you run apps with systemd, its sandboxing options are cheap wins: NoNewPrivileges=yes, ProtectSystem=strict, ProtectHome=yes and PrivateTmp=yes. Run systemd-analyze security your-app.service to get a score and a list of what's still open.
  • Keep secrets in files readable only by that user (chmod 600), not in your shell history or a world-readable .env.
  • Remove packages and services you don't use. Software that isn't installed can't be exploited.

9. Put the server behind Cloudflare (or another proxy) properly

A proxy such as Cloudflare hides your origin IP, absorbs floods and terminates TLS. That only helps if attackers can't skip it and hit the origin directly. Once traffic flows through the proxy, allow ports 80 and 443 only from the proxy's address ranges. Cloudflare publishes its ranges at https://www.cloudflare.com/ips-v4 and https://www.cloudflare.com/ips-v6.

Allow web traffic only from CloudflareBash
for ip in $(curl -s https://www.cloudflare.com/ips-v4) $(curl -s https://www.cloudflare.com/ips-v6); do
  sudo ufw allow from "$ip" to any port 80,443 proto tcp
done
sudo ufw delete allow 80,443/tcp

The ranges change occasionally, so re-run this from a monthly cron job or a deploy step. If your origin IP was ever public, consider moving to a new one after the switch, because old DNS records stay in public history.

10. Keep an eye on it

Hardening is a starting point, not a one-time job. A few habits catch most problems early:

  • Read the auth log now and then: journalctl -u ssh --since "24 hours ago".
  • Install auditd if you need a record of who changed what. Rules on /etc/passwd, /etc/sudoers and /etc/ssh/ are a good start.
  • Run Lynis (sudo lynis audit system) for a free, thorough second opinion with concrete suggestions.
  • Send logs and metrics somewhere off the server. An attacker who gets in will try to clean up local logs first.
  • Back up, and test a restore. Hardening lowers the odds of a bad day; backups decide how bad it gets.

The checklist

  • System fully updated, rebooted if needed
  • Non-root sudo user with an SSH key
  • SSH: no root, no passwords, AllowUsers, checked with sshd -T
  • Firewall on, only SSH, 80 and 443 open
  • No Docker ports published on 0.0.0.0 by accident
  • fail2ban running, sshd jail active
  • Automatic security updates enabled
  • ss -tulpn shows nothing unexpected; external scan agrees
  • Apps run as their own unprivileged users
  • Origin only accepts web traffic from the proxy
  • Logs leave the server, and backups restore

FAQ

What is the most important step in hardening a Linux server?

Turning off SSH password login and root login, so only keys work. Most automated attacks are password guessing, and keys-only login makes them useless. Check that it's really in effect with sudo sshd -T.

Why is my port open when ufw says it's blocked?

If the service runs in Docker with a published port, Docker's iptables rules are evaluated before ufw's, so the port is open regardless of ufw. Bind the port to 127.0.0.1 in your Compose file, or don't publish it at all.

Do I still need fail2ban if SSH only accepts keys?

It's not strictly required, since keys-only login already stops password guessing. It still helps: it keeps your logs readable, cuts wasted CPU from scanners, and can protect other services such as Nginx or mail with their own jails.

Is changing the SSH port worth it?

It reduces log noise from bots, but it doesn't stop a targeted attacker, who will find the new port with a quick scan. Treat it as optional; keys-only login is what protects you.

How often should I re-check a hardened server?

Re-run the open-port audit after every new install, and do a broader review, such as a Lynis run, every few months. Automatic security updates handle the day-to-day patching.