A fresh Linux VPS is reachable from the whole internet on every port where something happens to be listening. A firewall changes that default: it blocks everything inbound and lets through only the services you choose, such as SSH, HTTP and HTTPS. It is one of the first things to set up on a new server, and one of the easiest to get wrong in a way that locks you out.
By the end of this guide you will have a working deny-by-default firewall on Ubuntu (UFW) or AlmaLinux/Rocky Linux (firewalld), you will know how to restrict a port to one IP address, and you will know the traps around IPv6 and Docker.
Where the firewall fits
Think of server security in layers:
- Network filtering upstream, which absorbs volumetric attacks before they reach your server. See what a DDoS attack is.
- The server firewall, which decides which ports accept connections.
- The service itself, for example SSH accepting keys only.
- The application and its users: passwords, updates, plugins.
Each layer covers gaps the others leave. Upstream filtering does not know that your database port should be private; a server firewall cannot help when a flood has already filled the line.
Before you begin
Find out what is listening right now:
sudo ss -tulpn
Write down the ports you actually need. For a typical web server that is 22 (SSH), 80 and 443. Then:
- Confirm which port SSH uses. It is 22 unless you have changed it.
- Keep a second SSH session open while you work. If a rule goes wrong, the open session usually survives and you can fix it.
- Use one firewall tool. UFW and firewalld both manage nftables rules; running both leads to rules that contradict each other.
Warning: Always allow SSH before you enable the firewall. Enabling a deny-by-default policy without an SSH rule will cut your connection, and you will need console access to get back in.
UFW on Ubuntu and Debian
UFW (Uncomplicated Firewall) is installed on Ubuntu Server by default. On Debian, install it with sudo apt install ufw.
Set the defaults and open the basics
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose
OpenSSH is an application profile that opens port 22/tcp. If you moved SSH to another port, allow that port instead, for example sudo ufw allow 2222/tcp. sudo ufw app list shows the profiles installed packages provide, such as Nginx Full.
Useful rules
sudo ufw allow from 203.0.113.10 to any port 3306 proto tcp # database from one app server only
sudo ufw allow 51820/udp # a UDP port, for example WireGuard
sudo ufw deny from 198.51.100.0/24 # block a whole network range
sudo ufw limit OpenSSH # throttle SSH brute force
ufw limit blocks an address that opens six or more connections within 30 seconds. That is plenty for a person and far too few for a password-guessing bot. Rules are evaluated in order, and the first match wins, so a deny added after a broad allow for the same port has no effect. Use sudo ufw insert 1 deny from 198.51.100.0/24 to put a block at the top of the list.
To remove a rule, list rules with numbers and delete by number:
sudo ufw status numbered
sudo ufw delete 4
Don't forget IPv6
If your server has an IPv6 address, a service closed only on IPv4 may still be reachable over IPv6. Check that /etc/default/ufw contains:
IPV6=yes
This is the default on Ubuntu, and UFW then creates every rule for both protocols. ufw status shows the IPv6 rules with (v6).
firewalld on AlmaLinux and Rocky Linux
firewalld is the standard tool on RHEL-compatible systems. It groups rules into zones; the network interface normally belongs to the public zone, which already allows SSH.
sudo dnf install firewalld
sudo systemctl enable --now firewalld
sudo firewall-cmd --get-active-zones
sudo firewall-cmd --list-all
Open HTTP and HTTPS permanently and load the change:
sudo firewall-cmd --permanent --add-service=http --add-service=https
sudo firewall-cmd --reload
Rules added without --permanent only last until the next reload or reboot. That is a handy safety net: test a rule at runtime, and when it works, save the whole runtime state:
sudo firewall-cmd --add-port=8080/tcp
sudo firewall-cmd --runtime-to-permanent
For finer control, use a rich rule. This allows MySQL only from one address:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.10" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reload
Zones in firewalld apply to IPv4 and IPv6 alike, so services you open are open on both unless a rich rule names a family.
Read the firewall log
When a connection fails and you are not sure whether the firewall is to blame, the log tells you. UFW logs blocked packets with the prefix [UFW BLOCK]:
sudo ufw logging low
sudo journalctl -k --since "10 minutes ago" | grep "UFW BLOCK"
Each line shows the source address (SRC=), the destination port (DPT=) and the protocol. If you see your own traffic being blocked on a port you thought was open, the rule is missing or sits below a deny. On firewalld, turn on logging of denied packets with sudo firewall-cmd --set-log-denied=all and read the same kernel log. Switch it back to off afterwards, because a busy server can produce a lot of entries.
UFW, firewalld or nftables?
| UFW | firewalld | nftables directly | |
|---|---|---|---|
| Default on | Ubuntu | AlmaLinux, Rocky | none (it is the engine underneath) |
| Learning curve | Low | Medium | High |
| Core idea | Simple allow/deny rules | Zones and services | Tables, chains and rules |
| Best for | Most single servers | RHEL-family servers | Complex setups, routers |
For a typical VPS, use the tool your distribution ships with. Writing raw nftables rules gives full control but makes it easy to forget something essential, such as ICMPv6, which IPv6 needs to work at all.
Common mistakes
Locking yourself out. The classic. Allow SSH first, keep a second session open and test a new login before closing the old one. The SSH security guide explains how to harden SSH itself.
Docker bypasses UFW. When you publish a container port with -p 8080:80, Docker inserts its own NAT and forwarding rules, and that traffic never passes through UFW's input rules. The port is open even if ufw status does not list it. Bind internal services to localhost (-p 127.0.0.1:8080:80) and put a reverse proxy in front. The Docker on Ubuntu guide covers this in detail.
Forgetting UDP or IPv6. Game servers, VPNs and DNS use UDP. A rule written as allow 51820 without a protocol covers both, but a /tcp rule does not.
Treating the firewall as the only defence. A firewall cannot protect a service that must be public. SSH keys, updates and strong application passwords still matter.
Test from the outside
After every change, test from another machine:
nc -zv YOUR_SERVER_IP 22
nc -zv YOUR_SERVER_IP 3306
The first should succeed, the second should time out unless you are on the allowed IP. On Windows, use Test-NetConnection YOUR_SERVER_IP -Port 22 in PowerShell.
In short
Deny everything inbound by default, allow SSH first, then open only the ports you use. Use UFW on Ubuntu and Debian and firewalld on AlmaLinux and Rocky Linux. Remember IPv6 and UDP, publish Docker ports on localhost, and test from outside after each change. Then continue with the full VPS security checklist.
Firewalls and Hostigan
Every Hostigan VPS comes with full root access, so you control the server firewall yourself with whichever tool suits your distribution. In front of it, always-on upstream DDoS filtering is included on every plan, so the two layers work together.
Frequently asked questions
Yes. DDoS filtering removes attack traffic before it reaches your server, but it cannot know which of your ports should be private. The server firewall decides which services are reachable at all.