Hey guys,
I finally come around and started the journey of self-hosting and decided to get a VPS. After securing it, by making login via public-key default, I installed CasaOS and instantly regretted it since it now can be reached freely from the internet. A while ago, I read that you can limit the incoming connection to only VPN and ssh. And configured a Wireguard-VPN on the server via pivpn. But the server can still be reached from the internet. So what do I have to do now? Is it even the right choice, or am I missing something? How do you guys secure your VPS?
You can set up firewall rules that limit connections to certain ports. For example, you can disallow connections to everything but port 22 (which ssh runs on) to only allow access to the ssh server from outside. Same with VPN.
Read up on ufw (or iptables if you want to do it lower level and have a drink on hand).
iptables!
ufw makes it way nicer to use tho. I only use iptables as I have a configuration script I wrote way back in the day.
Yeah UFW is the way to go, but when you run into an embedded Linux device that only runs iptables and can only be accessed via ssh?
Well…. Keep that drink handy
Yes, but how often does that happen to the average person?
Deity of choice willing, never lol
Before you go too far into it and spend lots of time, I think most VPS services let you installed a new OS on their admin site so you can start again from scratch. If you’re not sure that is the right linux flavour, go for something else more mainstream so you can find lots of support online. Looking at the OS, I’m sure it might be good but I’m also sure you can install all the features very easily yourself, especially if it’s just using docker mainly.
I second UFW. I found this guide useful: https://www.digitalocean.com/community/tutorials/ufw-essentials-common-firewall-rules-and-commands. You might want to try tailscale as others use it for easily setting up vpn access but not used it myself. Also go for fail2ban or, for more assurance but harder work, try crowdsec too.
You could also use cloudflare dns and add IP and/or country restrictions to block all traffic before it gets to your VPS. I have a country filter and it’s crazy how many bots get blocked from all over!
Also before diving too deep into firewalls, which is a good idea none the less, just go to SSHd config and set
ListenAddress
to only listen on the VPN network address instead of public IP, and no one can connect via the public IP.Keep it simple, and add complexity only as needed.
I personally prefer using public key encryption over passwords for ssh authentication. There’s no need to rely on third-party VPN providers (like ZeroTier or Tailscale) or hosting your own “vpn server” for that purpose as ssh trafic is already encrypted.
The drawback of following the route you suggested is that you have to operate yet another service that could be misconfigured, potentially causing you to lose access to your server. If you’re keen on further restricting access, consider whitelisting your static(!) IP address, the IP address ranges associated with your provider or the ranges assigned to your country for an additional layer of security.
My preferred option is to have the VPS inside a VPC that blocks all external traffic by default. Then I can open up specific ports for specific IP ranges.
The reason I prefer this over a firewall configuration on the VPS itself is that the latter seems far more error-prone to me. For example, I’ve had problems in the past with ufw and Docker where container ports were still reachable even though access was denied via ufw.