Firewall Fundamentals

A firewall is your network's first line of defense. It monitors and controls incoming and outgoing network traffic based on predetermined security rules, acting as a barrier between your trusted internal network and untrusted external networks like the internet.

How Firewalls Work

Firewalls examine network packets (small chunks of data) and decide whether to allow or block them based on rules you define. Think of it as a security guard checking IDs at a building entrance.

Basic Firewall Operation

  1. Packet arrives: Data comes in from the network
  2. Inspection: Firewall examines packet headers (source, destination, port, protocol)
  3. Rule matching: Packet is compared against firewall rules
  4. Decision: Packet is allowed, blocked, or logged
Internet Traffic
      │
      ▼
┌─────────────┐
│  FIREWALL   │
│             │
│ Rules Check │
│  ┌───────┐  │
│  │Allow? │  │
│  └───┬───┘  │
└──────┼──────┘
       │
   ┌───┴───┐
   │       │
   ▼       ▼
 Allow   Block
   │       │
   ▼       X
Internal
Network
info Inbound vs Outbound

Inbound rules control traffic coming into your network from the internet. Outbound rules control traffic leaving your network. Most home firewalls focus on inbound protection, while enterprise firewalls also heavily regulate outbound traffic.

Types of Firewalls

By Filtering Method

Type How It Works Pros/Cons
Packet Filtering Examines individual packets based on headers Fast but basic; no context awareness
Stateful Inspection Tracks connection state and context More secure; understands sessions
Application Layer Inspects actual application data Deep inspection but slower
Next-Gen (NGFW) Combines all methods + threat intelligence Most comprehensive protection

By Deployment

Stateful vs Stateless

The key difference between modern and legacy firewalls:

Stateless (Packet Filtering) Stateful
Each packet examined independently Tracks entire connection state
No memory of previous packets Remembers established connections
Can't detect session hijacking Can identify suspicious behavior
Faster but less secure Slightly slower but much more secure

Firewall Rules

Firewall rules define what traffic is allowed or denied. Rules are processed in order, and the first matching rule wins.

Rule Components

Example Rules

# Allow all outbound HTTPS traffic
iptables -A OUTPUT -p tcp --dport 443 -j ACCEPT

# Allow inbound SSH only from specific IP
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.100 -j ACCEPT

# Block all other inbound SSH
iptables -A INPUT -p tcp --dport 22 -j DROP

# Allow established connections (stateful)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Default deny all inbound
iptables -P INPUT DROP
lightbulb Default Deny Principle

The most secure approach is "default deny" - block everything by default, then explicitly allow only what's needed. This is better than trying to block known bad things while allowing everything else.

Common Ports to Know

Port Service Notes
22 SSH Secure shell - restrict to known IPs
80 HTTP Unencrypted web traffic
443 HTTPS Encrypted web traffic
3389 RDP Remote Desktop - major attack target
3306 MySQL Database - never expose publicly
5432 PostgreSQL Database - never expose publicly

Best Practices

Configuration Guidelines

  1. Default deny: Start with blocking everything, then allow specific services
  2. Least privilege: Only open ports that are absolutely necessary
  3. Document rules: Keep track of why each rule exists
  4. Regular audits: Review rules periodically and remove unused ones
  5. Log everything: Enable logging for blocked traffic to detect attacks
  6. Test changes: Always test new rules in a non-production environment first
warning Don't Lock Yourself Out

When configuring firewalls remotely (especially via SSH), always ensure you have an alternative way to access the system. A wrong rule can cut off your own access. Consider using a console connection or out-of-band management.

What Not To Do

Home vs Enterprise Firewalls

Home Router Firewall

Your home router likely has a basic firewall built in:

Recommended settings:

Enterprise Firewall Features

Business-grade firewalls offer additional capabilities:

Check Your Open Ports

Scan your IP to see what ports are visible from the internet.

radar Run Port Scan