What is a Firewall? The Visual Guide

Learn how firewalls protect your network, the difference between hardware and software firewalls, and how to configure UFW on Linux.

Network diagram showing a firewall between a computer and the internet
📧

Get weekly IT guides

Join 5,000+ IT professionals

Subscribe Free

Imagine your computer is a house with 65,535 doors (ports). Without a firewall, every single one of them is unlocked and wide open to the internet.

Introduction

A Firewall is a network security device (hardware or software) that monitors incoming and outgoing network traffic. It decides whether to allow or block specific traffic based on a defined set of security rules.

What You'll Learn
  • How firewalls filter traffic (Stateless vs Stateful vs App-Layer)
  • The difference between iptables and UFW
  • Setting up UFW rules on an Ubuntu server
  • Reading and writing iptables rules manually
  • Firewall debugging: Why your service is still blocked
Mental Model: The Nightclub Bouncer

Think of a Firewall as a Bouncer at a club:

  1. The List (Rules): The bouncer has a list of who is allowed in (VIPs) and who is banned.
  2. Checking IDs (Packet Headers): He checks every person’s ID (IP address and Port) against the list.
  3. Deny/Allow: If you’re on the list, you enter. If not, you’re blocked.

How It Works

Firewalls sit between a trusted network (your home/office) and an untrusted network (the internet).

Basic Firewall Allow/Block Logic
How Firewalls Work

Different Types of Firewalls

1. Packet Filtering (Stateless)

The most basic type. It looks at the header of a packet (Source IP, Dest IP, Port) and says Yes or No. It doesn’t remember previous packets.

  • Analogy: bouncer checks ID but forgets you the moment you walk inside.

2. Stateful Inspection

Smarter. It remembers the state of active connections. If you send a request OUT to a website, it automatically allows the reply IN.

  • Analogy: The bouncer remembers you went out for a smoke and lets you back in without checking ID again.
Stateful Inspection Sequence
Stateful Inspection Logic

3. Application Layer (Next-Gen)

The smartest. It looks inside the packet payload. It can block “Facebook Games” while allowing “Facebook Chat”.

Configuring ufw (Uncomplicated Firewall)

On Ubuntu/Debian, ufw is the standard tool. It’s much easier than the old iptables.

sudo ufw status

Check if the firewall is active

beginner
sudo ufw status
sudo ufw status verbose
sudo ufw allow 22/tcp

Allow SSH connections (DO THIS FIRST!)

beginner
sudo ufw allow [port]
sudo ufw allow 22/tcp
sudo ufw enable

Turn on the firewall

beginner
sudo ufw enable
sudo ufw enable
Don't Lock Yourself Out

Always allow SSH (Port 22) before enabling the firewall on a remote server. Otherwise, you will disconnect yourself forever.

More Essential UFW Commands

sudo ufw deny 23/tcp

Explicitly block Telnet (insecure — always block this)

beginner
  • Use deny to drop silently, reject to notify the sender
sudo ufw allow from 192.168.1.0/24

Allow all connections from your local network only

intermediate
  • Replace 192.168.1.0/24 with your actual LAN subnet
  • Great for allowing DB access from app servers only
sudo ufw status verbose

View all active rules with their actions and routing details

beginner
Status: active\n\nTo Action From\n-- ------ ----\n22/tcp ALLOW IN Anywhere\n80/tcp ALLOW IN Anywhere
sudo ufw delete allow 80/tcp

Remove a specific rule (the reverse of how you added it)

beginner
  • Or: sudo ufw status numbered, then sudo ufw delete [N]

Under the Hood: iptables

UFW is a friendly wrapper around the real firewall engine: iptables. Understanding it helps you debug.

UFWiptables
ComplexitySimple (human-readable)Complex (chains and tables)
Use CasePersonal servers, UbuntuEnterprise, advanced configs
PersistenceAuto-savedRequires saving manually
SpeedFine for most workloadsDirect kernel interface
sudo iptables -L -n -v

List all current iptables rules with packet counts

intermediate
  • -L lists, -n shows IPs (not hostnames), -v shows packet counts
  • Three chains: INPUT (inbound), OUTPUT (outbound), FORWARD (routing)
sudo iptables -A INPUT -s 203.0.113.5 -j DROP

Block all traffic from a specific attacker IP address

intermediate
Block IP 203.0.113.5 from reaching your server
  • -A = Append (add to end), -s = source, -j = jump to action (DROP/ACCEPT)
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

Allow HTTPS traffic on port 443

intermediate
  • -p tcp = protocol, --dport = destination port
  • Always add an ACCEPT rule before a default DROP policy
nftables: The Future of Linux Firewalls

Modern Linux kernels (5.x+) include nftables, the successor to iptables. It uses a more consistent syntax and is faster. Ubuntu 20.04+ uses nftables under the hood. UFW still works on top of it transparently.

Common Issues

ProblemCauseSolution
Website not loadingPort 80/443 blockedsudo ufw allow 80 & sudo ufw allow 443
Cannot SSH inPort 22 blockedAccess console, run sudo ufw allow 22
Ping not workingICMP blockedEdit /etc/ufw/before.rules to allow ICMP

🧠 Check Your Understanding

🧠

Firewall Mastery Check

Take a quick 3-question quiz to check your understanding.


Key Takeaways

  1. Firewalls filter traffic based on IP, Port, and Protocol.
  2. Stateless checks packets individually; Stateful tracks full connections (smarter).
  3. Application-layer firewalls (WAF) understand HTTP/DNS — they see attacks, not just ports.
  4. UFW is the friendly wrapper. iptables is the real engine underneath.
  5. Default policy = Deny All incoming. Allow only what you need.
  6. Always allow SSH before enabling UFW on a remote server — or you lock yourself out.
You Now Know

The firewall is your first line of defense. You know how to act like a Bouncer for your network using ufw.

Next Steps

Found this helpful? Explore more in the Security Hub!

📧

Get weekly IT guides

Join 5,000+ IT professionals

Subscribe Free
Type to start searching...