How to Secure a VPS: First 10 Steps After Setup

How to secure a new VPS in 10 steps: updates, a sudo user, SSH keys, disabling root and passwords, UFW, the Docker port trap, Fail2ban, auto-updates and backups.

Summarize this post with AI

A 20-minute read, or the key points in about 30 seconds, in the assistant you already use.

Opens in a new tab with the prompt filled in. Nothing passes through this site.

A new VPS is reachable from the whole internet the moment it boots. You get a public IP, a root login and an SSH port that bots start knocking on long before your first app is installed.

The good news: securing a VPS isn't a weekend project. Ten steps, done in the right order, close the doors most attacks walk through. The order matters more than people think, because two of these steps can lock you out of your own server if you do them too early.

This guide walks through the first 10 steps after setting up a Linux VPS: updates, a sudo user, SSH keys, SSH hardening, two firewall layers, the Docker port trap, Fail2ban, automatic security updates, closing unused services and backups. If you're still choosing a server, start with what a VPS is and when you need one.

The commands assume Ubuntu, and every setting comes from Ubuntu's server docs, Docker's docs and Hostinger's support pages. Some links here are affiliate links. They don't change your price.

Editorial cover showing a server rack behind an amber shield with a padlock, titled Secure a VPS
Most VPS attacks go after the same few doors: passwords, open ports and unpatched software. These ten steps close them.

Why does a new VPS need securing right away?

A fresh server is built to be easy to reach, not hard to break into. Root can usually log in with a password, every listening service is exposed, and nothing watches for repeated failed logins.

That's fine for the first five minutes. It's not fine for a server that will hold your n8n credentials, a database or client sites.

Here's the order I follow, and why it's this order:

  • Steps 1 to 4 make sure you can log in safely with a key before you take password and root access away.
  • Steps 5 to 7 limit what the internet can reach and slow down anyone guessing.
  • Steps 8 to 10 keep the server patched, lean and recoverable.
Checklist diagram of ten numbered step cards with amber ticks, grouped into three bands labelled Access, Network and Upkeep
Do access first, network second and upkeep last, so you never lock yourself out halfway.

Step 1: How do you update a new VPS?

Log in as root with the details from your provider's dashboard, then install every pending update:

bash
ssh root@YOUR_SERVER_IP
apt update && apt full-upgrade -y

If the update touched the kernel, Ubuntu leaves a file at /var/run/reboot-required. Reboot so the new kernel is actually running:

bash
[ -f /var/run/reboot-required ] && reboot

Provider images are often weeks or months old, so this first update can be large. Do it before you install anything else.

Step 2: How do you create a non-root sudo user?

Root can do anything, and every bot on the internet already knows its username. Create your own user and give it sudo rights instead:

bash
adduser lokesh
usermod -aG sudo lokesh

Pick a strong password when adduser asks. You'll still need it for sudo, even after password logins over SSH are turned off.

Open a second terminal and check the new account works before you go any further:

bash
ssh lokesh@YOUR_SERVER_IP
sudo whoami

If the last command prints root, your user has admin rights through sudo.

Step 3: How do you set up SSH key login?

An SSH key replaces a guessable password with a key file that never leaves your computer. Ubuntu's docs recommend the ed25519 algorithm for its shorter keys and lower processing cost.

Run these on your own computer, not on the server:

bash
ssh-keygen -t ed25519
ssh-copy-id lokesh@YOUR_SERVER_IP

Set a passphrase when ssh-keygen asks. If the key file is ever copied off your laptop, the passphrase is what stops it working.

ssh-copy-id adds your public key to ~/.ssh/authorized_keys on the server. Ubuntu's docs add one check: only your user should be able to write to that file.

bash
chmod go-w ~/.ssh/authorized_keys

Now log out and SSH in again. If it doesn't ask for your account password (it may ask for the key passphrase), key login is working.

Diagram of a laptop with a private key connected by an amber SSH arrow to a locked server labelled Public key
The private key stays on your computer. The server only stores the public half, so there is no password to steal from it.

Step 4: How do you disable root login and password authentication?

This is the step that locks people out, so keep your current SSH session open the whole time.

Ubuntu reads extra SSH settings from files in /etc/ssh/sshd_config.d/, and its docs point out that OpenSSH uses the first value it finds for most settings. Some provider images already put a file in that folder. Naming yours with a 00 prefix means it's read first, so your values win.

bash
sudo nano /etc/ssh/sshd_config.d/00-hardening.conf

Add these lines:

text
PermitRootLogin no
PasswordAuthentication no
KbdInteractiveAuthentication no
PubkeyAuthentication yes

Then test the configuration before you restart anything. Ubuntu's docs recommend sshd -t for exactly this, because a broken config can stop SSH from starting:

bash
sudo sshd -t
sudo systemctl restart ssh.service
sudo sshd -T | grep -iE 'permitrootlogin|passwordauthentication'

The last command prints the settings SSH is really using. You want to see permitrootlogin no and passwordauthentication no.

Now open a new terminal and log in again with your key. Only close the old session once the new one works.

Ubuntu also supports two-factor authentication for SSH with hardware keys or one-time codes. It's worth adding for a server several people log in to.

Step 5: How do you set up a firewall on a VPS?

A firewall decides which ports the internet can reach. On a VPS you can have two layers, and I'd use both.

Turn on UFW on the server

UFW is Ubuntu's default firewall tool, and Ubuntu's docs note it starts disabled. Allow SSH first, then web traffic, then switch it on:

bash
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
sudo ufw enable
sudo ufw status verbose

Allowing OpenSSH before ufw enable is the part that matters. Do it in the other order and the firewall cuts your own connection.

If you only ever log in from a fixed IP, you can go further and allow SSH from that address alone, as Ubuntu's docs show with sudo ufw allow proto tcp from YOUR_IP to any port 22.

Add the provider's firewall in front

A provider firewall filters traffic before it ever reaches your server. On Hostinger it's under Security, then Firewall, in the VPS dashboard. Hostinger's support page says a firewall group drops all traffic by default, so you add an Accept rule for each port you need, starting with SSH. Rule changes apply in two minutes or less, and connections that are already open aren't cut.

Keep the two layers in sync. A port has to be open in both before anything can reach it.

Step 6: Why does Docker bypass UFW, and how do you fix it?

This one surprises almost everyone who self-hosts apps. Docker's own docs say Docker and UFW are incompatible: when you publish a container port, the traffic is routed before it reaches the rules UFW uses. So a port you never opened in UFW can still be wide open to the internet.

Diagram of internet traffic passing a cloud firewall, then splitting: one path goes through UFW to the server, an amber path skips UFW straight to Docker
Published Docker ports skip UFW. The provider firewall still applies, and binding to 127.0.0.1 keeps the port off the internet entirely.

There are two practical fixes:

  • Publish container ports on 127.0.0.1 only, and put a reverse proxy like Caddy or Nginx in front to serve them over HTTPS on 443.
  • Rely on the provider firewall, which sits outside the server, so Docker can't change it.

In a compose file, the first fix looks like this:

yaml
ports:
  - "127.0.0.1:5678:5678"

My guide to self-hosting n8n with Docker on a VPS uses exactly this pattern behind Caddy, and the wider self-hosting apps on a VPS guide applies it to other tools.

Step 7: How do you install Fail2ban to block brute-force attempts?

Fail2ban reads log files, spots IP addresses with too many failed logins and blocks them in the firewall for a set time. Install it and turn on the SSH jail:

bash
sudo apt install fail2ban -y
sudo nano /etc/fail2ban/jail.local
text
[sshd]
enabled = true
maxretry = 5
findtime = 10m
bantime = 1h
bash
sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

Put your settings in jail.local rather than editing jail.conf, so package updates don't overwrite them. On systems that log only to the systemd journal and have no /var/log/auth.log, add backend = systemd to the sshd section.

Keep expectations honest. Fail2ban's own README says it can reduce the rate of incorrect login attempts but "cannot eliminate the risk presented by weak authentication." With password logins already off from Step 4, it mostly cuts log noise and slows scanners down.

Step 8: How do you turn on automatic security updates?

Ubuntu installs security updates on its own through the unattended-upgrades package. Its docs say the package is installed by default and runs once a day. Confirm it's on:

bash
cat /etc/apt/apt.conf.d/20auto-upgrades
sudo unattended-upgrade -v --dry-run

You want both lines in 20auto-upgrades set to "1". The dry run shows which updates it would apply without changing anything.

One gap: by default it won't reboot for kernel updates. Automatic-Reboot is false out of the box. If a nightly reboot is acceptable for your apps, set it in /etc/apt/apt.conf.d/50unattended-upgrades:

text
Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";

The time uses the server's own time zone, so check it with timedatectl first. If you'd rather reboot by hand, look for /var/run/reboot-required once a week.

Step 9: How do you find and close services you don't need?

Every listening service is a door. List them:

bash
sudo ss -tulpn

Go through each line. Anything listening on 0.0.0.0 or [::] is reachable from outside, unless a firewall blocks it. For each one, either you need it, it should listen on 127.0.0.1 only, or it should be removed:

bash
sudo systemctl disable --now SERVICE_NAME

Databases are the classic mistake. MySQL, Postgres or Redis rarely need to be public. Bind them to localhost and let your apps connect locally.

While you're here, Ubuntu's docs show a small extra: adding DebianBanner no to your SSH hardening file stops SSH from announcing your OS version to anyone who connects.

Step 10: How do you back up a VPS before something goes wrong?

Security includes recovery. If a bad update, a mistake or an attacker breaks the server, backups decide whether you lose an hour or a month.

Check what your provider gives you. Hostinger's support page, as of 11 November 2026, lists:

FeatureHow it works on Hostinger VPS
Weekly backupsOn by default, stored separately from the server, two kept
Daily backupsPaid upgrade, two daily copies kept on top of the weekly ones
SnapshotMade by hand, only one at a time, expires after one day
RestoreOverwrites the whole VPS and can't be cancelled once started
Comparison diagram with a Snapshot card showing a camera and a one-day timer, and a Backups card showing cloud, storage and calendar icons
A snapshot is a short undo button before a risky change. Scheduled backups are your real safety net.

Use a snapshot right before risky work, like a major upgrade, and rely on scheduled backups for everything else. Hostinger also notes that VPS backups aren't encrypted at rest, so keep a separate copy of databases and important files somewhere you control, such as object storage. The same thinking is behind my WordPress security checklist, where backups are part of security, not an afterthought.

A backup you've never restored is a guess. Restore one to a test server at least once.

Which VPS makes these steps easier?

Every step above works on any Linux VPS. A few provider features make it smoother: a cloud firewall outside the server, automatic backups, snapshots and a browser console for when SSH goes wrong.

I recommend Hostinger's KVM VPS for self-hosting because it includes all four in the dashboard covered above. My Hostinger review goes through the plans, and if you're hosting n8n specifically, my roundup of n8n hosting providers compares other options.

Frequently asked questions

What is the first thing to do after buying a VPS?▾
Update the system. Run apt update and apt full-upgrade, then reboot if a kernel update needs it. Provider images are often weeks or months old, so they can be missing security fixes on day one.
Should I change the SSH port from 22?▾
It reduces log noise from bots that only scan port 22, but it isn't real protection, since a port scan finds the new port quickly. SSH keys with password login turned off matter far more. If you do change it, open the new port in UFW and your provider firewall before restarting SSH.
Is UFW enough to secure a VPS?▾
It's a good start, but not on its own. Docker's docs say published container ports bypass UFW, so on a server running Docker you also need ports bound to 127.0.0.1 or a provider firewall in front. UFW also does nothing about weak passwords or outdated software.
Do I still need Fail2ban if I use SSH keys?▾
It's less important but still useful. With password login off, brute-force attempts can't succeed, and Fail2ban mostly cuts log noise and blocks scanners. It's more valuable for services that still use passwords, like a web login.
How do I get back in if I lock myself out of SSH?▾
Use the browser console in your provider's VPS dashboard. It connects to the server directly, without SSH. Fix the file you changed, run sudo sshd -t to check it, then restart ssh.service.
Where should I host apps like n8n once the VPS is secure?▾
Run them in Docker with ports bound to 127.0.0.1 and a reverse proxy handling HTTPS. My step-by-step guide covers the full setup: [Self-host n8n with Docker on a VPS](/blog/self-host-n8n-docker-vps)

If you only have ten minutes today, do Steps 3 and 4: key login on, passwords and root login off. That one change shuts out most automated attacks. Then add the firewalls, check your Docker ports and confirm backups are running before you put anything important on the server.

Share