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:
sudo apt updateNow apply those updates by running:
sudo apt upgrade -yOnce complete, you can remove packages that are no longer needed:
sudo apt autoremove -ySetup automatic updates
Ubuntu can be configured to automatically install new security updates.
To do so, run the following commands:
sudo apt install unattended-upgrades -ysudo dpkg-reconfigure --priority=low unattended-upgradesThis 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:
sudo apt install fail2ban -ysudo systemctl enable --now fail2banTo verify that Fail2ban is running:
sudo systemctl status fail2banIf the service is running, you should see it output active (running):

To see the current list of banned IPs for SSH, run:
sudo fail2ban-client status sshdSetup 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:
sudo ufw allow sshIt’s important to allow SSH first to ensure you don’t lock yourself out of the server.
To enable the firewall, run:
sudo ufw enableNow, only connections to allowed ports will be permitted, while still allowing outgoing traffic.
To check the status of the firewall, run:
sudo ufw status verboseIf you configured it correctly, you should see the SSH ports allowed in:

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):
adduser <name>Follow the prompts to set a password, everything else is optional.
Grant the new user sudo privileges:
usermod -aG sudo <name>Test the new user by switching to it and checking sudo access:
su - <name>sudo whoamiwhoamiNote 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:
sudo nano /etc/ssh/sshd_configFind PermitRootLogin and change it from yes to no:
PermitRootLogin noFinally, restart SSH to apply the changes:
sudo systemctl restart sshSetup 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:
ssh-keygen -t ed25519Copy the public key to your server:
ssh-copy-id your_username@your_server_ipTest the connection to ensure key-based login works:
ssh -v your_username@your_server_ipThe -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):

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:
mkdir -p ~/.sshnano ~/.ssh/authorized_keysPaste your public key into the file and save.
Now, set the correct permissions on the server:
chmod 700 ~/.sshchmod 600 ~/.ssh/authorized_keysVerify 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:

Disable password login
After confirming SSH key authentication is working, you should disable password logins entirely.
Open the SSH configuration file:
sudo nano /etc/ssh/sshd_configFind PasswordAuthentication and change it from yes to no:
PasswordAuthentication noRestart the SSH service to apply the changes:
sudo systemctl restart sshConclusion
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.