A fresh VPS is reachable from the entire internet within seconds of provisioning, and bots find it in minutes. This is the sequence we run before handing a server over, ordered so you cannot lock yourself out.
1. Add your key before disabling passwords
Generate a key on your laptop, then append the public half to the server while password login is still on:
ssh-keygen -t ed25519 -C "you@laptop"
ssh-copy-id -i ~/.ssh/id_ed25519.pub root@SERVER_IP
ssh root@SERVER_IP # must work without a password prompt
Do not skip the verification step. Most lockouts happen because the key was added to the wrong user or the permissions were wrong.
2. Harden sshd, keep a session open
# /etc/ssh/sshd_config
PermitRootLogin prohibit-password
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
LoginGraceTime 20
Test the config with sshd -t, reload — not restart — and only close your current session once a second login works. If your provider offers a console, keep it open as a backstop.
3. Firewall: allow SSH first
ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw allow 80,443/tcp
ufw enable
Order matters. Enabling the firewall before allowing SSH is the classic way to lock yourself out of a server you just paid for. If you move SSH to a custom port, allow it before you change the config, and check the provider's firewall too.
4. fail2ban, with sensible jails
apt install fail2ban -y
# /etc/fail2ban/jail.local
[sshd]
enabled = true
maxretry = 4
bantime = 1h
Do not ban yourself: add your office IP to ignoreip. Check with fail2ban-client status sshd.
5. Updates and unattended upgrades
Security patches should not depend on you remembering. Enable unattended upgrades for security only, and reboot in a window you control. Check /var/run/reboot-required weekly.
6. The extras that pay off
- A non-root deploy user with sudo, so day-to-day work is not done as root.
- Time sync —
timedatectl set-ntp true. Certificates and logs depend on it. - Log rotation before the disk fills.
- A backup you restored once, not a backup job you assume works.
- Monitoring that alerts when SSH or HTTP stops answering.
What not to do
Do not change the SSH port and assume that is security. Do not install a random “optimizer” script from a forum. Do not disable SELinux or the firewall to make an app work — fix the app's permissions instead.
If you would rather not
We harden the OS and hand over keys and a checklist as part of managed setup on any KVM VPS. Ask for the first-hour hardening and it is done before you log in.