A firewall blocks every connection to your server except the ones you allow. It's one of the simplest and most effective ways to protect a VPS, VDS or dedicated server.
Golden rule: allow SSH first
Always allow SSH before you turn the firewall on, or you could lock yourself out. If you changed your SSH port, allow that port instead of 22.
Ubuntu and Debian (UFW)
- Install UFW if it isn't already there:
apt install ufw -y - Allow SSH:
ufw allow 22/tcp - Allow web traffic if you run a website:
ufw allow 80/tcpandufw allow 443/tcp - Turn the firewall on:
ufw enable - Check what's allowed:
ufw status verbose
AlmaLinux, Rocky and CentOS (firewalld)
- Start the firewall:
systemctl enable --now firewalld - Allow SSH:
firewall-cmd --permanent --add-service=ssh - Allow web traffic:
firewall-cmd --permanent --add-service=httpandfirewall-cmd --permanent --add-service=https - Apply the changes:
firewall-cmd --reload - Check what's allowed:
firewall-cmd --list-all
More good habits
- Only open the ports you actually use. For example, keep database ports like
3306closed to the internet unless you really need remote access. - Install
fail2banto automatically block IP addresses that keep guessing passwords. - Use SSH keys and turn off password login when you're comfortable.
Locked yourself out?
It happens to the best of us. Open a ticket with your server's IP address and we'll help you get back in.
Our network includes DDoS protection on every virtual server plan and on Performance dedicated servers, but it isn't a replacement for a firewall on the server itself.