Exova Academy

Blog / Cybersecurity in the Age of AI

How to Secure a Linux Server: The Complete 2026 Checklist

October 1, 2026 · 10 min read

To secure a Linux server exposed to the internet, seven areas of work are enough to stop the bulk of automated attacks: automatic updates, key-only SSH access, a default-deny firewall with correctly ordered rules, fail2ban, fewer exposed services, backups out of an attacker's reach and monitoring for signs of compromise. This checklist gives the exact commands for Debian 12 and Ubuntu 24.04, in the order to apply them, using an example server web01 at 203.0.113.10 managed from 198.51.100.7. Plan on two to three hours for a first pass.

Before you start: inventory and safety net

Hardening a remote server carries one main risk: locking yourself out. Take three precautions before changing anything.

  1. Check your hosting provider's console access. It is the only way in that does not go through the network.
  2. Keep an SSH session open for the whole operation and test every change in a second session.
  3. Schedule an automatic firewall rollback before changing it (detailed below).

Then take inventory of what is actually listening:

sudo ss -tulpn
systemctl list-units --type=service --state=running

Every line bound to 0.0.0.0 or [::] raises a question: should this service be reachable from the internet, and by whom? Write the answer down in a traffic matrix. For web01: SSH from the office only, HTTP and HTTPS for everyone, the database for no one.

Step 1: updates and a smaller attack surface

Most successful break-ins exploit a vulnerability that has already been patched. Update the system, then automate security patches:

sudo apt update && sudo apt full-upgrade
sudo apt install unattended-upgrades needrestart
sudo dpkg-reconfigure -plow unattended-upgrades
sudo unattended-upgrade --dry-run --debug

The third command creates /etc/apt/apt.conf.d/20auto-upgrades, which enables daily updates. The last one checks the configuration without installing anything. needrestart flags services that need a restart after a library update. Kernel reboots stay manual: schedule them, otherwise the patch is not active.

Next, remove anything that has no reason to be there:

sudo systemctl disable --now cups avahi-daemon   # examples of services useless on a server
sudo apt purge telnetd rsh-server

For services that do need to run, make them listen locally when they do not need to be reachable: listen_addresses = 'localhost' for PostgreSQL, bind-address = 127.0.0.1 for MariaDB, bind 127.0.0.1 ::1 for Redis. The firewall is a second barrier, not the only one.

Step 2: key-based SSH and least privilege

An exposed server receives thousands of SSH login attempts a day. The goal is not to make password login hard, but impossible.

On your workstation, generate a modern key and copy it over:

ssh-keygen -t ed25519 -a 100 -C "alex@admin-laptop"
ssh-copy-id -i ~/.ssh/id_ed25519.pub alex@203.0.113.10

On the server, create one named account per administrator, then a dedicated config file:

sudo adduser alex
sudo usermod -aG sudo alex
sudo groupadd -f ssh-users && sudo usermod -aG ssh-users alex

sudo tee /etc/ssh/sshd_config.d/00-hardening.conf >/dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 20
X11Forwarding no
AllowGroups ssh-users
EOF

sudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -Ei '^(permitrootlogin|passwordauthentication|allowgroups)'

Two pitfalls to know about:

  • The first value read wins. Cloud images often ship 50-cloud-init.conf with PasswordAuthentication yes. The 00- prefix guarantees your file is read first. Only the output of sshd -T tells you the truth.
  • Forgotten keys. Regularly audit ~/.ssh/authorized_keys for every account, including root: an unknown key is a classic sign of a backdoor.

Finally, apply least privilege: a contractor does not need the sudo group, just a few specific commands declared in a file under /etc/sudoers.d/ (edited with visudo -f).

Step 3: nftables firewall, where rule order matters

nftables is the default packet filtering framework on recent Debian and Ubuntu. An inet table handles IPv4 and IPv6 at the same time. On Ubuntu, do not use ufw alongside a hand-written nftables file: pick one or the other.

Safety net first: back up the current rules and schedule their restoration in five minutes.

sudo apt install at nftables
sudo nft list ruleset | sudo tee /root/rules-ok.nft >/dev/null
echo 'nft flush ruleset; nft -f /root/rules-ok.nft' | sudo at now + 5 minutes

Then the ruleset, in /etc/nftables.conf:

#!/usr/sbin/nft -f
flush ruleset

table inet filter {
  set admins_v4 {
    type ipv4_addr
    elements = { 198.51.100.7 }
  }

  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
    ct state invalid drop
    iif "lo" accept
    ip protocol icmp icmp type { echo-request, destination-unreachable, time-exceeded } limit rate 10/second accept
    ip6 nexthdr icmpv6 accept
    tcp dport 22 ip saddr @admins_v4 accept
    tcp dport { 80, 443 } accept
    limit rate 5/minute log prefix "nft-drop: " level info
  }

  chain forward {
    type filter hook forward priority 0; policy drop;
  }

  chain output {
    type filter hook output priority 0; policy accept;
  }
}
sudo nft -c -f /etc/nftables.conf       # checks the syntax without applying it
sudo systemctl enable nftables
sudo systemctl restart nftables

Test a new SSH connection. If it works, cancel the rollback with sudo atq then sudo atrm <job number>.

%% titre: A packet's path through the input chain
%% narration: In this diagram, an incoming packet goes through the rules from top to bottom. The first matching rule decides; a packet that matches none is logged and then dropped by the default policy.
flowchart TD
  P["Incoming packet"] --> A{"Established connection?"}
  A -- "yes" --> OK["Accepted"]
  A -- "no" --> B{"SSH from admins_v4?"}
  B -- "yes" --> OK
  B -- "no" --> C{"Port 80 or 443?"}
  C -- "yes" --> OK
  C -- "no" --> D["Logged then dropped"]
A packet's path through the input chain

Why order is decisive: rules are evaluated top to bottom and the first one that matches decides. With iptables, still very widespread, -A appends to the end of the chain and -I inserts at the top. A command like iptables -I INPUT -p tcp --dport 5432 -j DROP, typed to block a suspicious IP but without specifying the source, lands above the rules that allowed your application servers: the database becomes unreachable for everyone. Before and after every change, read the numbered chain:

sudo iptables -L INPUT -n -v --line-numbers

An ACCEPT rule whose counter stays at zero while the service is in use means a rule above it is short-circuiting it. To block a specific source, target it and choose the position: sudo iptables -I INPUT 2 -s 198.51.100.230 -p tcp --dport 5432 -j DROP.

Docker pitfall: a container published with -p 5432:5432 is reachable despite your firewall, because Docker inserts its own rules. Publish on 127.0.0.1:5432:5432 or filter in the DOCKER-USER chain.

To apply this checklist to a complete case, including compromise detection and incident response: Explore the "Cybersecurity in the Age of AI" course

Step 4: fail2ban against brute force

Even with key-based SSH, fail2ban cuts the noise and protects services that still accept passwords (webmail, admin panels, SIP). On Debian 12, SSH logs live in journald rather than /var/log/auth.log, so you need the systemd backend.

sudo apt install fail2ban python3-systemd
sudo tee /etc/fail2ban/jail.local >/dev/null <<'EOF'
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
banaction = nftables-multiport
banaction_allports = nftables-allports
ignoreip = 127.0.0.1/8 ::1 198.51.100.7

[sshd]
enabled = true
backend = systemd

[recidive]
enabled  = true
bantime  = 1w
findtime = 1d
EOF
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

Never edit jail.conf, which gets overwritten on updates. Always add your own IPs to ignoreip. The recidive jail bans addresses that have already been banned several times for a week. To release an address banned by mistake: sudo fail2ban-client set sshd unbanip 198.51.100.7.

Step 5: backups out of reach

A backup that helps against ransomware starts from one assumption: the attacker is root on the server and can read everything, including your backup scripts and credentials. If those credentials can delete the copies, you do not have a backup. Apply the 3-2-1 rule: three copies, two types of media, one off-site, with at least one immutable or offline.

%% titre: Backups that survive ransomware
%% narration: In this diagram, the server sends its backups to an append-only repository at another hosting provider. An immutable copy is added there, and a monthly restore test checks that everything can be recovered.
flowchart LR
  S["web01: files and dumps"] -- "restic, append only" --> B["Off-site backup server"]
  B --> I["30-day immutable copy"]
  B -. "monthly test" .-> T["Verified restore"]
Backups that survive ransomware

With restic pointed at a rest-server started in --append-only mode, the production server can add backups but not delete them:

# run as root (sudo -i)
apt install restic
sudo -u postgres pg_dump -Fc app > /var/backups/app.dump
export RESTIC_REPOSITORY=rest:https://web01:PASSWORD@backup01.example.com:8000/web01
export RESTIC_PASSWORD_FILE=/root/.restic-pass
restic init                                   # once only
restic backup /etc /var/www /var/backups
restic check

Pruning (restic forget --keep-daily 7 --keep-weekly 4 --keep-monthly 6 --prune) runs from the backup server itself, never from production. Above all, test a restore every month: restic restore latest --target /tmp/restore-test, then check that the dump loads into a test database. A backup that has never been restored is just a hypothesis.

Step 6: monitoring and compromise detection

Hardening is not enough: you need to see when something gets through. The signs that should raise an alarm are a CPU at 100% for no reason (crypto miner), outbound connections to unusual ports, a service or cron job nobody created, an unknown SSH key.

Quick check commands:

journalctl -u ssh --since "24 hours ago" | grep -E "Accepted|Failed"
last -n 20
ps aux --sort=-%cpu | head -n 10
sudo ss -tnp state established
systemctl list-timers --all
for u in $(cut -d: -f1 /etc/passwd); do sudo crontab -l -u "$u" 2>/dev/null | sed "s/^/$u: /"; done
sudo find /etc/systemd/system /etc/cron* -mtime -7 -type f

For lasting monitoring, install auditd and a file integrity database:

sudo apt install auditd aide
echo '-w /etc/ssh/sshd_config -p wa -k sshd
-w /etc/passwd -p wa -k identity
-w /root/.ssh -p wa -k root_keys' | sudo tee /etc/audit/rules.d/hardening.rules
sudo augenrules --load
sudo ausearch -k root_keys

sudo aideinit                       # creates the baseline database
sudo aide --check --config /etc/aide/aide.conf

Send these alerts somewhere someone actually reads them: a daily email, a team chat or a tool like Wazuh if you manage several servers. An alert nobody looks at does not exist.

Linux server security checklist: the summary

Control Check command Expected result
Automatic updates cat /etc/apt/apt.conf.d/20auto-upgrades Two lines set to "1"
Root login disabled sudo sshd -T, permitrootlogin line no
SSH passwords refused sudo sshd -T, passwordauthentication line no
Firewall active sudo nft list ruleset drop policy on input
Exposed ports justified sudo ss -tulpn Only the planned services
fail2ban active sudo fail2ban-client status sshd and recidive jails
Recent, healthy backup restic snapshots then restic check Snapshot under 24 h old, no errors
Restore tested Test log Within the last 30 days
Auditing active sudo auditctl -l Rules loaded
File integrity sudo aide --check --config /etc/aide/aide.conf No unexplained changes

Go through this list every quarter and after every significant change.

Frequently asked questions

Does changing the SSH port improve security?

Very little. It reduces log noise, but a scan finds the new port in seconds. Key authentication, IP restrictions at the firewall and fail2ban are what actually protect you. On Ubuntu 24.04, SSH is socket-activated by systemd: a port change is made in ssh.socket, not just in sshd_config.

nftables or iptables: which should I use?

nftables for any new installation: it is the default framework on Debian and Ubuntu, more readable and unified for IPv4 and IPv6. On an existing iptables server, master rule ordering before you migrate.

Is fail2ban useful if SSH only accepts keys?

Yes, to a lesser degree: it lightens the logs and mostly helps for other exposed services that still accept passwords. Remember to add your own IPs to ignoreip.

How long does it take to harden a server?

Two to three hours for a complete first pass with this checklist, then an hour per quarter for the review. The longest part is often setting up and testing backups.

Conclusion

Securing a Linux server is a series of simple steps, as long as you do them in the right order and verify each one: updates, key-based SSH, a default-deny firewall with ordered rules, fail2ban, backups out of reach and monitoring that someone actually reads. Hardening is never finished: schedule your quarterly review today.

Read the first lesson for free: The 2026 threat landscape: what AI has changed.

Explore the "Cybersecurity in the Age of AI" course