What Is a Firewall and How Does It Work?

Internet Security6 min read
Illustration of a brick wall blocking arrows between the internet and a protected network

The word firewall comes from construction, where a fire-resistant wall stops flames from spreading from one part of a building to another. In networking, a firewall plays a similar role: it sits between two networks, such as your home and the internet, and decides which traffic may pass. Firewalls are among the oldest and most important security tools, and you almost certainly already use several without realizing it.

What a firewall does

A firewall inspects network traffic and compares it with a set of rules. Each rule says, in effect, "if the traffic matches these properties, allow it, block it or log it". Properties can include the source and destination IP address, the port number, the protocol (such as TCP or UDP), the direction of the traffic and, in more advanced products, the application or even the content. The goal is to reduce exposure: only the traffic that is needed gets through, and everything else is stopped before it can reach a vulnerable service.

How firewalls make decisions

Packet filtering

The simplest approach examines each packet in isolation. A rule might say "block all incoming traffic to TCP port 23" (Telnet). Packet filters are fast, but they cannot tell whether a packet is part of a legitimate conversation.

Stateful inspection

Modern firewalls remember connections. When a device inside the network starts a connection, the firewall records it and automatically allows the matching replies. Unsolicited incoming packets that match no known connection are dropped. This is how home routers allow your browsing while blocking outside attempts to connect to your devices.

Application-layer and next-generation firewalls

Proxy firewalls and next-generation firewalls (NGFWs) understand application protocols. They can recognize that traffic on port 443 is a particular web application, block specific features, decrypt and inspect traffic with a configured certificate, and use intrusion-prevention signatures. They are common in companies. A related product, the web application firewall (WAF), sits in front of a website and filters malicious HTTP requests such as injection attempts.

Network firewall vs host firewall

A network firewall protects a whole network and usually runs on a router or dedicated appliance. A host firewall runs on one device: Windows Defender Firewall, the macOS application firewall, and tools such as ufw or nftables on Linux are examples. The two work well together. The network firewall blocks most unwanted traffic at the edge, and host firewalls protect each device from threats that originate inside the network, for example a compromised smart television or a laptop on public Wi-Fi.

Default deny: the principle behind good rules

A well-designed policy starts from "deny everything" and then allows only what is needed. For a typical home router, that means blocking all unsolicited inbound connections and allowing outbound traffic. For a small web server, it might mean allowing inbound ports 80 and 443 for everyone, port 22 (SSH) only from your own address, and nothing else. Every open port is something that must be kept updated, so fewer open ports means a smaller attack surface.

Firewalls and NAT

Home routers combine a stateful firewall with Network Address Translation. NAT on its own is not a security feature, but it has a similar effect because connections from the internet have no matching entry in the translation table. With IPv6, NAT is usually absent and the stateful firewall does all of the work, which is why it should be left enabled. Learn more in IPv4 vs IPv6.

What a firewall cannot do

  • Stop threats you invite in. If you download malware or log in to a phishing site, the traffic looks like normal outbound connections.
  • Fix weak passwords or unpatched services that you deliberately expose through open ports.
  • Inspect encrypted content unless the firewall is set up to decrypt it.
  • Replace other security layers such as updates, backups, two-factor authentication and common sense.

The topic of IP address exposure is covered in Can Someone Hack You Using Your IP Address?

Practical tips for home users

  1. Leave the router's built-in firewall enabled and avoid placing devices in the "DMZ" setting.
  2. Keep the host firewall on for every computer and choose the "public" network profile on shared Wi-Fi.
  3. Review port forwarding and UPnP rules regularly and remove what you do not need.
  4. Update router firmware, as firewall bugs are fixed through updates.
  5. If you run a server, restrict administration ports to known addresses and use strong authentication.

A broader plan is outlined in How to Protect Your Home Network from Cyber Threats.

Conclusion

A firewall is a rule-based gatekeeper that allows needed traffic and blocks the rest. Stateful inspection, host and network placement, and a default-deny policy make it a strong first layer of defense. It is not a complete solution, but together with updates, strong credentials and cautious behavior, it prevents most automated attacks from ever reaching your devices.

Frequently asked questions

Is a software firewall enough at home?

The router's firewall plus the firewall built into your operating system gives good protection for most households. Keep both enabled.

Can a firewall slow down my connection?

Basic firewalls have no noticeable impact. Deep inspection on heavily loaded business appliances can add latency if undersized.