A new VPS usually ships with a default login, password authentication switched on, and no firewall rules at all. Anyone on the internet can reach its SSH port within minutes of the first boot, and automated scanners will try common usernames all day long. This guide covers how to harden fresh ubuntu server installs before you point a domain at them or move real data onto the machine. The steps assume Ubuntu 22.04 or 24.04, a server you can still reach through your provider console, and about an hour of focused work.
Work through the sections in order, and treat the result as your ubuntu server security checklist for every machine you spin up afterwards. Nothing here needs paid software, and every step can be undone from the console if you lock yourself out.
Before you open the firewall
Do the setup work before you install any public-facing app. Open your provider console in a browser tab and keep it open for the whole session, since console access is your way back in if SSH breaks. Confirm you can log in as root or through the console, note the server IP address, and take a snapshot if your provider offers one. A snapshot gives you a clean rollback point, and a real backup routine matters even more once the server holds data you care about. If you have not settled on backup tooling yet, this comparison of Linux server backup tools including Restic, Borg, and Duplicacy walks through the trade-offs.
Next, check what is already listening. Run ss -tlnp to list open ports and the processes behind them. A fresh image should show little besides SSH. Anything else deserves a question: did you install it, and does it need to face the internet?
User and SSH lockdown
Start by creating a normal user and giving it sudo access:
adduser deployusermod -aG sudo deploy
Then copy your public key into that account with ssh-copy-id deploy@SERVER_IP and confirm you can log in as that user without a password prompt. Once ssh key authentication works from a second terminal window, disable password logins for good. Edit /etc/ssh/sshd_config and set these lines:
PermitRootLogin noPasswordAuthentication noPubkeyAuthentication yes
Restart SSH with sudo systemctl restart ssh, then test again from the second window before you close the first one. This second-session test is the single most important habit in this whole guide. If the new settings break logins, the still-open first session lets you fix the file instead of filing a support ticket from the console.
Changing the SSH port from 22 to something less obvious cuts down scanner noise, but treat it as noise reduction rather than real protection. Scanners find open ports quickly, so a moved port never replaces key-only logins and a firewall. If several people manage the server, store keys and credentials properly instead of passing them around in chat. A comparison of secrets managers including Vault, AWS Secrets Manager, and Doppler covers the options worth considering.
Firewall and fail2ban
Ubuntu ships with UFW, a front end for iptables that keeps firewall rules readable. Allow SSH first, then switch the firewall on:
sudo ufw allow OpenSSHsudo ufw enablesudo ufw status verbose
The default policy denies incoming traffic and allows outgoing traffic, which is the right starting point. Open extra ports one at a time, only for services you actually run, and write down why each port is open. A basic ufw fail2ban setup adds a second layer: Fail2ban watches log files for repeated failed logins and blocks the offending address for a while. Install it with sudo apt install fail2ban, then create /etc/fail2ban/jail.local with an [sshd] section enabled and using the systemd backend. Check its work with sudo fail2ban-client status sshd after a few days.
Updates and unattended upgrades
Apply pending updates before you do anything else: sudo apt update && sudo apt upgrade. Then install unattended-upgrades so security patches land without you remembering to run them. On Ubuntu the package usually arrives with the base system, and you can check its schedule in /etc/apt/apt.conf.d/50unattended-upgrades. Decide whether automatic reboots are acceptable for your workload and set the reboot window to a quiet hour if they are. A server that never reboots can run a patched kernel in name only, since the running kernel only changes at boot.
How to harden fresh ubuntu server installs: the 15-step checklist
Run through this list top to bottom on every new machine. Most items take a minute each once you know the commands.
- Create a non-root user with sudo access and use it for daily work.
- Set up SSH key authentication for that user and confirm it works.
- Disable root login over SSH with
PermitRootLogin no. - Disable password authentication with
PasswordAuthentication no. - Consider moving SSH off port 22 to reduce scanner noise, and update the firewall rule to match.
- Enable UFW with a default-deny incoming policy and open ports one at a time.
- Install and enable Fail2ban for the SSH service.
- Turn on unattended security upgrades and set a reboot window.
- Remove software you do not need, starting with anything listening on a port you cannot explain.
- Disable unused services with
systemctl disable --nowafter checking what they do. - Set the timezone and confirm time sync with
timedatectl, since logs and certificates depend on a correct clock. - Confirm log rotation is active so a full disk never takes the server down.
- Schedule backups and test a restore at least once before the server holds anything important.
- Run each application under its own user account so one compromised app cannot read the others.
- Write down what you changed, and re-check the list after any major change to the server.
What to check weekly
Hardening is a starting state, and servers drift. Once a week, spend ten minutes on these checks. Review failed logins with sudo journalctl -u ssh --since "7 days ago" | grep -i failed and look for patterns rather than single attempts. Run sudo ufw status verbose and sudo fail2ban-client status sshd to confirm both tools are still running. Check pending updates with apt list --upgradable, confirm disk space with df -h, and audit the user list with getent passwd and lastlog to spot accounts you forgot about. For a deeper production pass on the current LTS release, this Ubuntu 24.04 production security checklist covers ground beyond this starter list, including kernel settings and audit tooling.
If the server runs deployments, the weekly review should also cover the pipeline side. Stolen deploy keys and overly broad CI permissions undo server hardening fast, so these CI/CD pipeline hardening steps belong in the same routine. Once the checklist is done and the weekly habit is in place, your next job is keeping the setup that way, since a hardened server that nobody watches slowly slides back toward its default state.