A fresh VPS with a public IP starts getting SSH login attempts within minutes of going live. You don't need fancy tooling to deal with that. Updates, key-based login, a firewall and fail2ban cover the basics. The order matters, though: one bad SSH setting can lock you out of your own server. So you confirm the key works first, and only then turn off passwords.

The order of operations (and how not to lock yourself out)

The golden rule: never close your current SSH session until you've confirmed you can log in from a new one. Open a second terminal window and connect from there. Once the new session works, you can close the old one. If you do get locked out, you'll have to recover through your provider's console or support.

This guide assumes you already log in with an SSH key, as covered in How to connect to a VPS over SSH.

Step 1. Update the system

Start by updating packages and enabling automatic security updates. These commands are for Ubuntu 24.04 and Debian 12:

sudo apt update && sudo apt upgrade -y
sudo apt install -y unattended-upgrades
sudo dpkg-reconfigure -plow unattended-upgrades

unattended-upgrades installs security patches on its own. After a big kernel update you should still reboot and check that your services came back up.

Step 2. Create a sudo user

Working as root all the time is risky: any mistake hits the whole system immediately. Create a regular user, give it sudo and copy your key over. The example below uses a user called deploy and assumes your key is already in root's ~/.ssh/authorized_keys:

sudo adduser deploy
sudo usermod -aG sudo deploy
sudo mkdir -p /home/deploy/.ssh
sudo cp ~/.ssh/authorized_keys /home/deploy/.ssh/
sudo chown -R deploy:deploy /home/deploy/.ssh
sudo chmod 700 /home/deploy/.ssh
sudo chmod 600 /home/deploy/.ssh/authorized_keys

Open a new terminal window, log in as the new user and make sure sudo works:

ssh deploy@203.0.113.10
sudo -v

Step 3. Disable password and root login

Put your OpenSSH settings in a separate file under /etc/ssh/sshd_config.d/ instead of editing the main sshd_config. Ubuntu 24.04 and Debian 12 include that directory by default. Files are read in alphabetical order, and for each option the first value found wins. That's why the file is named 00-hardening.conf: even if cloud-init dropped in something like 50-cloud-init.conf with PasswordAuthentication yes, your setting still takes effect.

sudo tee /etc/ssh/sshd_config.d/00-hardening.conf > /dev/null <<'EOF'
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes
PermitRootLogin no
EOF

Check the syntax and the effective values before reloading the service. sshd -t prints nothing if everything is fine, and sshd -T dumps the values actually in use:

sudo sshd -t
sudo sshd -T | grep -E 'passwordauthentication|kbdinteractiveauthentication|permitrootlogin'

Apply the change with reload so existing sessions stay open:

sudo systemctl reload ssh

Step 4. Set up the ufw firewall

ufw (Uncomplicated Firewall) is a friendly wrapper around netfilter rules. Add the SSH rule before you enable the firewall, or enabling it will drop your current connection. If sshd listens on a non-standard port, allow that port explicitly, for example sudo ufw allow 22022/tcp.

sudo ufw allow OpenSSH
sudo ufw enable
sudo ufw status verbose

Only open ports 80 and 443 if you actually run a web server: sudo ufw allow 80/tcp and sudo ufw allow 443/tcp. Keep everything else closed until a service really needs it.

Heads up: Docker publishes ports through its own iptables rules and can bypass ufw. If you run containers, check open ports from outside the server, not just ufw status. More on that in Docker on a VPS.

ufw rules describe what's allowed in, but they don't tell you what's actually listening. Check both. ss lists every open TCP port and the process holding it:

sudo ss -tlnp

If you see a port you didn't mean to open, find the process in the users column and stop or remove it. A service listening behind a port that ufw blocks can't be reached from outside, but it's still running on the box and can become a way in if it has a vulnerability.

Step 5. Install fail2ban

fail2ban watches your logs and temporarily bans addresses that fail to log in too often. It doesn't replace keys, but it cuts a lot of noise from the logs and the load from brute-force bots.

sudo apt install -y fail2ban python3-systemd

Keep your local settings in jail.local. jail.conf gets replaced when the package updates, so any edits there will be lost. Create a minimal config:

sudo tee /etc/fail2ban/jail.local > /dev/null <<'EOF'
[DEFAULT]
bantime  = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1

[sshd]
enabled = true
port    = ssh
backend = systemd
EOF

If you have a static IP, add it to ignoreip so a few typos don't get you banned. If SSH runs on another port, put it in the port field. The backend = systemd line matters: Debian 12 has no /var/log/auth.log, and without it fail2ban won't start. It works fine on Ubuntu 24.04 too. Now start the service and check the jail:

sudo systemctl enable --now fail2ban
sudo fail2ban-client status sshd

To unban an address, use unbanip. The address below is just an example:

sudo fail2ban-client set sshd unbanip 198.51.100.7

Step 6. Verify everything

Run through this quick checklist:

  • key login works for the deploy user;
  • sudo -v runs without errors;
  • sudo sshd -T shows passwordauthentication no and permitrootlogin no;
  • sudo ufw status shows OpenSSH allowed, plus any other ports you need;
  • sudo fail2ban-client status sshd shows an active jail.

To see recent login attempts, check the journal:

sudo journalctl -u ssh --since today | tail -n 50

What else to do

  • back up your data and configs, and keep the backups off the server;
  • install only the packages and services you actually use;
  • give sudo only to users who need it;
  • keep an eye on free disk space and update status.

Running a website? Follow the separate guide on hosting a website on a VPS with Nginx and SSL. If you get stuck on setup, reach out to Pulsar support.

Wrapping up

Server security is a few simple layers stacked together: keys instead of passwords, no root login, a firewall that only opens the ports you need, and fail2ban against brute force. Stick to the order of operations and always test a new session before closing the old one.