Four changes to make on a new Ubuntu Server before anything goes live: SSH keys, a ufw firewall, automatic security updates and fail2ban. Here is the order, the commands and the checks that keep you from locking yourself out.
A fresh Ubuntu Server is reachable from the internet within minutes of being created, and automated scanners find new machines quickly. Within the first hours the logs usually already show strangers trying to log in as root and as a dozen other common user names. Nothing is wrong with the machine; that is simply what a public address attracts.
This article is the first hour: four changes that remove the most common ways in, in the order we apply them on a new server. It is deliberately not the long-term routine; that lives in our Ubuntu Server maintenance checklist. If you would rather have this done and documented for you, see our server administration service.
Ubuntu Server hardening: the short answer
On a new Ubuntu Server, do four things before anything else goes live: log in with SSH keys and switch off password and root login, enable the ufw firewall with only SSH, HTTP and HTTPS open, turn on automatic security updates with unattended-upgrades, and install fail2ban to ban addresses that keep failing to log in. Together they take well under an hour and cover the attacks seen most often.
Before you start: protect yourself from locking yourself out
Two of the steps below change how you can log in. A mistake in either can lock you out of your own server, so follow these rules throughout:
- Keep a second SSH session open while you change SSH or firewall settings. If the new settings break something, the open session still works and you can undo the change.
- Know your fallback. Find out how you reach the machine without SSH: the provider’s web console, or the Proxmox console for a virtual machine.
- Change one thing at a time and test it before moving on.
Then bring the system up to date and create a named account that uses sudo, so that you do not work as root:
sudo apt update && sudo apt upgrade
sudo adduser deploy
sudo usermod -aG sudo deploy
Replace deploy with the name you want. Use a personal account for each person who administers the server.

Step 1: SSH keys instead of passwords
A password can be guessed, reused or leaked. An SSH key pair cannot be guessed in any practical way: the private key stays on your computer and the server only holds the public half. Once keys work, passwords can be switched off, which removes password guessing as an attack altogether.
Create a key and install it
On your own computer (not the server), generate a key and copy the public part to the new account:
ssh-keygen -t ed25519 -C "laptop-2026"
ssh-copy-id deploy@your-server-address
Protect the private key with a passphrase. Then open a new terminal and confirm that ssh deploy@your-server-address logs you in without asking for the account password. Do not continue until this works.
Turn off password and root login
Edit the SSH server configuration with sudo nano /etc/ssh/sshd_config and set:
PasswordAuthentication no
PermitRootLogin no
On current Ubuntu releases the main file also reads extra files from /etc/ssh/sshd_config.d/, and some cloud images ship a file there that sets PasswordAuthentication yes. Look in that folder and make sure nothing there contradicts what you just set. Then check the syntax and restart the service, with your second session still open:
sudo sshd -t
sudo systemctl restart ssh
From a third terminal, confirm that the key login still works. Only then close the extra session. If you rely on the provider’s console as a fallback, test that it works before you need it.
Moving SSH to another port is sometimes suggested. It cuts the noise in the logs but does not make a weak setup safe, so treat it as optional and never as a replacement for keys.
Step 2: a firewall with ufw
A firewall closes every door you did not deliberately open. Ubuntu ships with ufw, the Uncomplicated Firewall, which is a front end to the kernel packet filter and is simple enough to read at a glance. The order matters here: allow SSH before you enable the firewall, otherwise you cut off your own session.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose
The commands deny all incoming connections by default, allow outgoing ones, and open only SSH and the two web ports. Open ports only for services you actually run; a database or admin panel should usually not be reachable from the whole internet. Keep your second session open when you run sudo ufw enable, and check the output of the last command against what you intended.
If the server runs inside a virtual machine or a cloud account, there may be a second firewall at the provider or hypervisor level. It does not replace ufw; the two work together, and it is worth knowing both exist. Our comparison of Proxmox and the public cloud explains where such layers sit.
Step 3: automatic security updates
Most real-world intrusions use a flaw that already has a fix. The window between a fix being published and you installing it is the risk. Ubuntu’s unattended-upgrades package closes that window by installing security updates by itself. It is usually present on Ubuntu Server already, but confirm it instead of assuming:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgrades
Choose yes when asked. This writes the file /etc/apt/apt.conf.d/20auto-upgrades, which switches on the daily update check and the unattended upgrade. By default only updates from the security pocket are installed automatically, which is the sensible setting for a production machine.
Some updates, such as a new kernel, only take effect after a restart. The default is that the server does not reboot by itself. You can enable automatic reboots in /etc/apt/apt.conf.d/50unattended-upgrades, but only choose that if a restart at a fixed quiet hour is acceptable for what the server does; otherwise schedule reboots yourself. Look at the logs in /var/log/unattended-upgrades/ now and then to see what was installed.
Step 4: fail2ban against repeated login attempts
With keys only, password guessing cannot succeed, but the attempts still fill the logs and waste resources. fail2ban reads the logs and temporarily bans addresses that fail repeatedly. It also protects other services later, such as a mail or web login, so it is worth having from the start.
sudo apt install fail2ban
sudo systemctl enable --now fail2ban
Do not edit the default configuration file directly; it can be overwritten by updates. Put your settings in /etc/fail2ban/jail.local:
[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
This bans an address for an hour after five failed attempts within ten minutes. Restart the service and check that the SSH jail is active:
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd
One caution: a ban applies to you too. If you mistype something repeatedly from your own address, you can ban yourself, which is another reason to know your console fallback.
Check that everything works
| Check | Command | What you expect |
|---|---|---|
| Key login works | ssh deploy@your-server-address | Login without the account password |
| Password login is off | sudo sshd -T | grep -i passwordauthentication | passwordauthentication no |
| Root login is off | sudo sshd -T | grep -i permitrootlogin | permitrootlogin no |
| Firewall is active | sudo ufw status verbose | Default deny incoming; only SSH, 80 and 443 allowed |
| Listening ports | sudo ss -tlnp | Nothing listening that you did not put there |
| Updates are automatic | cat /etc/apt/apt.conf.d/20auto-upgrades | Both lines set to "1" |
| fail2ban is running | sudo fail2ban-client status sshd | The jail is listed and active |
What this first hour does not cover
These four steps are a baseline, not a complete security programme. They do not replace backups that leave the server and are restored in tests, monitoring with alerts that reach a real person, a periodic review of who has access, or a plan for the end of support of your Ubuntu release. Those are the recurring jobs we list in the maintenance checklist.
The application matters too. A hardened server running outdated code with a weak admin login is still exposed, so keep the framework and its packages updated as well.
What to do next
If you have just created a server, work through the four steps now, while nothing important depends on it. If it has been running for a while on passwords and open ports, do the same steps carefully, one at a time, with a second session open. Afterwards write down what you changed and where the fallback console is.
Hardening limits the damage an attacker can do, but it does not replace backups, so read our backup strategy for small companies on Proxmox as well.
When you want a second pair of hands, our server administration service covers Ubuntu Server and Proxmox, including hardening, backups, monitoring and updates. You can also contact us and describe your setup.