Firewalls Explained
Understand what a firewall allows and blocks, how packet, stateful and application filtering differ, and how to set sensible rules.
Key takeaways
- A firewall applies rules to traffic, and a good default is to block unsolicited inbound connections and allow only what you need.
- Stateful firewalls remember connections you started, so replies come back in without a rule for each one.
- Home routers with NAT block most unsolicited traffic, but NAT isn't a security design, and IPv6 needs real firewall rules.
- Host firewalls protect a device wherever it goes, including on networks you don't control.
- A firewall can't judge intent inside allowed, encrypted traffic, so it complements updates, strong sign-in and care.
A firewall is a filter that decides which network traffic is allowed to pass, based on rules. At home, it mostly stops strangers on the internet from starting connections to your devices while letting your own connections work normally. On a computer or server, it controls which services can be reached and by whom.
You almost certainly already have at least one, probably two: one in your router and one in your operating system. This article explains how they make decisions, what they can't do and how to configure them without breaking things.
What a firewall looks at
Network traffic is broken into packets. Each carries addressing information that a firewall can read: the source and destination IP addresses, the protocol (such as TCP, UDP or ICMP), and for TCP and UDP the source and destination port numbers. A port identifies which service on a machine the traffic is meant for. Ports explained covers that idea.
A rule combines conditions with an action. For example: allow TCP to port 443 on the web server from anywhere; allow TCP to port 22 only from the office network; drop everything else. Rules are usually evaluated in order, and the first match wins, though some systems resolve conflicts differently, so read your product's documentation.
"Drop" silently discards the packet. "Reject" sends back a refusal, which makes failures faster to diagnose.
Packet filtering, stateful inspection and beyond
Stateless packet filters judge each packet alone. They're fast and simple, but to let replies back in you have to write rules that are broader than you'd like.
Stateful firewalls keep a table of active connections. When your laptop opens a connection to a website, the firewall notes it, and returning packets that match are let through automatically. Unsolicited packets that don't belong to any known connection fall under the inbound rules. Most firewalls you'll meet work this way.
Application-aware firewalls, often marketed as next-generation firewalls, try to identify the application or user behind the traffic, apply web category filtering, and may include intrusion prevention. Intrusion detection and prevention systems covers that function.
Web application firewalls sit in front of websites and examine HTTP requests for attack patterns. They're a different tool for a different job; see web application firewalls.
Where firewalls sit
| Location | Protects | Notes |
|---|---|---|
| Home router | The whole home network from the internet | Defaults usually block unsolicited inbound |
| Host firewall | One computer or server | Travels with the device |
| Business edge appliance | An office or site | Often adds VPN and filtering |
| Cloud security group | Virtual machines and services | Managed in the provider console |
| Internal segment firewall | One part of a network from another | Limits spread after a breach |
Your router and NAT
Most home IPv4 networks use network address translation, which lets many devices share one public address. Because the router only forwards inbound traffic that matches a connection or a forwarding rule, NAT has the side effect of blocking unsolicited connections. See NAT explained.
Don't rely on that side effect. With IPv6, devices can have globally reachable addresses, and protection comes from the router's firewall. Check that IPv6 firewalling is on. Also look at port forwarding rules and UPnP, which can create openings you didn't intend. Port forwarding explains the risks.
Host firewalls on your computer
Menu names vary by version.
Windows has Windows Defender Firewall with separate profiles for domain, private and public networks. Mark café and hotel networks as public, which applies stricter rules. You can check the profiles in PowerShell:
Get-NetFirewallProfile | Format-Table Name, Enabled macOS has an application firewall in the network settings that controls incoming connections to apps.
Linux uses the kernel's packet filtering, managed with nftables directly or through front ends such as ufw and firewalld. A cautious ufw setup on a server you administer remotely looks like this:
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow from 198.51.100.0/24 to any port 22 proto tcp
sudo ufw enable
sudo ufw status verbose The addresses are documentation examples; use your real trusted range. Add the SSH rule before enabling, or you may lock yourself out. Keep a console session or another way in while you test.
Cloud security groups
If you run servers with a cloud provider, you'll meet security groups or network firewall rules in the console. They behave like firewalls around each machine or interface. Many providers' security groups are stateful and contain only allow rules, with everything else denied, while some also offer separate subnet-level lists that are stateless. Read how your provider defines them before relying on either.
The common mistake is opening administration ports to the whole internet, written as the source 0.0.0.0/0 for IPv4 or ::/0 for IPv6, during setup and then forgetting. Open to the world only what the public needs, typically web ports. Reach administration through a VPN, a managed access service or a narrow source range. Remember that a host firewall inside the machine still applies, so a connection has to pass both. Cloud networking basics explains how these pieces fit.
Inbound and outbound
Inbound filtering gets most attention because it blocks outsiders from reaching your services. Outbound filtering restricts what your own devices may contact. It's valuable on servers, which usually need only a few destinations: if malware lands, it may be unable to reach its operator or send data out. On home computers, strict outbound rules produce many prompts and fatigue, so most people leave outbound traffic allowed.
Writing rules that stay sane
- Start from deny. Allow what you can name a reason for.
- Be specific. Limit sources and ports. "Any to any" rules tend to outlive their purpose.
- Document each rule with an owner and a reason. Date temporary ones.
- Order carefully, putting specific rules before broad ones where order matters.
- Don't expose management interfaces to the internet. Reach them through a VPN or similar.
- Review periodically and remove rules nothing uses. Logs show which ones match.
- Keep the firewall itself patched. Edge devices are frequent targets, as why VPN appliances get attacked explains.
For calculating address ranges in rules, the CIDR calculator is handy.
What a firewall can't do
A firewall allows or blocks based on what it can observe. If you permit web traffic, a malicious download arriving over HTTPS looks like any other web traffic unless the firewall can inspect inside it, which raises privacy and complexity issues. It can't stop you from entering a password on a fake page, and it doesn't help once an attacker is already inside and moving through allowed paths. That last gap is why organizations add internal segmentation and identity checks, ideas covered in zero trust networking.
Reading firewall logs
Logs tell you what a firewall is actually doing. A typical entry records the time, the action, the interface, source and destination addresses, protocol and ports. On an internet-facing connection you'll see a constant trickle of blocked attempts from automated scanners. That's normal background noise, and seeing it blocked means the rule is working.
Pay more attention to entries that originate inside your network. A printer trying to reach unfamiliar servers, or one workstation suddenly attempting connections to many others, deserves a look. Logging every allowed packet fills storage quickly, so most people log drops and selected allows. If your router can send logs to another machine, they survive a reboot and are harder for an intruder to erase.
Troubleshooting a blocked connection
When something stops working, resist turning the firewall off and leaving it off. Instead:
- Confirm the service is running and listening on the expected port.
- Check whether the failure is a timeout (often a drop) or an immediate refusal (often a reject or nothing listening).
- Test from the same network and from another to locate the block; testing a port from the command line shows how, on systems you're allowed to test.
- Read the firewall log for dropped packets matching your attempt.
- Add the narrowest rule that fixes it, then verify.
- Check every layer: host firewall, router, cloud security group and any provider filtering.
Common mistakes
- Leaving a test rule in place. A broad rule added to diagnose a problem becomes permanent.
- Forgetting IPv6. Carefully filtered IPv4 with an open IPv6 side leaves the same service reachable.
- Trusting the port number. Moving a service to an unusual port reduces log noise but doesn't protect it.
- Allowing whole programs on every network. A permission granted at home may still apply on airport Wi-Fi if the profile is wrong.
Common questions
Do I need to buy a separate firewall for home?
Usually not. A maintained router and the built-in host firewalls serve most households. Enthusiasts add dedicated appliances for segmentation, logging or learning.
Does a VPN replace a firewall?
No. A VPN encrypts traffic between two points. It doesn't decide which services on your device may be reached.