Securing VPS/Servers

VPS Hardening Checklist: 10 Steps, Each With a Verification Command

Secure a Linux VPS in ten steps: updates, SSH keys, firewall, exposed services, fail2ban, SELinux or AppArmor, admin panels and an audit, with a command that proves each one.

Most compromised servers were not attacked by anyone in particular. Automated scanners found a password login, an unpatched service or a database open to the internet. This checklist closes those doors on a Linux VPS. Every step has a command that proves it worked, because a setting you have not verified is a setting you hope is there.

Commands are given for Ubuntu 26.04/24.04 and Debian 13 (apt) and for AlmaLinux and Rocky Linux 9 and 10 (dnf). They are standard tools from each distribution; this guide is a reference and was not run on a test server for this article.

Before you start: take a provider snapshot, and keep one SSH session open while you change SSH or firewall settings. Test each change from a second terminal. If you do lock yourself out, the provider's web console still works.

1. Patch the system and keep it patched

# Ubuntu, Debian
sudo apt update && sudo apt full-upgrade
sudo apt install unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

# AlmaLinux, Rocky Linux
sudo dnf upgrade
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer

On AlmaLinux and Rocky Linux set apply_updates = yes in /etc/dnf/automatic.conf, otherwise updates are only downloaded.

Verify: systemctl list-timers | grep -E "apt-daily-upgrade|dnf-automatic" shows a next run time.

2. SSH: keys only, no root

Create a normal user with sudo rights and install your public key for it (the initial server setup guide has the steps). Confirm that key login works, then create /etc/ssh/sshd_config.d/00-hardening.conf:

PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin no
AllowUsers deploy

Replace deploy with your user. AllowUsers refuses every other account, including service accounts with weak passwords you did not know existed.

sudo sshd -t && sudo systemctl reload ssh     # sshd on AlmaLinux, Rocky Linux

Verify:

sudo sshd -T | grep -E "^(passwordauthentication|permitrootlogin|allowusers)"
# from your own computer, this must be refused:
ssh -o PubkeyAuthentication=no -o PreferredAuthentications=password deploy@YOUR_SERVER_IP

Changing the SSH port reduces log noise and nothing else. It is optional, and it is not a substitute for keys.

3. Firewall: default deny

# Ubuntu, Debian
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable

# AlmaLinux, Rocky Linux
sudo systemctl enable --now firewalld
sudo firewall-cmd --permanent --add-service=ssh --add-service=http --add-service=https
sudo firewall-cmd --reload

If your provider offers a cloud firewall, use it as well. It filters traffic before it reaches the VPS and is not affected by Docker, which bypasses ufw for published container ports.

Verify from outside, not from the server. On another machine:

nmap -Pn YOUR_SERVER_IP

Only the ports you allowed should be open. Scan your own servers only.

4. Nothing listens that should not

sudo ss -tulpn

Read the "Local Address" column. Databases (3306, 5432), Redis (6379), Memcached (11211) and admin tools should show 127.0.0.1 or [::1]. If one shows 0.0.0.0 or *, bind it to localhost in its configuration. Remove services you do not use:

systemctl list-unit-files --type=service --state=enabled
sudo systemctl disable --now SERVICE_NAME

5. Slow down password guessing

With password login off, guessing cannot succeed, but it still fills logs and can hit other services. Fail2ban blocks addresses that fail repeatedly.

# Ubuntu, Debian
sudo apt install fail2ban

# AlmaLinux, Rocky Linux (from the EPEL repository)
sudo dnf install epel-release
sudo dnf install fail2ban

Create /etc/fail2ban/jail.local:

[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
sudo systemctl enable --now fail2ban

Verify: sudo fail2ban-client status sshd lists the jail and, after a while on a public server, banned addresses.

6. Keep the built-in protections on

getenforce            # AlmaLinux, Rocky Linux: should print Enforcing
sudo aa-status        # Ubuntu, Debian: AppArmor profiles loaded

Tutorials that begin with "disable SELinux" are saving their author time at your expense. If SELinux blocks something legitimate, sudo ausearch -m avc -ts recent shows what, and the fix is usually one boolean or file label.

7. Protect admin interfaces

Control panels, phpMyAdmin, database ports and monitoring dashboards are the most attacked parts of a server after SSH.

  • Allow them only from your own address: sudo ufw allow from YOUR_IP to any port 8083 proto tcp.
  • Or reach them through an SSH tunnel and do not open the port at all: ssh -L 8083:127.0.0.1:8083 deploy@YOUR_SERVER_IP.
  • Turn on two-factor authentication where the panel offers it.

8. Web applications

A hardened server does not protect an outdated application. Keep the CMS, its plugins and its language runtime current: the last two months brought critical fixes for both WordPress and PHP-FPM. Run each site as its own system user so that one compromised site cannot read another's files.

9. Backups and a way back

Hardening lowers the odds; it does not make recovery unnecessary. Keep encrypted backups with a different provider and test a restore. See VPS backup strategy.

10. Audit the result

Lynis is an open-source audit tool that checks several hundred settings and prints suggestions.

sudo apt install lynis        # Ubuntu, Debian
sudo dnf install lynis        # AlmaLinux, Rocky Linux (EPEL)
sudo lynis audit system

Read the warnings and suggestions and decide on each; a higher "hardening index" is not a goal in itself, and some suggestions do not fit a small web server.

Verification summary

ControlCommandExpected
Automatic updatessystemctl list-timersapt or dnf timer scheduled
SSH passwords offsudo sshd -T | grep passwordauthenticationno
Root login offsudo sshd -T | grep permitrootloginno
Firewall activesudo ufw status / sudo firewall-cmd --stateactive / running
Open portsnmap -Pn from outsideOnly intended ports
Local-only servicessudo ss -tulpnDatabases on 127.0.0.1
Fail2bansudo fail2ban-client status sshdJail listed
SELinux / AppArmorgetenforce / sudo aa-statusEnforcing / profiles loaded

Frequently asked questions

What is the most important step in securing a VPS?

SSH keys with password login disabled, then automatic security updates. Those two remove the most common ways in.

Do I need fail2ban if I use SSH keys?

It is no longer what keeps attackers out, so it is optional. It still reduces log noise and protects other services that accept passwords.

Should I change the SSH port?

You can. It cuts automated login attempts in the logs and adds no real protection against someone who scans for it.

How long does this checklist take?

About an hour on a new server if you have done it before, longer the first time. Older guides on this site cover the individual tools in more depth: ufw and 25 hardening tips for Linux servers.

Share story:

Continue reading other guides on How VPS.