16/11/2026
Linux Server Hardening: SSH, nftables and fail2ban
You spin up a fresh VPS with a public IP. A few minutes later the SSH log is full of login attempts for root, admin and test. Those are bots scanning the whole internet.
You spin up a fresh VPS with a public IP. A few minutes later the SSH log is full of login attempts for root, admin and test. Those are bots scanning the whole internet.
Linux server hardening means closing the door before you bring up the real service. On the ITcorv team we do the same three things on every new server first: key-only SSH, an nftables firewall, and fail2ban for whoever keeps knocking.
This works on Debian 12 and Ubuntu 22.04/24.04. The network in front of the server matters too; if you run MikroTik, see MikroTik security hardening.
What is Linux server hardening?
Linux server hardening means shrinking a server's attack surface: only the services you need stay reachable from outside, logins use keys only, inbound traffic is denied by default, and security updates install themselves. It isn't a one-off task; it's a checklist you run on every new server before anything else.
Before you start: don't lock yourself out
The most important tip in this post: always keep one SSH session open. After every change, test from a second terminal. If it fails, fix it from the open session. If your VPS panel has a console (VNC or serial), know where it is before you begin.
1. A separate user and an SSH key
Don't work as root. Create a user and give it sudo:
adduser deploy
usermod -aG sudo deployOn your laptop, create a key (if you don't have one) and copy it to the server:
ssh-keygen -t ed25519 -C "deploy@laptop"
ssh-copy-id deploy@SERVER_IPCheck that the key login works and sudo works:
ssh deploy@SERVER_IP
sudo whoami # should print rootDon't move on until this works.
2. Lock down SSH: no root, no passwords
Leave /etc/ssh/sshd_config alone. On Debian 12 and recent Ubuntu, files in /etc/ssh/sshd_config.d/ are included. Create a separate one:
/etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 30
X11Forwarding no
AllowUsers deployA few notes:
AllowUsersmeans only this user can log in. Add any other users next to it.- For each option, sshd uses the first value it reads. We named the file
10-so it's read early. Some cloud images ship a file like50-cloud-init.confwithPasswordAuthentication yes, so run the test below.
Check the config before reloading:
sudo sshd -tNo output means it's fine. Then:
sudo systemctl reload sshWe didn't change the port. A different port cuts some bot noise but isn't real security; that comes from keys and the firewall. On Ubuntu 24.04, SSH starts through ssh.socket, so changing the port isn't done in sshd_config alone; read that release's docs first if you want to.
3. A firewall with nftables
nftables is the default framework on Debian 12 and recent Ubuntu. If ufw or firewalld is active, pick one; two firewalls on top of each other is trouble.
sudo apt install nftablesWrite /etc/nftables.conf like this:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto icmp accept
meta l4proto ipv6-icmp accept
tcp dport 22 accept
tcp dport { 80, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}Meaning: all inbound traffic is dropped except replies to connections we opened, loopback, ping, SSH and web. No web server? Remove the 80/443 line.
If your office has a static IP, allow SSH only from it:
tcp dport 22 ip saddr 203.0.113.10 accept(203.0.113.10 is an example; use your own IP.)
Watch out for Docker. If Docker runs on this server, it creates its own iptables/nftables rules, and published container ports go through the forward chain, not input. So a policy drop forward chain can break container networking, and closing a port in input won't block a container's published port. On a Docker host, drop the forward chain from this file and publish container ports on 127.0.0.1:. Also, on a Docker host replace flush ruleset at the top of the file with table inet filter + flush table inet filter, and use nft delete table inet filter in the rollback timer, so you only touch your own table and leave Docker's rules alone.
First, just check it without applying:
sudo nft -c -f /etc/nftables.confA trick so you don't get locked out: set a timer that opens everything again if you lose access:
sudo systemd-run --on-active=5m --unit=nft-rollback /usr/sbin/nft flush ruleset
sudo nft -f /etc/nftables.confNow SSH in from another terminal. If it works, cancel the rollback (sudo systemctl stop nft-rollback.timer) and enable the service so it survives reboots:
sudo systemctl enable --now nftables4. fail2ban against brute-force
With key-only SSH, password guessing basically can't get in, but all those attempts still flood the log and eat resources. fail2ban reads the log and bans an IP that keeps failing for a while.
sudo apt install fail2ban python3-systemdDon't edit jail.conf; create /etc/fail2ban/jail.local instead:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
banaction = nftables-multiport
banaction_allports = nftables-allports
ignoreip = 127.0.0.1/8 ::1
[sshd]
enabled = true
backend = systemdWhy backend = systemd? A default Debian 12 install has no /var/log/auth.log; SSH logs only go to journald. This way fail2ban reads the journal directly. Add your office or VPN IP to ignoreip so you don't ban yourself.
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban5. Automatic security updates
A hardened server that never gets updated isn't hardened a few months later.
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgradesBy default this installs security updates automatically.
Testing
SSH: from another terminal, try a password login:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@SERVER_IPIt should say Permission denied (publickey). Try root too; it should be refused.
Check the config that's actually in effect:
sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication|kbdinteractive|allowusers'You should see permitrootlogin no and passwordauthentication no. If you see yes, another file in sshd_config.d is being read first.
Firewall:
sudo nft list rulesetScan from an outside machine (not the server itself):
nmap -Pn -p 1-1000 SERVER_IPOnly 22 (and 80/443 if you kept them) should be open.
fail2ban:
sudo fail2ban-client status sshdThe jail should be active. To test a ban, ban an example IP by hand and check it shows up in nftables:
sudo fail2ban-client set sshd banip 203.0.113.50
sudo nft list ruleset | grep 203.0.113.50
sudo fail2ban-client set sshd unbanip 203.0.113.50Tips
- Keep a session open. Saying it again; most lockouts come from skipping this.
- If you see Too many authentication failures. An agent holding many keys can hit
MaxAuthTries. Connect with-o IdentitiesOnly=yes -i ~/.ssh/id_ed25519. - Guard your private key. Put a passphrase on it. If a laptop is stolen, remove its key from
~/.ssh/authorized_keyson the server. - Test before you apply. Both
sshd -tandnft -cbefore every change. - Turn off what you don't need. Run
sudo ss -tulpnto see what's listening. Never leave a database open to the internet. - The web app needs its own protection. A host firewall doesn't stop attacks on the site itself; for that see a WAF with nginx and ModSecurity.
- The network edge. Servers behind a corporate firewall? The FortiGate hardening checklist helps too.
- Once isn't enough. Re-check logs, users and open ports every so often.
FAQ
nftables or ufw?
ufw is simpler and common on Ubuntu. nftables gives you more control and its config is easier to read. Whichever you pick, run only that one.
Is fail2ban needed if passwords are off?
Not strictly, but it doesn't hurt. The log stays cleaner, and if you add web or mail later you can turn on jails for those too.
I locked myself out. Now what?
Log in through your VPS panel's console (VNC or serial) and fix the file you broke. For the firewall, nft flush ruleset opens everything. That's why we use the timer and an open session.
Should we do this by hand on dozens of servers?
No. Once you have many servers, you write these settings in Ansible or a similar tool so every machine matches and the config lives in Git.
The ITcorv team handles server and network hardening as part of our network security services, with 24/7 support. For a consultation, reach out on this page.