The Linux server security checklist
A new VPS gets its first SSH login attempts within minutes of booting. Do these in order, on Ubuntu or Debian; the commands are similar on other distributions.
- Install updates.
- Turn on automatic security updates.
- Create a user with sudo.
- Sign in with an SSH key.
- Turn off root login.
- Turn off password login.
- Turn on a firewall.
- Install fail2ban.
- Close services you do not need.
- Read the login log.
- Set up backups, and test a restore.
- Protect the keys on your own devices.
Steps 1 and 2: how do you keep the server updated?
Install what is waiting, then let security fixes install themselves:
sudo apt update && sudo apt upgrade -y
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
Answer Yes. Security updates now install daily. When a kernel update arrives, /var/run/reboot-required appears; reboot at a quiet time.
Steps 3 and 4: a sudo user and an SSH key
Daily work happens as a normal user, never as root:
adduser deploy
usermod -aG sudo deploy
From your own computer, copy your public key to the new user:
ssh-copy-id deploy@203.0.113.10
ssh deploy@203.0.113.10 # should not ask for a password
No key yet? ssh-keygen -t ed25519 makes one; our guide to generating an ed25519 key explains the passphrase. For the user setup in detail, see creating an SSH user with sudo.
Steps 5 and 6: how do you secure SSH on a new server?
Put your changes in a drop-in file, so package updates never overwrite them:
sudo tee /etc/ssh/sshd_config.d/10-hardening.conf >/dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
EOF
sudo sshd -t && sudo systemctl reload ssh
On some systems the service is called sshd instead of ssh. Before you close your session, open a second terminal and check that you can still sign in. If you cannot, fix it from the first one.
Check what the server really uses, since the first value read wins:
sudo sshd -T | grep -Ei 'permitrootlogin|passwordauthentication'
More settings worth a look are in our sshd_config hardening guide and the guide to disabling password login.
Steps 7 and 8: a firewall and fail2ban
Allow SSH first, then turn the firewall on:
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp # only if it serves websites
sudo ufw enable
sudo ufw status verbose
Your cloud provider may have its own firewall too; keep the two in step. See allow SSH through UFW.
fail2ban reads the log and blocks addresses that keep failing:
sudo apt install -y fail2ban
sudo tee /etc/fail2ban/jail.local >/dev/null <<'EOF'
[sshd]
enabled = true
maxretry = 5
bantime = 1h
EOF
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
Our fail2ban guide covers longer bans for repeat visitors.
Steps 9 and 10: what is running, and who is knocking?
List every port that listens, and the program behind it:
sudo ss -tulpn
Anything you do not recognise, stop it or bind it to 127.0.0.1. A database should never listen on the public address; reach it through an SSH tunnel instead (see database through an SSH tunnel).
Then look at the log once in a while:
sudo journalctl -u ssh --since "24 hours ago" | grep -c "Invalid user"
last -n 10 # recent successful logins
sudo lastb -n 10 # recent failed logins
Thousands of failed attempts are normal noise. A successful login you do not recognise is not.
Step 11: why do backups belong on a security checklist?
Because the most common disaster is not an attacker. It is a wrong rm, a failed disk or a bad migration. A backup counts only if you have restored from it once.
- Turn on your provider's snapshots for the whole disk.
- Back up data, such as databases and uploads, to another place, for example with
resticorborg. - Restore one file today, on purpose, so you know how.
To make the wrong command less likely in the first place, make production look different. Mark the server as Production in Wardterm and every session on it carries a clear label, so you always know where you are typing. Our guide to avoiding mistakes on production servers has more habits that help.
See what ran, and when
Wardterm can record your sessions with passwords hidden, so after an incident you can replay exactly what ran on the server.
The Free plan costs nothing. Pro is €6.99 or $8.49 a month before tax, and you can try it for 14 days with no card.
Step 12: how do you protect the keys on your own devices?
A hardened server is only as safe as the laptop that holds its key. Three habits:
- One key per device. When a laptop is lost, remove its line from
authorized_keysand nothing else changes. See revoking an SSH key. - A passphrase or a secure chip. A key in the Secure Enclave cannot be copied off the device at all; each use is confirmed with Touch ID or Face ID.
- Check the fingerprint on first connect. Compare it with the one your provider shows in its console.
Wardterm does all three: it keeps keys in an encrypted vault, can create a key inside the secure chip of your Mac, iPhone or iPad, and shows the server's fingerprint the first time you connect. See keys on this device and server fingerprints.
What does the Linux server security checklist leave out?
These twelve steps cover the ground every server needs. Beyond them, depending on what you run: TLS for websites, a web application firewall, SSH certificates for larger teams (see SSH certificates), and session recording for audits. Start with the twelve; they stop the attacks that actually happen to a new VPS.
Manage the servers you just secured
Add your hardened servers to Wardterm, sign in with a key from the secure chip, and see stats and containers next to the terminal. Free to start.
The Free plan costs nothing. Pro is €6.99 or $8.49 a month before tax, and you can try it for 14 days with no card.
Servers6
deploy@10.20.0.11Productiondeploy@10.20.0.12Productiondeploy@10.30.0.11Productiondeploy@10.30.0.12Productionredis@cache-eu.internalci@192.168.4.20sudo systemctl reload nginx
Questions
What is the first thing to do on a new VPS?
Update the system with sudo apt update && sudo apt upgrade, then create a normal user with sudo and copy your SSH key to it. Everything else builds on that.
Should I disable root login over SSH?
Yes. Sign in as a normal user and use sudo. Set PermitRootLogin no, and keep a second session open while you test the change.
Should I change the SSH port?
It cuts the noise in your logs but does not stop a real attacker. Keys only, no root login and fail2ban do the real work. If you change it, update the firewall first. See how to change the SSH port.
Is fail2ban still needed with key-only login?
It is not strictly needed, since a password guess cannot succeed. It still keeps logs clean and blocks scanners early, and it protects other services such as a web login.
How do I check who tried to log in to my server?
Run sudo journalctl -u ssh --since today for attempts, last for successful logins and sudo lastb for failed ones. Our guide on checking SSH login attempts explains the output.
How do I avoid locking myself out when hardening SSH?
Keep your current session open, test the configuration with sudo sshd -t, reload SSH, then open a second session to check you can still sign in before you close the first.
Do I need two-factor authentication for SSH?
Keys already count as a strong factor. For extra protection, use a key that needs a touch, such as a YubiKey, or a key in the secure chip of your device confirmed with Touch ID or Face ID.
How often should I update a Linux server?
Security updates should install automatically every day with unattended-upgrades. Review other updates weekly, and reboot when the kernel changes.
Sources
Each page was read on the date shown. Prices and features change, so check the maker's page before you decide.
- Ubuntu Server docs, automatic updates, read on
- OpenBSD manual, sshd_config, read on
- Ubuntu manual, ufw, read on
- fail2ban, source repository, read on
- Debian wiki, unattended upgrades, read on