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.
Wardterm keeps your servers and keys in an encrypted vault, and can keep a key in the secure chip of your Mac or iPhone, confirmed with Touch ID or Face ID.
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.
Wardterm can record your sessions with passwords hidden, so after an incident you can replay exactly what ran on the server.
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.
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.
Originally published on the Wardterm blog.