How to secure a Linux VPS: a practical checklist
The steps that stop most attacks on a new Linux server, with the commands, and how to check each one actually worked.
A new server on a public IP address starts receiving SSH login attempts within minutes. Almost all of them are automated and almost all of them fail, as long as a few basics are in place. This checklist covers those basics for Debian and Ubuntu; the ideas are the same on other distributions.
Keep your current SSH session open while you change SSH or firewall settings, and test from a second terminal. That way a mistake doesn’t lock you out.
1. Install updates, and keep installing them
sudo apt update && sudo apt upgrade
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgradesAutomatic security updates close known holes without anyone remembering to log in. Reboot when a kernel update asks for it.
2. Log in as a normal user with an SSH key
sudo adduser deploy
sudo usermod -aG sudo deploy
# from your own computer:
ssh-copy-id deploy@your-serverCheck that you can log in as that user with the key before going further.
3. Turn off password and root logins
Put your settings in a drop-in file so package updates don’t overwrite them:
# /etc/ssh/sshd_config.d/10-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin nosudo sshd -t && sudo systemctl reload ssh
sudo sshd -T | grep -E "passwordauthentication|permitrootlogin"The last command prints the settings sshd is really using. If it still says yes, another file is setting the value first: sshd uses the first value it reads, so check the other files in sshd_config.d. On RHEL-based systems the service is called sshd instead of ssh. SSH hardening in more depth →
4. Close every port you don’t need
sudo ss -tulpn
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 443/tcp # only if this server serves HTTPS
sudo ufw enabless shows what is listening. Anything listening on 0.0.0.0 that only needs to be reached locally, such as a database, should listen on 127.0.0.1 instead.
Running Docker? Ports published with -p are opened by Docker’s own firewall rules and are reachable even when ufw says they are blocked. Publish to 127.0.0.1 (for example -p 127.0.0.1:5432:5432) unless the port really should be public.
5. Block brute-force attempts
# /etc/fail2ban/jail.local
[sshd]
enabled = true
backend = systemdsudo apt install fail2ban
sudo systemctl restart fail2ban
sudo fail2ban-client status sshdbackend = systemd matters on systems that only log to the journal, which is the default on recent Debian. Without it the sshd jail has no log file to read. The status command should show a non-zero “Total failed” count within a day on any public server; if it stays at zero, the jail isn’t seeing your logs. Alternatives to fail2ban →
6. Keep backups somewhere else
A backup stored on the same server disappears with it. Copy backups to another provider or account, and restore one now and then to prove it works.
7. Watch for what gets through
Everything above lowers the odds of a break-in; none of it tells you if one happens. At minimum, look at recent logins and new listening ports from time to time:
last -a | head
sudo journalctl -u ssh --since "24 hours ago" | grep Accepted
sudo ss -tulpnPast a handful of servers, that turns into a job nobody has time for. A host-based intrusion detection system does the watching for you. What a HIDS does →
The short version
| Step | Check it worked |
|---|---|
| Automatic updates | systemctl list-timers apt-daily-upgrade shows a next run |
| Key-only SSH, no root | sshd -T shows passwordauthentication no and permitrootlogin no |
| Firewall | ufw status lists only the ports you meant to open |
| Brute-force blocking | fail2ban-client status sshd shows failed attempts being counted |
| Backups | You restored one recently |
| Monitoring | You would know about a new login today, not next month |
Vigilhold, our host agent for Linux servers, covers step 7 across a whole fleet: it watches logins, processes, connections and file changes on every server, and warns you if a server with SSH open stops reporting login activity, which is how a broken setup like a blind fail2ban jail usually shows up. See Vigilhold →