A new server on the internet is found by automated scanners within minutes. These steps block the most common attacks. They use Ubuntu and Debian commands; AlmaLinux and Rocky Linux equivalents are given where they differ.
With a VPS, the software on the server is yours to manage, so it is worth doing this on day one.
1. Update everything
- Ubuntu/Debian:
apt update && apt upgrade -y - AlmaLinux/Rocky:
dnf upgrade -y
2. Create your own user
Working as root all the time is risky.
adduser yourname
usermod -aG sudo yourname # AlmaLinux/Rocky: usermod -aG wheel yourname
3. Use SSH keys
Set up key login for your new user (see Use SSH keys instead of a password). Check that you can log in with the key before the next step.
4. Turn off password and root logins
Edit /etc/ssh/sshd_config and set:
PermitRootLogin no
PasswordAuthentication no
Then restart SSH: systemctl restart ssh (AlmaLinux/Rocky: systemctl restart sshd). Keep your current session open and test a new login in a second window.
5. Turn on a firewall
Ubuntu/Debian:
ufw allow OpenSSH
ufw allow 80,443/tcp
ufw enable
AlmaLinux/Rocky use firewalld: firewall-cmd --permanent --add-service={ssh,http,https} && firewall-cmd --reload.
6. Block repeated login attempts
Install fail2ban: apt install fail2ban -y (AlmaLinux/Rocky: from EPEL). Its default settings already protect SSH.
7. Keep it updated
Turn on automatic security updates (apt install unattended-upgrades on Ubuntu/Debian) and check in every month.
If you lock yourself out, the Console in your VPS control panel still works (see Start, stop, reboot and use the console).