Every time I spin up a fresh Ubuntu LTS machine, I run through the same twenty-minute baseline before installing an application, a web server, or Docker. None of these steps are exotic. The goal is simply to close off automated noise on port 22, keep the firewall explicit, and make sure security patches apply without waiting for me to remember to log in.
The order matters slightly: create your non-root user and verify SSH key login in a second terminal before you lock down root or enable the firewall. Locking yourself out of a fresh box on minute five is a rite of passage you only need once.
1. Update packages and create a sudo user
Start by bringing the package index and installed packages up to date, then create a dedicated administrative user instead of working as root.
apt update && apt upgrade -y
adduser deploy
usermod -aG sudo deploy
Copy your public key from your workstation into the new user's authorized_keys file. If you already uploaded a key for root during provisioning, you can also copy it over and fix ownership:
mkdir -p /home/deploy/.ssh
chmod 700 /home/deploy/.ssh
cp /root/.ssh/authorized_keys /home/deploy/.ssh/authorized_keys
chown -R deploy:deploy /home/deploy/.ssh
chmod 600 /home/deploy/.ssh/authorized_keys
Before changing anything in sshd_config, open a second terminal and confirm ssh deploy@your-server-ip logs in with your key and that sudo -v works.
2. Lock down SSH
Automated scanners hit port 22 within minutes of an IPv4 address coming online. Password authentication should be off, and root login should be disabled. Rather than editing /etc/ssh/sshd_config directly, drop a small override file into /etc/ssh/sshd_config.d/ so package upgrades never prompt about conflicting config files.
sudo tee /etc/ssh/sshd_config.d/99-hardening.conf > /dev/null <<'EOF'
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitEmptyPasswords no
X11Forwarding no
MaxAuthTries 3
AllowUsers deploy
EOF
Validate the syntax before reloading the daemon. On Ubuntu 24.04 and newer, SSH may use socket activation (ssh.socket), so reload the service carefully and keep your existing session open until you verify a new connection.
sudo sshd -t
sudo systemctl reload ssh
I usually leave SSH on port 22. Moving it to a high port cuts down log noise, which is nice, but it is not a security boundary and occasionally breaks muscle memory or restrictive corporate Wi-Fi networks.
3. Configure UFW with deny-by-default
Uncomplicated Firewall (ufw) gives you a readable wrapper around nftables/iptables. Deny all incoming traffic by default, allow outgoing traffic, and open only the ports your server actually listens on.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
One important caveat if you run Docker on the same host: Docker manipulates iptables chains directly and bypasses ufw rules for published container ports. If you bind a container with -p 5432:5432, it will be reachable from the internet even if UFW does not allow 5432. Always bind internal services to 127.0.0.1:5432:5432 when you only need local access.
4. Install fail2ban for SSH
Even with password authentication disabled, bots hammering SSH burn CPU cycles and clutter logs. fail2ban watches authentication logs and bans offending IPs at the firewall level after repeated failures.
sudo apt install -y fail2ban
Create a local jail file instead of editing jail.conf:
sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
banaction = ufw
[sshd]
enabled = true
backend = systemd
EOF
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd
Setting backend = systemd is essential on modern Ubuntu releases where /var/log/auth.log may not exist if rsyslog is not installed.
5. Enable unattended security upgrades
Most small servers get compromised through unpatched software, not zero-days. Enable automatic security updates so kernel and OpenSSL patches land promptly.
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades
Check /etc/apt/apt.conf.d/50unattended-upgrades to confirm security origins are enabled and decide whether you want automatic reboots at 03:00 UTC when a kernel update requires one. For single-node side projects, I enable automatic reboots at a quiet hour; for anything stateful with active users, I leave automatic reboot off and monitor /var/run/reboot-required.
6. Quick verification pass
Before moving on to application setup, run three quick checks to confirm the baseline is actually active:
ss -tulnp
sudo ufw status numbered
sudo fail2ban-client status
If ss -tulnp shows only sshd (and later your web server) listening on public interfaces, UFW is active, and fail2ban is watching sshd, the machine is in good shape for real work.