623 words
3 minutes
How to Secure and Harden Ubuntu 24.04 LTS

The default Ubuntu installation only provides basic security measures, leaving your system vulnerable to attacks.

You can take a few steps to drastically reduce the chances of your system being compromised.

Update packages#

It’s important to upgrade packages to ensure you have the latest security updates.

You can get the latest security updates by running:

Terminal window
sudo apt update

Now apply those updates by running:

Terminal window
sudo apt upgrade -y

Once complete, you can remove packages that are no longer needed:

Terminal window
sudo apt autoremove -y

Setup automatic updates#

Ubuntu can be configured to automatically install new security updates.

To do so, run the following commands:

Terminal window
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure --priority=low unattended-upgrades

This will open a configuration dialog where you can choose to enable automatic security updates.

Install fail2ban#

By default, SSH allows unlimited password attempts.

This means your server is currently exposed to brute-force attacks.

Fail2ban is a simple tool which will lock out IP addresses with repeated failed login attempts.

To install and enable it, run:

Terminal window
sudo apt install fail2ban -y
sudo systemctl enable --now fail2ban

To verify that Fail2ban is running:

Terminal window
sudo systemctl status fail2ban

If the service is running, you should see it output active (running):

The Fail2ban service active message

To see the current list of banned IPs for SSH, run:

Terminal window
sudo fail2ban-client status sshd

Setup a firewall#

UFW (Uncomplicated Firewall) comes pre-installed with Ubuntu, but is disabled by default.

First, run the following command to allow traffic through SSH:

Terminal window
sudo ufw allow ssh

It’s important to allow SSH first to ensure you don’t lock yourself out of the server.

To enable the firewall, run:

Terminal window
sudo ufw enable

Now, only connections to allowed ports will be permitted, while still allowing outgoing traffic.

To check the status of the firewall, run:

Terminal window
sudo ufw status verbose

If you configured it correctly, you should see the SSH ports allowed in:

The ufw service status message

Disable root access#

Logging in directly as root is a major security risk.

If the root account is compromised, the attacker gains full control of your server.

Add a new user (replace <name> with your chosen name):

Terminal window
adduser <name>

Follow the prompts to set a password, everything else is optional.

Grant the new user sudo privileges:

Terminal window
usermod -aG sudo <name>

Test the new user by switching to it and checking sudo access:

Terminal window
su - <name>
sudo whoami
whoami

Note that when sudo prompts for a password, it’s your user password, not the root password.

sudo whoami should return root and whoami should return your new username.

Once confirmed working, disable root SSH login by editing the SSH config:

Terminal window
sudo nano /etc/ssh/sshd_config

Find PermitRootLogin and change it from yes to no:

Terminal window
PermitRootLogin no

Finally, restart SSH to apply the changes:

Terminal window
sudo systemctl restart ssh

Setup SSH key authentication#

SSH key authentication is much more secure than password logins.

To connect with a key, we first need to generate a public and private key pair.

Linux#

Generate a new SSH key on your local machine:

Terminal window
ssh-keygen -t ed25519

Copy the public key to your server:

Terminal window
ssh-copy-id your_username@your_server_ip

Test the connection to ensure key-based login works:

Terminal window
ssh -v your_username@your_server_ip

The -v flag (verbose) will let you confirm that the public key is being used.

Windows#

Open PuTTYgen and generate a new key using EdDSA (ED25519):

The PuTTYgen screen

Save the private key (.ppk) and copy the public key text.

On the server, run the following commands to create the .ssh directory and open the authorized_keys file:

Terminal window
mkdir -p ~/.ssh
nano ~/.ssh/authorized_keys

Paste your public key into the file and save.

Now, set the correct permissions on the server:

Terminal window
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Verify it worked by connecting to the server using your private key in PuTTY.

You can specify your private key file in the Connection > SSH > Auth tab:

The putty SSH option

Disable password login#

After confirming SSH key authentication is working, you should disable password logins entirely.

Open the SSH configuration file:

Terminal window
sudo nano /etc/ssh/sshd_config

Find PasswordAuthentication and change it from yes to no:

Terminal window
PasswordAuthentication no

Restart the SSH service to apply the changes:

Terminal window
sudo systemctl restart ssh

Conclusion#

By following these steps you now have:

  • Regular security updates
  • Limited access
  • Strong authentication

With these combined, your Ubuntu system is now much more difficult to compromise.