Firewalls & ACLs
Write stateful firewall rules and ACLs that follow default-deny and least privilege.
By Invicly Tech, , in Computer Networking. About 12 minutes to read.
In short
Firewalls decide which traffic may pass based on rules about addresses, ports, protocols and connection state. Stateless ACLs check each packet in isolation, while stateful firewalls track connections and automatically allow replies. Good rule sets deny by default and allow only what is needed between clearly defined zones.
Explained simply
A firewall is a guard at the door of a network or a computer. Every packet that wants to pass is checked against a list of rules: where it comes from, where it goes, which protocol and which port. The rules say allow or drop.
The safest way to write rules is default deny. You block everything, then open only what is really needed, such as web traffic to the web server. Anything you forgot stays closed. The opposite, allowing everything and blocking a few bad things, always leaves holes.
Simple firewalls, often called ACLs (access control lists), look at each packet alone. Smarter stateful firewalls remember connections. When your laptop opens a connection to a website, the firewall remembers it and lets the replies back in automatically, so you need far fewer rules.
Firewalls also split a network into zones. Public servers sit in a DMZ (demilitarized zone), staff in an internal zone, guests in their own. Rules between zones decide who can talk to whom, so one infected guest laptop cannot reach your file server.
Press Play on a diagram to walk through it one step at a time.
- 1Packet arrives
On an interface or zone.
- 2Existing connection?
Established or related: allow.
- 3Match rules top-down
First match wins.
- 4Allow or drop
Log as configured.
- 5Default policy
No match: drop.
- 1ct state established,related accept
Replies and known flows, most packets
- 2ct state invalid drop
Broken or out-of-state packets
- 3iif lo accept
Local programs talking to each other
- 4ICMP and ICMPv6 accept
Ping, errors, IPv6 neighbour discovery
- 5Service ports from allowed sources
SSH from admin subnet, 443 from anyone
- 6policy drop
Everything else, optionally logged
In depth
Stateless ACLs versus stateful firewalls
A stateless ACL judges each packet only by its headers. To let a web server answer, you need one rule for requests to port 443 and another for replies from port 443 back to the client's random high port (the ephemeral range, often 1024 to 65535 or 32768 to 60999 on Linux). That second rule is wide and easy to abuse.
A stateful firewall keeps a connection table (on Linux, the conntrack table). The first packet of a flow is checked against the rules; if allowed, an entry is created, and later packets of that flow, plus replies, match the entry and pass. Linux names the states new, established, related (for example an ICMP error or an FTP data connection tied to a known flow) and invalid. Stateful filtering is safer and simpler, but the table uses memory and can fill up under a flood, so busy firewalls tune its size and timeouts.
- Rule 1: in, dst port 443 allow
- Rule 2: out, src port 443 to 1024-65535 allow
- Each packet judged alone
- Fast, no memory per flow
- Rule: new to port 443 allow
- Replies match the connection table
- Tracks new, established, related, invalid
- Table can fill during floods
Rule order and default deny
Most firewalls read rules from top to bottom and act on the first match. So specific rules go above general ones: a rule that drops one bad subnet must sit above the rule that allows the whole internet to port 443. Cisco ACLs also end with an invisible implicit deny any, so an ACL with only permit rules still blocks everything else.
A good default-deny rule set for a server has a fixed shape: allow established and related traffic first (cheap, and it covers most packets), drop invalid packets, allow the loopback interface, allow needed ICMP, allow each service port from only the sources that need it, then let the default policy drop the rest. Log the final drops at a limited rate so you can see what is being blocked without flooding the log. Outbound rules matter too: a database server rarely needs to start connections to the internet.
- Specific rules above general rules
- Cisco ACLs end with an implicit deny
- Allow established first for speed
- Allow ICMP; IPv6 breaks without ICMPv6
- Limit outbound traffic too, not only inbound
nftables and ufw in practice
On modern Linux, nftables is the packet filter; the iptables command on many distributions is now a front end to it. Rules live in tables (family inet covers IPv4 and IPv6 together) and chains attached to hooks such as input, forward and output. A chain's policy is what happens when no rule matches. Sets like { 22, 80, 443 } keep rules short and fast.
Keep the full rule set in /etc/nftables.conf, check it with nft -c -f /etc/nftables.conf, and load it with nft -f. nft list ruleset shows what is active. ufw (Uncomplicated Firewall, default on Ubuntu) is a simpler wrapper: ufw default deny incoming, ufw allow 22/tcp, ufw allow from 10.0.0.0/24 to any port 5432 proto tcp, ufw enable. On a remote server, always allow SSH before setting a drop policy, and keep a second session open while testing.
- nft -c -f file checks syntax without applying
- nft list ruleset shows active rules
- ufw status numbered shows rules with numbers
- ufw delete 3 removes rule number 3
DMZs and segmentation
Segmentation means splitting a network into zones and only allowing the traffic each zone needs. A classic design has three zones on one firewall: outside (the internet), DMZ (servers the public must reach, such as a web server or mail relay) and inside (staff and internal servers). The rules: outside may reach the DMZ only on its service ports; the DMZ may reach only specific inside services, such as the database on 5432; outside may never reach inside directly; inside may reach the internet.
If an attacker takes over the web server, they are stuck in the DMZ and can only reach what the rules allow. Inside larger networks, the same idea separates staff, guests, servers, management and IoT devices, often one VLAN and subnet per zone with the firewall or router ACLs between them. Microsegmentation pushes it further, with rules per workload, as cloud security groups do.
- 1Internet
Untrusted, default deny
- 2To DMZ only
TCP 80 and 443 to the web server
- 3DMZ web server
Compromise is contained here
- 4To inside, one port
Web server to database on TCP 5432 only
- 5Inside network
Never reachable from the internet directly
Learn it in 4 short steps
About 45m each. Track them with a timer in your plan.
- 1Compare stateless ACLs and stateful firewalls~45m
- 2Write default-deny rule sets~45m
- 3Configure nftables or ufw rules~45m
- 4Design DMZ and segmentation zones~45m
Key points
- Order matters: the first matching rule wins.
- Stateful firewalls allow return traffic without extra rules.
- A DMZ holds public-facing servers separate from the internal network.
- Cloud security groups are stateful; network ACLs are usually stateless.
Key terms
Tap a card to flip it. 0/4 known
Examples
sudo nft add table inet filter
sudo nft add chain inet filter input '{ type filter hook input priority 0; policy drop; }'
sudo nft add rule inet filter input ct state established,related accept
sudo nft add rule inet filter input iif lo accept
sudo nft add rule inet filter input tcp dport '{ 22, 443 }' accept
sudo nft list rulesetA complete nftables file for a web server
The server must serve HTTP and HTTPS to everyone, accept SSH only from the admin subnet 10.0.99.0/24, and drop everything else. Step 1: write the file below. Step 2: check it with sudo nft -c -f /etc/nftables.conf. Step 3: load it with sudo nft -f /etc/nftables.conf while a second SSH session stays open. Step 4: enable the nftables service so it loads at boot.
#!/usr/sbin/nft -f
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
ip saddr 10.0.99.0/24 tcp dport 22 accept
tcp dport { 80, 443 } accept
limit rate 5/minute log prefix "nft-drop: "
}
chain forward { type filter hook forward priority 0; policy drop; }
}Blocking guests from internal subnets with a Cisco ACL
Guests on VLAN 20 (10.20.20.0/24) may use the internet but must not reach any other 10.20.x.x subnet. Step 1: write an extended named ACL: permit DHCP first (renewals go to the router's own internal address), deny guest traffic to the internal range, then permit guests to anything else. Step 2: apply it inbound on the guest subinterface, close to the source, as extended ACLs should be. Step 3: test from a guest: pinging the file server fails, browsing works. Without the final permit, the implicit deny would block the internet too.
ip access-list extended GUEST-IN
permit udp any eq bootpc any eq bootps
deny ip 10.20.20.0 0.0.0.255 10.20.0.0 0.0.255.255
permit ip 10.20.20.0 0.0.0.255 any
!
interface g0/0.20
ip access-group GUEST-IN in
!
show access-lists GUEST-INQuick reference
- First matching rule wins: put narrow rules above broad ones
- Cisco ACLs end with an implicit deny any; standard 1-99, extended 100-199
- Place extended ACLs near the source, standard ACLs near the destination
- nft list ruleset shows active rules; nft -c -f file checks a file first
- ufw default deny incoming && ufw allow 22/tcp && ufw enable
- Allow SSH before a drop policy on a remote box, and keep a second session open
- Do not block all ICMPv6: IPv6 neighbour discovery needs it
- conntrack -L lists tracked connections (from the conntrack-tools package)
Common mistakes
- Applying a default-drop policy remotely before allowing SSH.
- Placing a broad allow rule above specific deny rules.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
On an Ubuntu VM, use ufw to deny all incoming traffic, allow SSH and HTTPS, and show the result.
- Exercise 2medium
Add an nftables rule that allows PostgreSQL (5432) only from the app subnet 10.0.10.0/24, and prove that other hosts are blocked.
- Exercise 3hard
Write a zone matrix for an office with zones Internet, DMZ, Staff, Guests and Servers. For each pair, write allow (with ports) or deny, following least privilege.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between iptables and nftables?
- nftables is the newer Linux packet filtering framework that replaces iptables, with one tool (nft), IPv4 and IPv6 in one table, and sets for fast matching. Many distributions now run iptables commands on top of nftables via iptables-nft.
- Do I need a firewall if my server only runs one service?
- Yes. Services you forgot about, such as a debug port or a database bound to 0.0.0.0, are common. A default-deny firewall protects you from your own mistakes.
- Should I block ping?
- Blocking ICMP echo hides little and makes troubleshooting harder. Never block all ICMP, because error messages are needed for path MTU discovery, and IPv6 cannot work without ICMPv6.
- What is a next-generation firewall?
- A firewall that also identifies applications and users, inspects content, and often includes intrusion prevention, instead of judging only by IP addresses and ports.