Note

Security Hardening Your VPS Server

Locking down a VPS without making it annoying to use

Security Hardening Your VPS Server
Fig. 01 — Security Hardening Your VPS Server
A rented server is on the public internet from the minute it boots. Nobody is looking for you. Bots scan every address, all day, and yours is just one more.

I found this out from my own logs. In one audit I counted 814 requests for files like .env, .git and wp-config.php, all from one IP. Another 731 went to /admin and /wp-admin, on a server that has neither. That's just how the internet looks now.

What follows is what I did about it. These are not optional for anyone, its mandatory in my opinion. Each step closes a door that was open by default, and I've written them in roughly the order you should do them.

1. Fix SSH first

This is the door that matters most, so close it before anything else.

  • Use keys only. Set PasswordAuthentication no and KbdInteractiveAuthentication no in /etc/ssh/sshd_config. Test the key in a second terminal before you close the first.
  • Disable root login. Set PermitRootLogin no. Log in as a normal user and use sudo.
  • Move SSH off port 22. This doesn't make you safer against anyone who's actually after you. It makes your logs quieter, and quiet logs get read.

After this, failed SSH logins went from a steady stream to almost none.

2. Ban repeat offenders with fail2ban

Keys stop people getting in. fail2ban stops them from trying forever.

  • Put your settings in jail.local, never jail.conf. Updates overwrite jail.conf.
  • For SSH, ban after 3 failures in 5 minutes.
  • Turn on escalating bans: 24 hours, then 48, then 96, then permanent. Add a little randomness to ban times so bots can't learn the timing.
  • Put localhost on the allowlist.

If you want a dashboard, use one with its own login, or keep it bound to 127.0.0.1 and reach it through an SSH tunnel. Never put a security tool with no login on a public port.

3. Turn on a firewall, then learn how Docker gets around it

Turn on UFW, block all incoming traffic by default, and open only the ports you can explain. Write down why each one is open.

Then the part almost every guide leaves out: Docker ignores UFW. A container started with -p 8080:8080 is reachable from the internet even if UFW says that port is closed. Docker writes its own firewall rules, and they come before UFW's.

The fix is to publish ports on localhost only:

``yaml ports: - "127.0.0.1:3330:3000" ``

Better still, publish nothing. Put containers on a shared Docker network with your reverse proxy and let the proxy be the only way in. And check from outside the server: scan its public IP from another machine. The UFW rule list is not the same thing as what's actually exposed.

4. One public entry point, everything else private

Only two ports should be open to the world: 80 and 443, both going to a reverse proxy. Behind it:

  • Use HTTPS everywhere, with HSTS on.
  • Turn on your proxy's exploit blocking. It quietly answers most of those .env probes.
  • Put admin panels behind real identity checks. I put the proxy's own admin page behind Cloudflare Access, so nobody even reaches its login form without passing my identity provider first.
  • Use a private mesh network for private services. I use Tailscale. If only you need a service, it doesn't need a public address.

Most of what I run is only for me. Most of it is now unreachable from the internet.

5. Don't run containers as root, ever

Plenty of images run as root by default. If someone breaks into one of those, they're root inside it, and one bad volume mount away from root on the server.

  • Set user: in your compose files wherever the image supports it.
  • Mount the Docker socket read-only, and only into containers that genuinely need it.
  • Avoid privileged: true. If a container asks for it, find out why, and look for a narrower permission.

6. Harden the apps too

Locking down the server isn't the end of it. Every app has its own settings.

  • Use secure cookies. If an app sits behind HTTPS, its session cookies should be marked Secure. Some apps ship with this off so first-time setup over plain HTTP works. Turn it back on.
  • Don't reuse API keys and tokens. Give each integration its own key so you can revoke one without breaking the rest.

7. Check yourself on a schedule

What you configured once won't stay that way by itself. I wrote a small audit script that checks:

  • SSH settings and recent failed logins
  • fail2ban status and ban counts
  • the firewall rules and the ports actually listening
  • user accounts with UID 0, and odd processes or cron jobs
  • the proxy's logs for attack patterns
  • pending updates and disk space

An automation tool runs it on a schedule and messages me the results. A report you have to remember to run, you won't run.

8. Update often, but not blindly

  • Let the OS apply security updates.
  • For containers, sort them by risk before updating. Stateless apps can update any time. Proxies and anything holding data get a closer look. Databases get a backup first, every time.
  • Pin versions for anything that changes data on upgrade. latest combined with automatic updates means one-way database migrations nobody watched.

9. Get secrets out of your notes

This is the one I had to admit to myself. My notes app was full of API keys, bot tokens and admin passwords, pasted in during late-night setup sessions and never cleaned up. Every bit of hardening above is worth very little if one notes login gives all of it away.

Move secrets into a password manager. Rotate anything that has ever sat in plain text. Treat that pile of notes as already leaked.

10. Remove what you stopped using

When I switched to Tailscale, the old tunnel's containers were gone, but its networks, volumes, folders and one leftover service key were still on the server. Anything you aren't using is still something an attacker can use. When you stop using a service, remove all of it.

What this doesn't do

None of this is a security programme. It won't stop someone determined who's after you in particular, or a zero-day in your reverse proxy. What it does is take you out of the easy pile, the servers that get broken into because a password login was left on or a database port was public.

It took a few evenings. Mostly I just stopped leaving doors open.