Firewall rules explained
A Linux firewall checks each packet against an ordered list of rules and falls back to a default policy when nothing matches; a stateful firewall also remembers connections, so replies to traffic you started are allowed automatically. On CheapVPS the firewall runs in the server OS with nftables or ufw, and network DDoS filtering in front of every server is a separate layer that you do not configure.
Networking and security guide, updated 2026-12-03
What does stateful mean?
An old style packet filter looks at each packet alone: source, destination, protocol, port. To let a web request out and the answer back in, you needed rules in both directions, and the inbound rule for replies was hard to write without opening too much.
A stateful firewall tracks connections in a table called conntrack. The first packet of a connection is checked against your rules. If it is accepted, the kernel records the flow, and every later packet in either direction is matched against that record as established. Related traffic, such as an ICMP error about that flow, is marked related. You can see the table live:
apt install -y conntrack
conntrack -L | head
conntrack -CThe practical result: you write rules only for new connections you want to accept, and one early rule lets all established and related traffic through. Packets that fit no known flow and are not a valid start are marked invalid and are safe to drop.
Why does the default policy matter most?
The default policy is what happens to a packet that no rule matched. There are two philosophies:
- Default deny for inbound: drop everything, then allow the few ports you serve. New software you install stays unreachable until you open it on purpose.
- Default allow for outbound: the server can start any connection it needs, such as package updates, DNS and API calls.
Default deny inbound is the right choice on almost every server. Default deny outbound is stricter and useful on very sensitive machines, but you must then allow DNS (UDP and TCP 53), NTP (UDP 123), HTTP and HTTPS for updates, and anything your apps call. Forgetting one shows up as strange timeouts days later.
Before switching any policy to drop over SSH, allow port 22 first and keep a second session open. If you lock yourself out, a reboot from the console clears rules that were not saved, which is your way back in.
Why does rule order change the result?
Rules in a chain are read top to bottom and the first rule with a final verdict (accept, drop, reject) wins. Put a broad drop above a narrow accept and the accept never runs. A sensible order for an input chain:
- Accept established and related.
- Drop invalid.
- Accept loopback (
lo). - Accept the ICMP and ICMPv6 types you need.
- Accept specific services: SSH, HTTP, HTTPS, WireGuard.
- Optional: log what is left.
- Fall through to the drop policy.
The established rule goes first for speed as well: most packets on a busy server belong to existing flows and are accepted after one check.
Reject versus drop: drop discards silently and the sender waits for a timeout; reject sends a refusal so the sender fails fast. Drop is the common choice for the public side, reject is friendlier inside a private network where quick errors help debugging.
How do you handle ICMP and IPv6 correctly?
Blocking all ICMP is a classic mistake. Ping (echo request) is optional, but other ICMP messages carry vital information. "Fragmentation needed" and its IPv6 cousin "packet too big" are how path MTU discovery works; drop them and large transfers hang while small ones work.
IPv6 depends on ICMPv6 even more. Neighbor discovery, which replaces ARP, and router advertisements run over ICMPv6. A firewall that drops it can make the server lose its IPv6 connectivity entirely. Accept ICMPv6 broadly on the input chain, or at least types 1~4 and 128~136.
Every server here has an IPv6 /64, and services that listen on [::] are reachable over it. A ruleset that covers only IPv4 leaves the IPv6 side open. nftables solves this with the inet family, which handles both in one table; ufw covers both when IPV6=yes is set in /etc/default/ufw, which is the default. More in IPv4 and IPv6 addresses.
What does a complete nftables ruleset look like?
On Debian 13 and Ubuntu 24.04, nftables reads /etc/nftables.conf when the nftables service is enabled. A full inbound ruleset:
#!/usr/sbin/nft -f
flush ruleset
table inet filter {
chain input {
type filter hook input priority filter; policy drop;
ct state established,related accept
ct state invalid drop
iif "lo" accept
meta l4proto ipv6-icmp accept
meta l4proto icmp icmp type { echo-request, destination-unreachable, time-exceeded } accept
tcp dport 22 ct state new limit rate 10/minute accept
tcp dport { 80, 443 } accept
udp dport 51820 accept
iifname "wg0" tcp dport { 5432, 6379 } accept
counter log prefix "nft-in-drop: " level info limit rate 5/minute
}
chain forward {
type filter hook forward priority filter; policy drop;
}
chain output {
type filter hook output priority filter; policy accept;
}
}Check the syntax, load it and make it survive reboots:
nft -c -f /etc/nftables.conf
nft -f /etc/nftables.conf
systemctl enable nftablesDo not run ufw and a hand-written nftables ruleset at the same time; pick one. Note that flush ruleset also removes rules other tools added, which matters with Docker (see below).
What is the same policy in ufw?
ufw writes rules for you and is the quicker choice on Ubuntu and Debian:
ufw default deny incoming
ufw default allow outgoing
ufw limit 22/tcp
ufw allow 80,443/tcp
ufw allow 51820/udp
ufw allow in on wg0 to any port 5432 proto tcp
ufw logging low
ufw enable
ufw status numberedufw limit allows SSH but blocks an address that opens six or more connections in 30 seconds, a cheap brake on password guessing. ufw already accepts established traffic, the needed ICMPv6 types and loopback through its built-in rules in /etc/ufw/before.rules and before6.rules. Delete a rule by its number from ufw status numbered with ufw delete 4.
On Rocky Linux and AlmaLinux the native tool is firewalld: firewall-cmd --permanent --add-service=https and firewall-cmd --reload. Our firewall setup guide has a short version for each family.
How do logging and counters help?
A firewall that you cannot observe is hard to trust. Counters tell you how many packets hit each rule:
nft list ruleset -a
nft list chain inet filter inputRules with the counter keyword show packet and byte totals. A service rule at zero after a day means nobody reaches it, which is either good or a sign that the rule never matches.
Log lines go to the kernel log. Read them with journalctl -k | grep nft-in-drop, or for ufw, journalctl -k | grep UFW (also written to /var/log/ufw.log when rsyslog is installed). Always rate limit logging: a scan can produce thousands of lines a second and fill the disk. Use logs while debugging, then lower them; constant scan noise on port 22 and 3389 is normal on any public address.
Why does Docker bypass ufw?
When you publish a container port with -p 8080:80, Docker adds its own NAT and filter rules so the traffic is forwarded to the container. Those packets go through the forward path, not the input chain where ufw rules live. The result surprises many people: ufw says port 8080 is closed, yet the container answers from the internet.
Three fixes, from simplest:
- Publish on loopback only and put a reverse proxy in front:
-p 127.0.0.1:8080:80, or"127.0.0.1:8080:80"in Compose. - Add your own filter rules to the
DOCKER-USERchain, which Docker evaluates before its own rules and never overwrites. - Do not publish database or admin ports at all; connect containers through a Docker network instead.
Also avoid flush ruleset in a reload script on a Docker host: it removes Docker's rules until the daemon restarts. More in Docker Compose setup.
How does the OS firewall relate to filtering at the network edge?
Some providers offer a firewall at the network edge that you configure in a panel. The console here has no such setting: the firewall you control is the one in the server OS, which you manage with the tools above. It is just as effective for deciding which ports accept connections, and it travels with the server image you build.
Separately, every server sits behind network DDoS filtering, which detects floods and removes attack traffic before it reaches the server. You do not configure it, and it does not replace your own rules: it deals with volume, your firewall deals with access.
Questions about this guide
Should I use nftables or ufw?
ufw for a quick, readable policy on Ubuntu or Debian. nftables directly when you need sets, rate limits per rule, or one file you keep in Git. On recent systems ufw itself runs on top of the nftables kernel framework.
Is there a firewall in the CheapVPS console?
No. You manage the firewall in the OS with ufw, nftables or firewalld. Network DDoS filtering is applied in front of every server and needs no setup.
Why did my rules disappear after a reboot?
Rules entered with nft add or iptables live in memory only. Save them to /etc/nftables.conf and enable the nftables service, or use ufw, which saves automatically.
Should I block ping?
You can drop ICMP echo requests if you prefer, but keep the other ICMP types, and never drop ICMPv6. Ping is very useful when diagnosing problems with mtr.
Do I need outbound rules?
For most servers no: allow outgoing by default. Restrict outbound only on machines with a strict security need, and remember DNS, NTP and package mirrors. Outgoing mail on port 25 needs a request to support first anyway.
How do I allow SSH only from my office?
Use ufw allow from 198.51.100.0/24 to any port 22 proto tcp and remove the general SSH rule. Keep a second way in, such as a WireGuard peer, in case your office address changes.
More in Networking and security
- IPv4 and IPv6 addresses
- Add IPv6 addresses
- Static network setup
- Reverse DNS (PTR)
- Networking terms
- How the VPS network works
- How DNS works
- Private networking
- Private link with WireGuard
- Failover and floating IPs
- Load balancing explained
- HAProxy load balancer
- TLS certificates explained
- SSH hardening
- Firewall setup
- Point a domain