Managed infrastructure for SMBs
Linux VPS Firewall Basics for Small Businesses: UFW, nftables and an Allow-List of Ports
The short answer
A firewall on a Linux VPS should do one simple job: block every incoming connection except the few ports your business actually uses. For most small-business servers that means SSH for administration, 80 and 443 for a website, and nothing else open to the whole internet. On Ubuntu the easiest tool is UFW; on Debian and many other distributions nftables is the built-in framework. This guide walks a small team through building an allow-list with either one, without locking yourself out of your own server. The one rule to remember before anything else: allow SSH first, then turn the firewall on.
1. Write down what the server actually needs
Before changing anything, list what has to reach the server from outside and from where. A typical list looks like this: SSH (port 22, or your custom port) from your office or VPN only, HTTP and HTTPS (80 and 443) from everyone for a website, and maybe one application port such as a Minecraft Java server on 25565/tcp. Everything not on the list stays closed. Databases, admin panels, caches and monitoring agents almost never need to be open to the internet. Writing the list first turns the firewall into a short, reviewable document instead of a pile of rules nobody remembers adding.
2. Check what is listening right now
Run sudo ss -tulpn to see every TCP and UDP port the server is listening on and which program owns it. Anything bound to 127.0.0.1 or ::1 is only reachable from the server itself. Anything bound to 0.0.0.0, :: or * is listening on every address. Compare this with your list from step 1. If a service is listening publicly but should not be, the best fix is to configure it to listen on localhost only; the firewall is the second layer, not the only one.
3. Keep a way back in before you start
A firewall mistake on a remote server can cut off your own SSH session. Before you enable anything, make sure you know how to reach the server's out-of-band console through your provider's control panel, keep your current SSH session open while you test, and open a second session to confirm new connections still work after each change. If you are not sure you have console access, stop and find out first.
4. Pick one tool and stick to it
UFW (Uncomplicated Firewall) is a front end that writes the low-level rules for you and is the usual choice on Ubuntu. nftables is the modern Linux packet filtering framework and the default on current Debian releases; you write the ruleset yourself in one file. Both work well for a single VPS. What causes trouble is mixing them: running UFW and a hand-written nftables ruleset (or old iptables scripts) at the same time makes it hard to know which rule is really in effect. Choose one, disable the others, and document the choice.
5. UFW: allow SSH, set defaults, then enable
On Ubuntu, install UFW if needed with sudo apt install ufw. Add the SSH rule before enabling the firewall. If you have a fixed office or VPN address, allow SSH only from there (replace the example address with your own):
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp
If you must allow SSH from anywhere, use sudo ufw limit 22/tcp instead of a plain allow. A limit rule allows the connection but denies an address that tries to open 6 or more connections within 30 seconds, which slows down password-guessing bots. Then set the defaults and enable:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw enable
sudo ufw status verbose
If you changed the SSH port, use that port number in the rule instead of 22. UFW handles IPv4 and IPv6 together by default, so the same rule covers both.
6. UFW: open only the ports on your list
Add one rule per entry on your list, with the protocol and a comment so the next person knows why it exists:
sudo ufw allow 80/tcp comment 'website'
sudo ufw allow 443/tcp comment 'website https'
sudo ufw allow 25565/tcp comment 'minecraft java'
Use sudo ufw status numbered to see the rules in order and sudo ufw delete 3 to remove rule number 3. To remove a rule that applies to both IPv4 and IPv6 in one go, repeat the original rule with delete in front, for example sudo ufw delete allow 25565/tcp. sudo ufw show listening lists the ports in use next to the rules that affect them, which is a quick way to spot something open that should not be.
7. nftables: one ruleset file with a default drop
On Debian, nftables reads /etc/nftables.conf at boot. A small, readable allow-list for a web server looks like this (adjust the SSH port and add your own application ports):
flush ruleset
table inet filter {
chain input {
type filter hook input priority 0; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto { icmp, ipv6-icmp } accept
tcp dport 22 accept
tcp dport { 80, 443 } accept
}
chain forward {
type filter hook forward priority 0; policy drop;
}
chain output {
type filter hook output priority 0; policy accept;
}
}
The inet table covers IPv4 and IPv6 together. The established and related rule lets replies to your own connections back in, and the ICMP line matters more than it looks: IPv6 depends on ICMPv6 to work at all, so dropping it can quietly break IPv6 on the server. To limit SSH to one address, replace the SSH line with ip saddr 203.0.113.10 tcp dport 22 accept.
8. nftables: check, load and enable at boot
Test the file before loading it, so a typo does not leave you with a half-applied ruleset:
sudo nft -c -f /etc/nftables.conf
sudo nft -f /etc/nftables.conf
sudo nft list ruleset
sudo systemctl enable nftables
The -c option checks the file without applying it. Load it while your current SSH session is still open, confirm a new SSH session works, and only then enable the service so the ruleset comes back after a reboot. Reboot once during a quiet window to prove it.
9. Watch out for Docker and services that open their own ports
Docker manages its own packet filtering rules, and a container port published on all addresses can be reachable from the internet even when UFW looks like it blocks that port. Publish container ports on 127.0.0.1 and put a reverse proxy in front, as described in our Docker on a Linux VPS guide and the reverse proxy and HTTPS guide. After any new install, run sudo ss -tulpn again and test the server's ports from a machine outside your network, not from the server itself.
10. Log, review and keep the list current
Turn on basic logging with sudo ufw logging on; blocked packets go to the kernel log, and on Ubuntu usually also to /var/log/ufw.log. With nftables, add a counter or a log rule to the chains you want to watch. Review the rules whenever you add or remove a service, and on a schedule even when nothing changed: every open port should still map to a line on your list from step 1. Keep a copy of the ruleset or the UFW rule list with your server notes and backups, so a rebuilt server gets the same protection on day one.
Quick checklist
- List of required ports, protocols and source addresses written down
- Listening services checked with ss, internal ones bound to localhost
- Provider console access confirmed and a second SSH session open
- One firewall tool chosen; no mix of UFW, nftables and old iptables scripts
- SSH allowed (ideally from your office or VPN only) before the firewall is enabled
- Default deny incoming, allow outgoing
- Only the ports on your list open, each rule commented
- nftables file checked with nft -c before loading, service enabled at boot
- Docker published ports bound to localhost behind a reverse proxy
- Logging on, rules reviewed after every change and on a schedule
A firewall is one layer. Pair it with securing SSH on a Linux VPS, regular backups and snapshots and monitoring and alerts so a mistake or an attack is noticed and recoverable.
Next steps
GlassHosting is a managed service and infrastructure provider: we run your servers on our own network, so you can run your business. If you would rather not maintain firewall rules, SSH hardening and updates yourself, read what managed hosting includes and managed vs unmanaged VPS, browse the plans, then tell us what you run or email sales@glasshosting.com.
Host with GlassHosting
Order a plan or browse the knowledgebase for deeper how-tos.