Ubuntu server hardening checklist

Ten steps that close the most common ways into a Linux server, with copy-and-paste commands and a way to check each one. Written for Ubuntu 22.04 and 24.04.

Security9 min readUpdated 26 Oct 2026

Before you start

Take a snapshot or backup first, and keep a second SSH session open while you change SSH settings. If a change locks you out, the open session lets you undo it.

Rule of thumb: change one thing, test it from a new session, then move on.

1. Create a named sudo user

Stop using the root account for daily work. Create a personal user and give it sudo.

adduser alex
usermod -aG sudo alex

Then copy your public key to the new user from your own computer:

ssh-copy-id alex@your-server

2. Lock down SSH

Only keys, no direct root login. Put the settings in a drop-in file so package updates do not overwrite them.

# /etc/ssh/sshd_config.d/10-hardening.conf
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
MaxAuthTries 3

Test the syntax, then reload. On Ubuntu the service is called ssh.

sudo sshd -t && sudo systemctl reload ssh
Check: open a new terminal and log in with your key before you close the old session.

3. Turn on a default-deny firewall

Allow SSH first, or you will lock yourself out.

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit 22/tcp
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose

4. Install security updates automatically

Most breaches use bugs that already have a patch. Let the server apply security updates itself.

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

To let it reboot when a kernel update needs one, set a quiet hour in /etc/apt/apt.conf.d/50unattended-upgrades:

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "03:00";

5. Add brute-force protection

With key-only SSH, guessing passwords cannot work, but banning noisy addresses keeps logs readable and load low.

sudo apt install -y fail2ban
sudo tee /etc/fail2ban/jail.d/sshd.local <<'EOF'
[sshd]
enabled = true
maxretry = 3
bantime = 24h
EOF
sudo systemctl restart fail2ban

6. Close what you do not use

List everything listening on the network, and stop what you do not need.

sudo ss -tulpn

Databases should listen on 127.0.0.1 only. If you see MySQL, Redis or MongoDB on 0.0.0.0, fix that first.

7. Use HTTPS everywhere

Get a free certificate and make sure it renews itself.

sudo apt install -y certbot python3-certbot-nginx
sudo certbot --nginx -d example.com -d www.example.com
sudo certbot renew --dry-run

8. Back up off the server, and test the restore

Hardening reduces the chance of a bad day. Backups decide how bad it is. Read our backup guide for a plan you can copy.

9. Know when something breaks

Add an uptime check on your site, and alerts for disk space and certificate expiry. A server nobody watches is a server nobody fixes.

10. Verify

These commands show the state you just built:

sudo sshd -T | grep -E 'permitrootlogin|passwordauthentication'
sudo ufw status verbose
sudo ss -tulpn
apt list --upgradable 2>/dev/null | head
systemctl list-timers | grep -E 'apt|certbot'

Questions

Is this enough to make a server secure?

It removes the most common causes of compromise, but no checklist makes a server unhackable. Applications, plugins and weak passwords elsewhere still matter.

Will these changes break my site?

They should not. SSH, firewall and update settings do not touch your web content. Test each step, and keep a second session open while you edit SSH.

Want this done for you? See our Server setup and hardening service: Secure access, firewall, patching and a written record. The basics, done properly.

Want this done for you?

We set up and check all of this on client servers, with a backup first and a written record.