Self-Hosting & VPS

Ubuntu VPS Security Hardening Checklist: SSH Keys, UFW Firewall, Fail2ban & Auto Updates

Within minutes of booting a brand-new Linux VPS with a public IPv4 address, automated scanners begin probing port 22 for weak passwords and misconfigured services. Leaving default root password login enabled or skipping basic firewall rules puts your web apps, databases, and domains at risk.

This checklist covers the essential hardening steps for Ubuntu 22.04 LTS and 24.04 LTS servers without breaking remote access.

Prerequisite: Keep an Active Root Session Open

Whenever you modify SSH settings or firewall rules, keep your current terminal session open while testing changes in a second terminal window. Never close your working SSH session until you confirm you can log in with the new configuration.

Step 1: Update System Packages First

Start with fresh package indexes and security patches:

sudo apt update && sudo apt upgrade -y

Step 2: Create a Non-Root Sudo User

Running day-to-day commands as root increases the blast radius of typos or compromised scripts. Create a dedicated administrative user:

adduser deployadmin
usermod -aG sudo deployadmin

If you already have an SSH public key in /root/.ssh/authorized_keys, copy it cleanly to the new user so key authentication works immediately:

rsync --archive --chown=deployadmin:deployadmin ~/.ssh /home/deployadmin

Test logging in from a second terminal using ssh deployadmin@YOUR_SERVER_IP and verify sudo whoami returns root before proceeding.

Step 3: Enforce SSH Key Authentication & Disable Root Login

Password-based SSH logins are the primary target of brute-force botnets. Once you verify your SSH key works for deployadmin, edit the SSH daemon configuration (or drop-in config under /etc/ssh/sshd_config.d/ on Ubuntu 24.04):

sudo nano /etc/ssh/sshd_config.d/99-hardening.conf

Add the following directives:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
KbdInteractiveAuthentication no
X11Forwarding no

Validate the SSH configuration syntax before reloading—this prevents syntax typos from locking you out:

sudo sshd -t && sudo systemctl reload ssh

Quick Verification

Open a new terminal window and run ssh -o PubkeyAuthentication=no deployadmin@YOUR_SERVER_IP. The server should reject the connection immediately with Permission denied (publickey).

Step 4: Configure UFW (Uncomplicated Firewall)

UFW provides a readable wrapper around Linux netfilter rules. Set strict defaults—deny all incoming traffic and allow outgoing traffic—then explicitly permit only the ports your server needs:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Enable UFW and inspect the active ruleset:

sudo ufw enable
sudo ufw status numbered
Port / Service Rule Why
22 / OpenSSH ALLOW Remote administration (protected by SSH keys + Fail2ban)
80 / TCP ALLOW HTTP redirects and Let’s Encrypt ACME verification
443 / TCP ALLOW Encrypted HTTPS web traffic
3306 / MySQL DENY (default) Databases should stay bound to 127.0.0.1 only

Step 5: Install Fail2ban to Block Repeated Scans

Even with SSH passwords disabled, bots repeatedly hammer port 22 and Nginx login endpoints, wasting CPU and filling logs. Install Fail2ban:

sudo apt install -y fail2ban

Create a local override file rather than editing jail.conf directly (package updates overwrite jail.conf):

sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true
backend = systemd

Enable and verify the SSH jail:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

Step 6: Enable Unattended Security Updates

Critical Linux kernel, OpenSSL, and Nginx vulnerabilities require timely patching. Ubuntu includes unattended-upgrades to apply security patches automatically:

sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

Verify the dry run completes without errors:

sudo unattended-upgrade --dry-run --debug

Common Hardening Mistakes to Avoid

  • Enabling UFW before allowing SSH: Always run ufw allow OpenSSH before ufw enable, or your active SSH connection will hang and block reconnection.
  • Cloud Firewall Mismatch: Remember that providers like Google Cloud, AWS, and Oracle Cloud enforce a separate VPC firewall outside Ubuntu. Both the cloud security group and UFW must allow ports 80 and 443 for websites to load.
  • Exposing Database Ports: Confirm MariaDB/MySQL or PostgreSQL binds to 127.0.0.1 rather than 0.0.0.0 unless strictly required over a private VPN.

Summary

With a non-root sudo account, key-only SSH, strict UFW rules, Fail2ban active, and unattended security upgrades enabled, your Ubuntu VPS eliminates the vast majority of automated internet attacks.

Leave a Reply

Your email address will not be published. Required fields are marked *