Hardening Ubuntu Server 24.04 and 26.04 LTS: a Practical Security Checklist

A practical checklist to secure an Ubuntu 24.04 or 26.04 LTS server: hardened SSH configuration, the UFW firewall, Fail2ban against brute-force attacks, and least-privilege permissions.

Hardening Ubuntu Server 24.04 and 26.04 LTS: a Practical Security Checklist

A freshly installed Ubuntu server — on a VPS, a cloud provider, or an old repurposed PC — is ready to "do its job", but it is not yet ready to withstand the unauthorized access attempts that often start just minutes after its IP address becomes reachable from the internet. This article collects a practical hardening checklist that applies to both Ubuntu Server 24.04 LTS "Noble Numbat" and the more recent Ubuntu Server 26.04 LTS "Resolute Raccoon", the two long-term-support releases currently in use across most production installations.

We'll cover four areas: securing SSH (the protocol almost always used to administer a server remotely), the UFW firewall, the anti-brute-force service Fail2ban, and a set of least-privilege permissions to apply to users and files. You don't need to be an experienced system administrator: every step is explained before the exact command to type is given.

Why hardening can't wait

As soon as a server has a public IP address, it becomes visible to bots that continuously scan the internet looking for open ports and weak credentials. The most common target is port 22, the one used by SSH: in the logs of an unprotected, exposed server it is normal to see thousands of login attempts per day, coming from IP addresses all over the world. Hardening doesn't eliminate the risk entirely, but it drastically reduces the attack surface and renders most common automated attacks ineffective.

Before starting, it's worth remembering a general good practice: every change to SSH should be tested while keeping the current session open and opening a second connection to verify, before closing the original one. This way, if a configuration is wrong, you won't get locked out of the server.

1. Updates and base packages

The first step, trivial but often overlooked, is making sure the system is up to date and that security updates will be applied automatically going forward.

sudo apt update && sudo apt full-upgrade -y
sudo apt install -y unattended-upgrades ufw fail2ban
sudo dpkg-reconfigure --priority=low unattended-upgrades

unattended-upgrades is Ubuntu's official package that automatically installs security patches without manual intervention: on a server you don't check every day, it's one of the most effective protections, and it's enabled by default on cloud images, though it's worth confirming with the command above.

2. A non-privileged user, not root

Many Ubuntu cloud images already disable direct root login and create an administrative user (often called ubuntu) with sudo privileges. If you're starting from an install that doesn't, create one manually:

sudo adduser username
sudo usermod -aG sudo username

Operating day-to-day with a regular user, elevating privileges only when needed with sudo, limits the possible damage from a mistaken command or a compromised process: an error in a service running as a normal user doesn't automatically have access to the entire system.

3. Hardening SSH

SSH is the main entry point of a headless server (one without a graphical interface), so it's the area deserving the most attention.

3.1 Key-based authentication, not passwords

An SSH key is a pair of cryptographic files (a public part to copy to the server, a private part to keep only on your own computer) that replaces a password: it's practically impossible to guess through a brute-force attack. From your own computer:

ssh-keygen -t ed25519 -C "name@device"
ssh-copy-id username@server-address

The ed25519 algorithm is today's recommended standard: shorter keys than RSA, faster generation and verification, and adequate cryptographic strength. After confirming that key-based login works (testing it in a new terminal window, without closing the current one), you can disable password-based access.

3.2 The correct configuration file to edit

A frequent source of confusion on Ubuntu 24.04 and 26.04 cloud installs: besides the main /etc/ssh/sshd_config file, there's a /etc/ssh/sshd_config.d/ folder containing additional configuration files (called "drop-ins"), loaded after the main file and therefore able to override its settings. On cloud images it's common to find a file such as 50-cloud-init.conf that re-enables password login for the first access. Before editing the configuration, it's worth checking what's already there:

sudo grep -ri "PasswordAuthentication\|PermitRootLogin" /etc/ssh/sshd_config /etc/ssh/sshd_config.d/*.conf

If more than one result appears for the same directive, the last-loaded file (in alphabetical order, inside sshd_config.d) wins. The cleanest solution is to create your own dedicated file with a name that loads last, for example:

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

with this content:

PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
KbdInteractiveAuthentication no
X11Forwarding no
MaxAuthTries 3
AllowUsers username

What the main directives mean: PermitRootLogin no prevents direct root login over SSH (you must authenticate as a regular user and then use sudo); PasswordAuthentication no disables password-based login, leaving only keys; MaxAuthTries 3 closes the connection after a few failed attempts; AllowUsers explicitly restricts which accounts may connect over SSH. After saving, check the syntax and reload the service without interrupting it:

sudo sshd -t
sudo systemctl reload ssh

The sshd -t command checks for syntax errors before applying the changes: if something is wrong, the service isn't restarted, avoiding a lockout.

3.3 Changing the port: useful, not a fix on its own

Moving SSH from port 22 to a non-standard port (for example with the Port 2222 directive in the same file) reduces the "noise" in the logs caused by the most generic automated scanners, but it isn't a security measure in the strict sense: a motivated attacker can still discover the real port with a full scan. It should be seen as a complement to the other measures, not a substitute.

4. Firewall with UFW

UFW (Uncomplicated Firewall) is Ubuntu's simplified interface for managing the Linux kernel's firewall rules (netfilter), without having to write complex iptables commands. The correct approach is "deny everything by default, open only what's needed":

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw limit OpenSSH

The first two lines set the base policy: all incoming traffic is blocked unless explicitly allowed, while outgoing traffic (the kind generated by the server itself, for example to download updates) stays permitted. allow OpenSSH opens the SSH port configured on the system (if it was changed, replace it with the correct number, e.g. ufw allow 2222/tcp). The ufw limit command is particularly useful: it applies rate limiting that automatically denies connections from an IP address attempting more than 6 connections within 30 seconds, an effective deterrent against the simplest brute-force scripts.

If the server also hosts a website, add the corresponding ports before enabling the firewall:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Enabling UFW before opening the SSH port is the most common mistake, and the most frustrating to fix on a server with no physical access: always double-check the order of commands, or check the status with ufw status numbered before typing enable.

5. Fail2ban: blocking persistent attackers

Fail2ban is a service that scans system logs (for SSH, but also for other services such as web servers) and temporarily blocks, at the firewall level, IP addresses generating too many failed access attempts within a time window. It's the natural complement to UFW's rate limiting: where UFW slows down, Fail2ban bans.

The configuration should never be edited directly in the jail.conf file (which gets overwritten by package updates), but in a dedicated copy:

sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
sudo nano /etc/fail2ban/jail.local

In the [sshd] section, set the desired parameters:

[sshd]
enabled = true
port = ssh
maxretry = 4
findtime = 10m
bantime = 1h

In plain terms: if the same IP address generates 4 failed attempts (maxretry) within a 10-minute window (findtime), that address is blocked by the firewall for 1 hour (bantime). On more exposed servers it's common to set an increasing bantime (bantime.increment = true) so that repeat offenders stay blocked for progressively longer periods. Apply and verify with:

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

The fail2ban-client status sshd command shows how many attempts have been blocked and which IP addresses are currently banned, useful for checking that the service is actually working.

6. Least-privilege permissions and other good practices

The last block of the checklist covers the principle of least privilege: every account, service or file should have only the permissions strictly necessary for its task, not one more.

  • Targeted sudo, not "ALL ALL": with sudo visudo you can restrict a user to running only specific commands as root (for example, only restarting a specific service) instead of granting unlimited privileges when they aren't needed.
  • Correct umask: the value umask 027 (set in /etc/profile or in the user's shell file) makes newly created files unreadable and unwritable by "other" system users by default.
  • Private home directories: chmod 750 /home/username prevents other local users from browsing each account's personal files.
  • No superfluous services: sudo ss -tulpn shows which ports are actually listening; any unnecessary service (a database accidentally exposed, a forgotten admin panel) should be disabled with sudo systemctl disable --now service-name.
  • Centralized, monitored logs: tools like journalctl -u ssh --since "1 hour ago" let you quickly check recent login attempts without installing extra software.
  • Kernel updates without missed manual reboots: on Ubuntu 24.04 and 26.04 the Livepatch service (free for personal use with an Ubuntu One account) applies critical kernel security patches without requiring an immediate reboot, useful when a scheduled reboot hasn't happened yet.

Quick-reference checklist

For anyone wanting a fast sequence to follow on a new Ubuntu 24.04 or 26.04 LTS server:

  • Update the system and install unattended-upgrades, ufw, fail2ban.
  • Create a non-privileged user with sudo access and stop using root directly.
  • Set up key-based SSH access, checking any files in sshd_config.d/ that might override the settings.
  • Disable root login and password authentication over SSH, after testing key-based access.
  • Enable UFW with a "deny everything by default" policy and a limit rule on SSH, without forgetting to open the SSH port before enable.
  • Configure Fail2ban with jail.local to automatically ban IPs with too many failed attempts.
  • Apply least-privilege permissions on sudo, home directories, active services, and periodically review listening ports.

None of these measures alone makes a server invulnerable: cybersecurity works in layers, and it's precisely the combination of several independent layers — keys instead of passwords, a restrictive firewall, automatic blocking of repeated attempts, least-privilege permissions — that makes an Ubuntu server a solid choice for hosting sites, applications, or personal services, even without a dedicated security team.

Official sources

Share

What's Your Reaction?

Like Like 0
Dislike Dislike 0
Love Love 0
Funny Funny 0
Angry Angry 0
Sad Sad 0
Wow Wow 0