Ubuntu Linux Server

This guide was written for Ubuntu Server 26.04 LTS.

For a VPS provider, I highly recommend Hostinger. I personally use their KVM 2 plan to host my website and a few other services:

Check for system updates

Terminal window
apt update && apt upgrade -y

If you see Some sources can be modernized. Run 'apt modernize-sources' to do so, run the following command:

Terminal window
apt modernize-sources

Switch to a limited user account

You should switch to a limited user account as soon as possible.

Delete or rename an existing user

Some platforms create a default limited user account (e.g., ubuntu) for you. For security reasons, you should delete or rename the account.

  1. List all existing users:

    Terminal window
    awk -F: '$3 >= 1000 && $3 < 65534 {print $1}' /etc/passwd

    And their home directories:

    Terminal window
    ls -l /home
  2. Either delete the existing user, group, and home directory:

    Terminal window
    pkill -u ubuntu
    deluser --remove-home ubuntu
    delgroup ubuntu

    Or rename the existing user, group, and home directory, replacing new_username with your desired username:

    Terminal window
    usermod -l new_username ubuntu
    groupmod -n new_username ubuntu
    mv /home/ubuntu /home/new_username
    usermod -d /home/new_username -m new_username
    chown -R new_username:new_username /home/new_username

Add a new user

  1. Create a new user, changing example_userto your preferred user name:

    Terminal window
    adduser example_user
  2. Give the user sudo access to be able to run commands as root:

    Terminal window
    adduser example_user sudo

Disable root login

  1. Open the sshd config file in a text editor:

    Terminal window
    sudo nano /etc/ssh/sshd_config
  2. Press Ctrl+F, type PermitRootLogin, and press Enter to find the setting.

  3. Either comment it out, or change it to prohibit-password or no.

    The default is prohibit-password, which only allows SSH key based logins.

  4. Press Ctrl+X, then Y, and finally Enter to save the file.

Enable the firewall

  1. Allow SSH through the firewall:

    Terminal window
    sudo ufw allow "OpenSSH"
  2. Enable logging:

    Terminal window
    sudo ufw logging on
  3. Start the firewall:

    Terminal window
    sudo ufw enable

Enable brute force protection

Use Fail2Ban to automatically block IP addresses with too many failed login attempts.

  1. Install fail2ban:

    Terminal window
    sudo apt install -y fail2ban
  2. Check the current status of all jails (to see what’s automatically protected):

    Terminal window
    sudo fail2ban-client status --all