How to Harden Ubuntu Server After a Fresh Install: The First 15 Minutes
A fresh Ubuntu install is not safe by default. SSH is open, the root account may still accept a password, and no firewall is running. That is the normal state of a new server, and it is also the state most attacks look for. Bots scan the internet around the clock for machines like this one, and the first login attempts often start within minutes of the server coming online. The good news is that you can harden Ubuntu server the right way in about fifteen minutes, and the steps below cover most of the risk. Work through them in order, in one sitting, before you install a web server or move any real data onto the box.
Harden Ubuntu Server Fast: Update Everything First
Start with the boring step, because it closes the most holes. A fresh image is almost never fully patched. The packages on it were frozen weeks or months before you downloaded it, and security fixes have landed since. Run the update, then check whether the kernel changed:
sudo apt update && sudo apt upgrade -y
sudo reboot
Reboot when the upgrade touches the kernel or core system libraries. It takes a minute, and it is the only way to know the new kernel is the one actually running. Skip the reboot and you may sit on a patched package that never loads. While the server comes back up, note the time. The rest of this guide assumes a server that is fully patched, and every step after this one builds on that.
One habit worth starting now: run sudo apt update && sudo apt upgrade -y on a schedule, or set up automatic patching (covered in step four). Manual updates are fine for a single server you log into often. They fall apart the moment you stop logging in, which is exactly when an unpatched package becomes a problem.
Lock Down SSH Before Anything Else
SSH is the front door, and on a fresh install it is wide open. Password login is allowed, the root user can log in directly on many images, and the service listens on the default port 22. Change all three of those things. First, make sure you can log in with an SSH key before you turn off passwords, or you will lock yourself out. Copy your public key to the server:
ssh-copy-id user@your-server-ip
Log out, log back in with the key, and confirm it works. Only then edit /etc/ssh/sshd_config and set these three lines:
PasswordAuthentication no
PermitRootLogin no
PubkeyAuthentication yes
Then restart the service with sudo systemctl restart ssh and test a second login in a new terminal window while the first session is still open. If the new session fails, the old one is still there to fix the config. This two-window test has saved more admins than any other habit in server work.
When you disable root login, the noise in your logs drops fast. Bots hammer the root account because it always exists. With PermitRootLogin no, every one of those attempts fails before it even asks for a password. Use a normal user account with sudo instead. For the full list of SSH settings worth changing, see our guide to hardening SSH on Ubuntu 24.04, which walks through ten configuration changes in detail.
Set Up the UFW Firewall With Three Rules
Ubuntu ships with UFW, the uncomplicated firewall, but it is off by default. A firewall that is installed and disabled protects nothing. The safe order is: allow SSH first, then turn the firewall on. If you enable UFW before allowing SSH, your next login attempt will fail and you will need console access to recover.
sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose
That is the minimum. From here, add rules only for services you actually run. A web server needs ports 80 and 443. A database that only your app talks to needs no public rule at all. Every open port is a door someone can knock on, so the rule is simple: if nothing on the server listens on a port, do not open it.
Check the status output after enabling. You should see SSH allowed and everything else denied by default. If you later install nginx or another service and it does not answer, check UFW before you blame the application. A missing firewall rule is one of the most common causes of a “fresh install, nothing works” afternoon. Our step-by-step UFW firewall setup guide for Ubuntu Server 24.04 covers the commands for web servers, custom ports, and rate limiting in more depth.
Turn On Automatic Security Updates
Updates only help if they get installed. Unattended upgrades handle that for security patches, so a fix released on a Tuesday is on your server by Wednesday even if you are on holiday. Install and enable it:
sudo apt install unattended-upgrades -y
sudo dpkg-reconfigure -plow unattended-upgrades
Answer yes at the prompt. By default it installs security updates only, not every package update, which keeps the risk of a surprise break low. Check that it is actually running with sudo systemctl status unattended-upgrades. If the service is dead or masked, nothing will happen on schedule and you will only find out when you check the logs months later.
Automatic updates are not a replacement for looking at your server. They cover security patches, not feature upgrades, and they will not tell you when a package fails to install. Once a month, log in and run sudo apt update && sudo apt upgrade by hand to catch the rest. If you want the full configuration, including email reports and reboot windows, read our guide to automatic security updates on Ubuntu with unattended upgrades.
Add Fail2ban to Slow Down Brute-Force Logins
Even with password login off, bots will keep trying. Fail2ban watches the auth log and temporarily bans IP addresses that fail too many times. It does not make your server unbreakable, but it turns a bot that tries ten thousand passwords an hour into one that tries a handful and then waits. Install it and start it:
sudo apt install fail2ban -y
sudo systemctl enable --now fail2ban
The default jail protects SSH out of the box. Confirm it is active with sudo fail2ban-client status sshd. You should see the jail listed with a small number of banned addresses after a day or two on a public IP. If the jail is missing, the service may be running with no rules loaded, which is the same as not running at all.
Fail2ban has limits worth knowing. It reacts to failed logins, so it does nothing against a stolen key or a flaw in a web app. It also bans by IP, and a botnet has many IPs. Treat it as a noise filter and a speed bump, not a wall. Pair it with key-only SSH from step two and you have two independent layers doing different jobs. Our fail2ban setup guide for SSH and nginx on Ubuntu shows how to tune ban times and add jails for web services.
Check What Is Listening and What Comes Next
The last step takes one minute and often surprises people. List every service that is listening on the network:
sudo ss -tlnp
Read the output line by line. Each listening port should map to a service you recognize and expect. A port you do not recognize is a question, not a detail to skip. Common surprises on fresh images include a mail service, an RPC service, or a database listening on all interfaces when it only needs localhost. Stop or restrict anything you do not need, then run the command again to confirm it is gone.
Fifteen minutes gets you a patched server, key-only SSH, a firewall that denies by default, automatic security updates, a brute-force filter, and a clean list of listening services. That is a solid base for ubuntu server security, but it is a base, not a finished job. The next layer depends on what the server will run. A WordPress site needs its own hardening, a public database needs its own access rules, and anything facing users needs backups you have actually tested by restoring them. Do the fifteen minutes now, then keep going one layer at a time.