Firewalls Explained: Types, Rule Order and Real Configurations
What is a firewall?
A firewall is a security control that allows or blocks network traffic according to a set of rules. Rules match on details such as source and destination address, port, protocol, connection state or application. Firewalls run on dedicated appliances at network boundaries, in cloud platforms and on individual hosts, and a well-built rule set denies everything that is not explicitly needed.
Key terms and where firewalls sit
- Rule: a match condition plus an action such as allow, deny (drop silently) or reject (refuse with a reply).
- Rule set or policy: the ordered list of rules on one firewall.
- Inbound and outbound: traffic arriving at or leaving the protected network or host.
- Zone: a group of interfaces with the same trust level, such as inside, outside and DMZ.
- DMZ: a separate network for public-facing servers, so a compromised web server does not sit on the internal LAN.
A network firewall sits between zones and filters traffic passing through it. A host firewall runs on one server or laptop and filters traffic to and from that machine. Using both is normal practice, because a network firewall cannot see traffic between two hosts on the same subnet. The wider design is covered in network security fundamentals.
Types of firewall: packet filter to NGFW and WAF
| Type | What it inspects | Typical use |
|---|---|---|
| Stateless packet filter | Each packet header alone: addresses, ports, protocol | Router access control lists, simple edge filtering |
| Stateful firewall | Headers plus a connection table that tracks sessions | Most host and network firewalls |
| Next-generation firewall (NGFW) | Application identity, user identity, intrusion signatures, optionally decrypted TLS | Enterprise perimeters and internal segmentation |
| Web application firewall (WAF) | HTTP requests and responses | Protecting websites and APIs from attacks such as SQL injection |
A stateless filter must allow return traffic with a separate rule, often by opening a wide range of high ports. A stateful firewall records each permitted outbound connection in its state table and automatically allows the matching replies, so inbound rules can stay tight. An NGFW adds the ability to write rules such as "allow this user group to use this application" rather than only "allow TCP 443".
A WAF works at the application layer and understands HTTP. It does not replace a network firewall or secure code; it adds an extra layer against the attack classes listed in the OWASP Top 10. The OWASP Core Rule Set is a widely used open rule set for WAFs.
Rule order and default deny
In router ACLs, iptables chains, ufw and most firewall appliances, rules are checked from top to bottom and the first match decides. A broad allow rule placed above a specific deny rule makes that deny rule useless. Put specific rules before general ones.
Default deny means traffic that matches no rule is blocked. Many devices end every list with an implicit deny; on Linux you set the chain policy to drop. You then add allow rules only for traffic the business needs, which is the principle of least privilege applied to the network.
Host firewall on Ubuntu with ufw
ufw (Uncomplicated Firewall) is a front end that writes the underlying netfilter rules for you. On a remote server, allow SSH before enabling it, or you will lock yourself out.
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 192.168.1.0/24 to any port 22 proto tcp # SSH from the LAN only
sudo ufw allow 443/tcp # HTTPS from anywhere
sudo ufw enable
sudo ufw status numbered # list rules with numbers
sudo ufw delete 2 # remove rule number 2
sudo ufw insert 1 deny from 203.0.113.50 # add a rule at the top iptables and nftables on Linux
nftables is the current Linux packet-filtering framework, and on many current distributions the iptables command is a compatibility layer that writes nftables rules. You will still meet iptables syntax in documentation and older servers.
# iptables: stateful, default-deny input
sudo iptables -A INPUT -i lo -j ACCEPT
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables -A INPUT -p tcp -s 192.168.1.0/24 --dport 22 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo iptables -P INPUT DROP
sudo iptables -L -n -v --line-numbers#!/usr/sbin/nft -f
# /etc/nftables.conf: the same policy in nftables
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
ip protocol icmp accept
meta l4proto ipv6-icmp accept
ip saddr 192.168.1.0/24 tcp dport 22 accept
tcp dport { 80, 443 } accept
}
}Load the file with sudo nft -f /etc/nftables.conf and check it with sudo nft list ruleset. The inet family covers IPv4 and IPv6 together. ICMPv6 is allowed because IPv6 neighbour discovery depends on it.
Windows Defender Firewall
Windows applies separate settings for the Domain, Private and Public network profiles. The defaults block unsolicited inbound traffic and allow outbound traffic.
# PowerShell, run as administrator
Get-NetFirewallProfile | Select-Object Name, Enabled, DefaultInboundAction
New-NetFirewallRule -DisplayName "SSH from LAN" -Direction Inbound `
-Protocol TCP -LocalPort 22 -RemoteAddress 192.168.1.0/24 -Action Allow
Get-NetFirewallRule -DisplayName "SSH from LAN" | Get-NetFirewallPortFilter
Remove-NetFirewallRule -DisplayName "SSH from LAN"Writing a rule set, step by step
- List the services the host or network must offer and who needs each one.
- Set the default inbound policy to deny.
- Add specific allow rules with the narrowest source addresses and ports that work.
- Order rules from specific to general and remove duplicates.
- Enable logging for denied traffic, then test from an allowed and a disallowed source.
- Record the reason and owner for every rule so it can be reviewed later.
Practical exercise: harden a test server
Use a Linux virtual machine you own with SSH and a web server installed.
- Run
ss -tulpnand list every listening port. - Write a ufw or nftables policy that allows SSH only from your LAN and HTTPS from anywhere, and drops everything else.
- From a second machine, confirm with
nc -zvorTest-NetConnectionthat allowed ports open and others do not. - Add a rule in the wrong position on purpose, observe the effect, then fix the order.
You are done when your test results match the written policy exactly. Firewall design and segmentation are explored further in the Advanced Networking course; check with common network commands if a test result looks wrong.
Common mistakes
- Enabling a default-deny policy on a remote server before allowing SSH.
- Placing a broad allow rule above a specific deny rule in a first-match rule set.
- Opening a port to
0.0.0.0/0when only one office or subnet needs it. - Treating a WAF as a reason to skip input validation in the application.
- Adding rules without recording why, so nobody dares remove them later.
Frequently asked questions
What is the difference between drop and reject?
Drop discards the packet silently, so the sender waits for a timeout. Reject sends back a TCP reset or ICMP error, so the sender fails quickly. Drop gives less information to scanners; reject makes internal troubleshooting faster.
Does NAT on my router act as a firewall?
NAT hides private addresses but is not a filtering policy. Home routers usually combine NAT with a stateful firewall, and that firewall is what blocks unsolicited inbound traffic.
Should I filter outbound traffic too?
On servers, yes where practical. Limiting outbound connections to known destinations makes it harder for malware to reach its control servers or send data out, a point covered in the malware guide.