A fresh Ubuntu server is open by default. Root login works, password login works, and every port your services need sits exposed to the internet. A secure Ubuntu 24.04 VPS setup closes those gaps before you install anything else, and it takes less than an hour if you work through it in order. This guide walks through each step on a clean server: updates, a non-root user, SSH hardening, a firewall, fail2ban, and backups.
Do the steps in the order shown. Each one builds on the last, and skipping ahead can lock you out.
What You Need Before You Start
You need a VPS with Ubuntu 24.04 installed, the root password or console access from your provider, and an SSH client on your own computer. Most providers give you the server IP address and root credentials by email after you order the server. If you have not picked a provider yet, read our roundup of the best unmanaged VPS providers for 2026 and compare them on performance and support before you buy.
Work from your own computer, not from a shared machine. You will create an SSH key pair and store the private key there. If that computer is lost or shared, anyone with the key file can reach your server, so keep it in your user folder and protect it with a passphrase.
One more thing before you log in: check that you can reach the provider console or recovery console. If an SSH change goes wrong, the console is your way back in. Test it once now, while the server still works.
Update the System First
Log in as root and update the package lists and installed packages. Fresh images often ship with packages that already have fixes waiting, so this step matters more than it looks.
apt update && apt upgrade -y
apt autoremove -y
After the upgrade, check whether the system asks for a reboot. A new kernel only loads after a restart, so reboot now if the message tells you to, then log back in and confirm the server responds.
Turn on automatic security updates next. Ubuntu ships with unattended-upgrades, and enabling it means security patches install on their own instead of waiting for you to remember. Edit the config file or run the setup command, then check that the service is active.
apt install unattended-upgrades -y
dpkg-reconfigure -plow unattended-upgrades
Secure Ubuntu 24.04 VPS Setup: Create a Non-Root User
Stop using root for daily work. Root can delete anything with one typo, and attackers know the username already, so every login attempt against root starts halfway there. Create a normal user with sudo rights and use that account instead.
adduser deploy
usermod -aG sudo deploy
Pick a username that is not obvious. Short names like admin, user, or test appear in brute force lists, so choose something plain but less common. Set a long password for the account even though you plan to use SSH keys, because the password is a fallback if keys fail.
Test the new account before you go further. Open a new terminal, log in as the new user, and run a sudo command such as sudo whoami. If it asks for the password and returns root, sudo works. Stay logged in as root in your first window until the new account is proven.
Set Up SSH Keys and Disable Root Login
Password login is the weakest part of a default server. Bots try common passwords around the clock, and a weak password is only a matter of time. SSH keys replace the password with a key pair, and the private half never leaves your computer.
On your own computer, generate an Ed25519 key if you do not have one yet:
ssh-keygen -t ed25519 -C "deploy@your-server"
Copy the public key to the server with ssh-copy-id, then log in as your new user with the key to confirm it works. If the key login succeeds, you can tighten the SSH settings. This is the point where ubuntu 24.04 server hardening starts to pay off, because the rest of the steps assume password login is gone.
Open /etc/ssh/sshd_config and change these lines:
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
These three lines do most of the work: they disable root ssh login, turn off password login, and keep key login on. Save the file, check the config with sshd -t, and restart the SSH service only if the check passes. Then test a fresh login in a second window before you close anything. If you want a deeper pass over the same settings, follow our 15-step guide to harden a fresh Ubuntu server after you finish this setup.
Set Up the UFW Firewall
A firewall decides which ports the world can reach. On a fresh server, only SSH needs to be open at first. Everything else stays closed until you install a service that needs a port.
A ufw firewall setup takes three commands. Allow SSH first, so you do not lock yourself out, then turn the firewall on and check its status.
ufw allow OpenSSH
ufw enable
ufw status verbose
When you add a web server later, open ports 80 and 443. When you add anything else, open only the port that service needs, and close it again when the service goes away. A firewall rule you forgot is a door you left open.
If your provider has its own cloud firewall, use that too. The two layers do not conflict: the cloud firewall filters traffic before it reaches your server, and UFW filters what gets through. Set the same rules in both places so a mistake in one layer does not leave the server exposed.
Add Fail2ban to Block Brute Force Attempts
Even with password login off, bots still knock on SSH. Fail2ban watches the logs and blocks IP addresses that fail too many times in a row. It does not replace key login, it cuts the noise and stops the constant probing.
apt install fail2ban -y
systemctl enable --now fail2ban
Check that it runs with fail2ban-client status sshd. The default settings ban an address after a small number of failed logins, and that is enough for most servers. You can raise or lower the limits in a local jail config file, but keep the defaults until you have a reason to change them. Our Ubuntu 24.04 production security checklist covers fail2ban tuning alongside the other checks to run before a server goes live.
Check the fail2ban log once a week at first. If your own IP address gets banned after a typo, unban it from the console with fail2ban-client unban.
Set Up Backups and a Staging Server
A secure server still fails. Disks die, updates break configs, and a wrong command deletes files. Backups are the only fix for that, and they need to live somewhere other than the server itself.
Start with the files that matter: your web root, your database dumps, and the config files in /etc for SSH, the firewall, and any web server you run. Copy them to a second location on a schedule, and test a restore at least once. A backup you never tested is a guess, not a backup.
Keep a separate server for testing changes. When you want to try a new app version or a config change, test it there first instead of on the live server. Our guide to setting up a staging environment on a separate VPS shows how to mirror a live server so a bad deploy never touches production.
Write down your backup schedule and your restore steps in a short note. If you are away when something breaks, that note is what lets someone else bring the server back.
Keep the Server Patched After Setup
Setup is a starting point, not a finish line. New flaws appear in software all the time, and a server you set up once and forgot will fall behind within months. Put a weekly reminder in your calendar to log in, check for updates, and look at the logs.
Each visit, run the update command, check the firewall status, check fail2ban, and check disk space. Disk space fills up quietly from logs and backups, and a full disk stops services in ways that look like other problems. Clean old logs and old kernels when space gets tight.
Review your users once a month. Remove accounts for people who no longer need access, and remove old SSH keys from authorized_keys files. Old access is how forgotten accounts turn into open doors.
Your server is now in solid shape after this secure Ubuntu 24.04 VPS setup: patched, key-only SSH, firewall on, fail2ban watching, and backups planned. Add services one at a time from here, open only the ports each one needs, and keep the same habit of testing every change in a second window before you close the old one.