So, you want to self-host?
30 November 2026
•9 min read

Meet Bob. He had recently ventured into self-hosting. While still in the development phase of his project, he logged on to his VPS dashboard and noticed a huge spike in outbound traffic from his newly rented server. He panicked. He SSHed into his server, copied the auth logs to his local computer, and ran tcpdump to capture the nature of the outbound traffic. He then switched the server off and deleted it (a mistake he would soon come to regret).
Next, he rotated every credential, key, and token that had been on that server and began analysing the logs and the network capture. He noticed that the logs from around the time of the spike were missing, so he could only see auth logs from after that. The network capture showed that his server was sending a flood of SYN packets to another server (about 40,000 packets per second, each about 1,024 bytes). His server was being used to DDoS another.
SSH password-based login was enabled by default, so he inferred that someone had kept trying to log in for about two days after he created the server, until they gained access and altered the logs to cover their tracks. This was, at best, a good guess. He couldn't perform further analysis because he had no snapshot and had already deleted the server.
Don't be Bob. In the age of AI, where speed is touted as paramount, it is easy to move fast and make mistakes. I decided to map out some steps to secure a VPS for anyone looking to start self-hosting.
1. Set Up an SSH Key
Typically, whenever you try to set up a VPS with any provider, you are presented with the option of creating an SSH key either during the server setup phase or after. Whichever you pick, you will need to have an SSH key lying around somewhere (sometimes the VPS provider hands you one). To create one:
After running this command, you'll be met with three prompts. The first asks you to optionally specify the file in which to store the generated key. Hit Enter to accept the default location (usually ~/.ssh/id_ed25519). The next two prompts ask you to enter and re-enter a passphrase to protect the private key, use a strong passphrase.
Once completed, the command creates two files in ~/.ssh. One is the private key (~/.ssh/id_ed25519), which should never leave your computer, and the other is the public key (~/.ssh/id_ed25519.pub), which you can safely upload to your server.
To move the public key to the server, run the following (assuming you stuck with the default file name):
Replace user and remote_host with the user created during the server setup and the IP of the server respectively.
2. Harden the SSH Daemon
After the key setup, edit the SSH config file (/etc/ssh/sshd_config) on the server and update the following parameters (This is the most important step in this write-up):
This blocks root login over SSH and SSH password authentication.
check the config for syntax errors:
then restart the sshd service.
Warning
Keep your current SSH session open until you confirm that key login works from a second terminal. If something is wrong, you can still fix it from there.
Note
On some linux distros, sshd_config includes any files in /etc/ssh/sshd_config.d/ near the top of the effective config, and for SSH the first value found wins. Some cloud images ship a drop-in file (for example 50-cloud-init.conf) that sets PasswordAuthentication yes, which would silently override your edit. Check the effective values:
You should see:
If either of them still says yes, find the file that overrides it:
3. Configure Kernel Security Parameters
These settings harden the server’s network stack against specific attacks, such as IP spoofing, ICMP redirect and source-routing abuse, SYN floods, and Smurf amplification. Configure them by adding the following parameters to a new file, /etc/sysctl.d/99-hardening.conf :
Then to apply the settings without rebooting:
| Setting | Value | Effect |
|---|---|---|
net.ipv4.conf.all.rp_filter | 1 | Drops packets whose source ip wouldn't be routed back out the arrival interface. Prevents IP source spoofing, which is used in DDoS and reflection attacks. Read more. |
net.ipv4.conf.default.rp_filter | 1 | Same as above, applied to interfaces created later. |
net.ipv4.conf.all.accept_redirects | 0 | Prevents ICMP redirect routing-table poisoning, which leads to man-in-the-middle or traffic black-holing. Read more. |
net.ipv6.conf.all.accept_redirects | 0 | Same as above, but for IPv6. |
net.ipv4.conf.all.accept_source_route | 0 | Prevents source-routed packets, which are used for firewall bypass and address spoofing. |
net.ipv6.conf.all.accept_source_route | 0 | Same as above, but for IPv6. |
net.ipv4.tcp_syncookies | 1 | Defends against SYN floods using cryptographic cookies. Read more. |
net.ipv4.icmp_echo_ignore_broadcasts | 1 | Ignores pings sent to broadcast addresses. Prevents Smurf amplification attacks. Read more. |
4. Install and Configure UFW
Uncomplicated Firewall (UFW) is a user-friendly command-line interface for managing the Linux firewall. Basically, it is a frontend for iptables. To install UFW (for example, on Debian), we run:
After installation, we allow access to the ports that need to be public by running(e.g ports 22, 80 and 443):
Then we enable UFW by running:
Warning
Allow the SSH port (22) before enabling UFW to prevent locking yourself out of your server.
Warning
ports published by Docker (for example -p 8080:80) bypass UFW's rules, so a container can be reachable from the internet even if UFW hasn't allowed that port. If you run Docker, bind published ports to localhost (-p 127.0.0.1:8080:80) and put a reverse proxy in front.
5. Install and Configure Fail2Ban
Fail2Ban is a Linux framework that monitors the system's auth and application logs in real time and blocks IP addresses carrying out brute-force or automated attacks. By disabling password-based SSH authentication (in step 2), we made brute-force attacks significantly harder. Fail2Ban simply adds defence in depth. To install Fail2Ban (e.g., on Debian), we run:
To configure Fail2Ban's SSH protection, we edit its config file /etc/fail2ban/jail.local:
Then restart the Fail2Ban service.
Here, maxretry shows how many authentication attempts an IP makes before it is banned, and bantime signifies how long the IP remains banned.
6. Automatic updates
The rate at which new vulnerabilities are being found is at an all-time high. Typically, patches for them are released as soon as possible. If you want to self-host, it's your duty to make sure your server and everything on it is up to date with the latest security patches. Keeping up with these patches is tedious, so we turn to automatic updates. On Debian or Debian-based distros, the package unattended-upgrades is used (other distros have their equivalents).
First, we install the package:
Then we enable automatic updates by ensuring the file /etc/apt/apt.conf.d/20auto-upgrades contains these two lines:
Finally, we set our preferences through its main config file, /etc/apt/apt.conf.d/50unattended-upgrades. The config options are mostly commented out, and by default, only security updates get installed, which is sufficient for us.
Even though the updates are automatic, we may need to restart the server for some of them to take effect (e.g., kernel patches and services that are already running). To enable automatic reboots, uncomment and set the following lines:
Warning
Depending on your uptime requirements these options might be okay or terrible
This enables automatic reboots and schedules them for 2 a.m. in the server's local time zone.
If something looks wrong
If you notice something suspicious on your server(such as a spike in outbound traffic), work in this order:
- Don’t delete the server. Take a snapshot first (most providers offer this), so you can analyse it later.
- Cut it off from the network. Use your provider’s firewall or console to block traffic, rather than powering it off.
- Copy the evidence off the server. Grab
/var/log/auth.log(or journalctl output). - Rebuild from a clean image and rotate every credential, key, and token that was on the old server.
Following these steps, you'll have a solid security baseline for your VPS. To go further, you could set up automatic off-server backups, and look into the OpenTelemetry and Grafana stack for monitoring, alerts and log management.
If your server runs Debian (Trixie or Bookworm) and you're comfortable with Ansible, you're in luck. I turned all of these steps into an Ansible role that applies them in a single run, so you don't have to do them manually. You can find it here.
Black-Lotus builds, secures, and maintains custom software for businesses. If you need help with a project, get in touch.
