Dev.to Security 🔐 Cybersecurity 👁 0 📖 4 min read

How to harden a new Ubuntu VPS without locking yourself out

A new VPS is on the public internet from the first minute, and bots start guessing its root password almost as fast. Hardening it takes about fifteen minutes. The risky part is that the same steps that keep attackers out

A new VPS is on the public internet from the first minute, and bots start guessing its root password almost as fast. Hardening it takes about fifteen minutes. The risky part is that the same steps that keep attackers out can also keep you out, if you do them in the wrong order.

I've run my own VPSs for about seven years. This is the checklist for a fresh Ubuntu server, written in the order that avoids a lockout. It was tested on Ubuntu 24.04 and works the same on 22.04.

Two rules before you start

  1. Keep one SSH session open the whole time. Test every change from a second terminal. If the new login fails, the old session is still there to fix it.
  2. Know where your provider's web console is. It works even when SSH doesn't, and it is your way back in if everything else goes wrong.

The commands below assume you are logged in as root on a new server.

1. Update the system

apt update && apt upgrade -y

If the kernel was updated, the server needs a reboot:

[ -f /var/run/reboot-required ] && echo "Reboot needed"

2. Create a user with sudo

Working as root all day means every typo runs with full power. Create a normal user and give it sudo. I'll call it deploy.

adduser deploy
usermod -aG sudo deploy

3. Give that user your SSH key

If you already log in as root with a key, copy it to the new user:

rsync --archive --chown=deploy:deploy ~/.ssh /home/deploy

If you log in as root with a password, run this on your own computer instead:

ssh-copy-id deploy@your_server_ip

Now open a second terminal and test it. Do not skip this.

ssh deploy@your_server_ip
sudo whoami

The second command should print root. If it does, the new user works and you have a safe way in.

4. Turn on the firewall, SSH first

UFW blocks everything you have not allowed. If you enable it before allowing SSH, you cut your own connection. So the order matters:

sudo ufw allow OpenSSH
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status verbose

If the server will host a website, open the web ports too:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

5. Turn off password logins

Passwords are what the bots are guessing. Once key login works, switch passwords off.

Don't edit the main sshd_config. Put your settings in their own file, so they are easy to find and easy to remove:

sudo nano /etc/ssh/sshd_config.d/00-hardening.conf
PasswordAuthentication no
KbdInteractiveAuthentication no
PermitRootLogin prohibit-password
PubkeyAuthentication yes

The file name matters. SSH uses the first value it reads for each setting, and it reads these files in alphabetical order. Many cloud images ship a 50-cloud-init.conf that turns password logins back on. A file starting with 00 is read before it and wins.

PermitRootLogin prohibit-password still lets root in with a key. When you are sure the deploy user does everything you need, you can change it to no.

Check the file for mistakes before you apply it. No output means no errors:

sudo sshd -t

Then confirm what SSH will really use:

sudo sshd -T | grep -Ei 'passwordauthentication|permitrootlogin'

Apply it:

sudo systemctl reload ssh

Go back to your second terminal and log in again as deploy. If it works, you are done with SSH. If it doesn't, your first session is still open, and this puts everything back:

sudo rm /etc/ssh/sshd_config.d/00-hardening.conf
sudo systemctl reload ssh

6. Ban repeat offenders with fail2ban

With passwords off the guesses can't succeed, but they still fill your logs. fail2ban blocks an address after a few failed tries.

sudo apt install fail2ban -y
sudo nano /etc/fail2ban/jail.local
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5

[sshd]
enabled = true
sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

If you ever ban yourself, log in through the provider console and run:

sudo fail2ban-client set sshd unbanip YOUR_IP

7. Let security updates install themselves

Most Ubuntu images have this on already. It takes a minute to confirm:

sudo apt install unattended-upgrades -y
cat /etc/apt/apt.conf.d/20auto-upgrades

Both lines in that file should end in "1". If they don't:

sudo dpkg-reconfigure --priority=low unattended-upgrades

8. See what is listening

Last check. List every port the server has open:

sudo ss -tulpn

On a fresh server you should see SSH and little else. If something is listening that you don't recognise, find out what it is before you put anything important on the machine.

The short version

  • Keep one session open and test from a second one.
  • Update, then create a sudo user and test its key.
  • Allow SSH in the firewall before you enable it.
  • Put SSH settings in a 00- file, run sshd -t, reload, test.
  • Add fail2ban and confirm automatic security updates.
  • Check open ports with ss -tulpn.

Doing this from a phone

I often set servers up away from my laptop, and typing these commands on a phone keyboard is where mistakes happen. That is why I built VpsPilot, an Android app that runs this kind of checklist as a guided workflow. It shows every command before it runs, checks the SSH config before reloading, and restores the old settings if the new ones fail.

It is a paid app with a 14-day free trial, then $2.99 a month or $19.99 a year.

The checklist above works with no app at all. If you spot a step I should add, tell me in the comments.

📰 Read the original article on Dev.to Security

Originally published by Dev.to Security. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.