Networking

What Is a Firewall? How It Works, Types, and How to Check Yours

A firewall is software or a device that checks network traffic against a set of rules and allows or blocks each connection, usually by IP address, port, protocol or program. The typical default, used by Windows Firewall and Ubuntu's ufw, is to block unsolicited incoming connections and allow outgoing ones, so your computer can reach the internet while strangers can't reach services on it.

NIST's firewall guideline (SP 800-41 Rev. 1) puts it in one line: firewalls "control the flow of network traffic between networks or hosts". Everything else is detail about which traffic, and where the firewall sits.

How a firewall decides: rules and default policy

A firewall holds a list of rules. Each rule names conditions and an action. Microsoft lists the conditions a Windows Firewall rule can use: the program or service, source and destination IP addresses, protocol and port numbers for TCP and UDP, interface type, and ICMP type. A rule might say "allow inbound TCP port 22 from the local subnet" or "block outbound traffic from app.exe".

Traffic that matches no rule gets the default policy. Two published defaults:

FirewallIncoming defaultOutgoing defaultOn out of the box?
Windows FirewallBlock, unless solicited or matching a ruleAllow, unless matching a ruleYes, on all editions
ufw (Ubuntu)Deny (forwarded traffic also deny)AllowNo, disabled after installation
macOS firewallPer app: allow or block incoming connectionsNot filtered by this firewallA switch in System Settings

"Unless solicited" is the important part. When you open a website, your computer starts the connection, so the replies coming back are expected and let in. An incoming connection that nobody on your machine asked for is dropped unless a rule allows it.

Stateful vs stateless firewalls

A stateless (packet-filter) firewall judges each packet on its own: addresses, protocol, ports. To let web replies in, it would need a rule allowing traffic from port 443 anywhere, which also lets in anything that pretends to come from port 443.

A stateful firewall tracks connections. Linux's kernel firewall, netfilter, which nftables configures, labels each packet with a connection state. The nftables documentation defines them:

  • new: packets seen in one direction only so far, such as a TCP SYN;
  • established: valid packets have travelled in both directions;
  • related: a new connection expected because of an existing one, such as an FTP data channel;
  • invalid: packets that don't follow the expected behaviour of a connection.

The classic stateful rule set for a desktop is a default-drop input chain plus one rule:

table inet filter {
  chain input {
    type filter hook input priority 0; policy drop;
    ct state established,related accept
  }
}

Replies to connections you started are established and get in. Everything else arriving from outside is dropped. A real configuration also accepts traffic on the loopback interface and whatever services you deliberately run. Windows Firewall works on the same principle; Microsoft describes it as dropping traffic that isn't allowed unsolicited traffic or a response to a request the device made.

Host firewalls vs network firewalls

Host-based firewallNetwork firewall
Runs onThe computer itself (Windows Firewall, macOS firewall, ufw/nftables)A separate appliance, or a function of a router or cloud network
ProtectsThat one machineEvery device behind it
Can filter by programYes: it knows which app opened the socketNo: it only sees packets
Follows a laptop to a caféYesNo

The two are complementary. A network firewall keeps a whole office or home network's inbound traffic in check; a host firewall still protects a laptop on hotel Wi-Fi and stops other devices on the same network from reaching it. Windows uses network profiles for exactly this: the public profile, the default for unidentified networks, is designed for Wi-Fi hotspots, coffee shops and airports, while private is for a home network you trust.

How to check your firewall on Windows

From PowerShell (no administrator rights needed to read):

Get-NetFirewallProfile | Select-Object Name, Enabled, DefaultInboundAction, DefaultOutboundAction

Example output on Windows 11:

Name    Enabled DefaultInboundAction DefaultOutboundAction
----    ------- -------------------- ---------------------
Domain     True        NotConfigured         NotConfigured
Private    True        NotConfigured         NotConfigured
Public     True        NotConfigured         NotConfigured

Enabled True on all three profiles is what you want. NotConfigured means nobody has overridden the action, so the built-in default applies: block unsolicited inbound, allow outbound. The Command Prompt equivalent is netsh advfirewall show allprofiles state, which prints State ON for each profile. To see which profile your current network uses, run Get-NetConnectionProfile and read NetworkCategory.

The rules themselves are in Windows Security > Firewall & network protection > Advanced settings, or from PowerShell with Get-NetFirewallRule. A Windows 11 machine with ordinary software installed had 1,101 rules in that list, most of them predefined and many disabled, such as the Remote Desktop - User Mode (TCP-In) rule that stays off until Remote Desktop is enabled.

Opening a port, from an elevated PowerShell (shown, not something to run unless you need it):

New-NetFirewallRule -DisplayName "Dev server 8080" -Direction Inbound -Protocol TCP -LocalPort 8080 -Action Allow -Profile Private

Restricting it to the Private profile means the port stays closed on public Wi-Fi.

How to check your firewall on macOS

Apple's path is Apple menu > System Settings > Network > Firewall. Turn it on there, then use Options to add apps and set each one to allow or block incoming connections. When an app that isn't in the list receives a connection attempt, macOS asks whether to allow it, and Apple notes that attempts are denied until you answer. Apple also notes that some system apps and services can have firewall access without appearing in the list.

How to check your firewall on Linux (ufw)

sudo ufw status verbose

If it prints Status: inactive, nothing is being filtered by ufw: the man page says ufw is disabled after installation. A minimal setup for a machine you reach over SSH:

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
sudo ufw enable

Add the SSH rule before ufw enable when you are connected remotely, or you lock yourself out. ufw asks for confirmation when enabled over SSH for exactly this reason.

What a firewall doesn't do

  • It doesn't fix the service behind an open port. If you allow port 22 or 3389 from the internet, the firewall lets attackers reach that service; its passwords and patches are what protect you. Port forwarding explained covers the router side of exposing a service.
  • It doesn't inspect what you download. A file you fetch over a connection you started is allowed in. Phishing and malware are a separate problem (how to spot phishing).
  • It doesn't encrypt anything. That is TLS's or a VPN's job.

If a firewall is blocking something you need

First confirm the program is actually listening on the port you think (netstat -ano | findstr :8080 on Windows, ss -ltn on Linux). Then check which network profile you're on: a rule for Private does nothing while Windows thinks you're on a Public network. Allow the specific program or port rather than switching the firewall off; Microsoft explicitly recommends against disabling Windows Firewall, and against stopping its service (MpsSvc) in particular, which it says can break the Start menu and app installs. What a port number is explains the port side of the rule you're writing.

Frequently asked questions

Do I need a firewall on my home computer?
Keep the one your operating system includes switched on. Windows Firewall is enabled by default on all Windows editions, and Microsoft recommends not disabling it. On a Mac the firewall is a setting in System Settings > Network > Firewall, and on Ubuntu ufw is installed but disabled until you run sudo ufw enable.
Does a firewall protect against viruses?
Only partly. A firewall decides which network connections are allowed; it does not inspect files you choose to download or open. It can stop an attacker from reaching a vulnerable service on your machine, but malware you run yourself arrives through a connection you started, which a typical firewall allows. Antivirus and keeping software updated cover that side.
Should I block outgoing traffic too?
On a personal computer, usually not. Windows Firewall and ufw both allow outgoing traffic by default and block unsolicited incoming traffic, which stops the most common attacks without breaking apps. Outbound rules are useful for specific cases, such as stopping one program from reaching the internet.
Is a VPN a firewall?
No. A VPN encrypts traffic between your device and a VPN server and changes the address websites see. It does not decide which incoming connections your device accepts. Many firewall products include VPN features, but the two do different jobs, and you can use both.

Sources