Skip to content
Back to Series Network Security from Scratch
Security

Firewall: Concepts, Types, and Basic Configuration

Understand firewalls from concept to practice: stateful vs stateless, NGFW, essential rules, and how to build secure configurations with default-deny.

A
Alif Afriza Author
· · 4 min read
Firewall: Concepts, Types, and Basic Configuration

A firewall is the gatekeeper between your network and the outside world. It decides which packets may pass and which are denied. This article covers the concepts, types of firewalls, and how to build correct rules.

What Does a Firewall Do?

A firewall inspects passing packets and matches them against rules. Based on those rules, a packet can be allowed, rejected, or dropped (dropped without a reply). Rules are arranged in order, and usually the first matching decision applies.

Two decisions that often confuse beginners:

  • Reject: denies and notifies the sender with an error message. Useful so users know their connection was rejected.
  • Drop: silently discards the packet without a reply. Safer for closing unused ports, because attackers get no information.

Stateful vs Stateless

Stateless Firewall

Inspects each packet individually without memory. Fast, but easy to trick because it does not know the context of a conversation.

Stateful Firewall

Remembers the status of ongoing connections. It knows that packets like this are part of an already permitted connection, so it does not need to re-inspect everything. This is the modern standard. Stateful firewalls also have connection tracking, which can stop packets that do not match a legitimate conversation.

Firewall Types by Architecture

TypeCharacteristicWhen Used
Packet filteringInspects packet headers (IP, port, protocol)Simple routers, iptables
StatefulAdds more connection memoryNetwork firewall standard
Application / NGFWInspects application content, user, and contextEnterprises, granular requirements
Host-basedProtects a single device, not a networkLaptops, individual servers

Do not be fooled by labels. A firewall can use several techniques at once, and the best one is not the most expensive, but the one that best fits your needs and whose rules you understand.

Basic Principles of Rule Writing: Default-Deny

A correct firewall rule set starts with default-deny: everything is denied unless explicitly allowed.

[, new connections that are not to ports 443 and 22 are denied]

The logical explanation: it is harder to compile a complete “deny” list than a short “allow” list. Listing what is allowed is limited and auditable; listing what is forbidden is never finished.

Example Configuration: iptables for a Minimal Server

The example below uses default-deny logic for web services (ports 80/443) and SSH (port 22). The values are examples; adjust them to your needs.

# Set initial policy: deny all incoming, allow all outgoing
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# Allow established (response) connections to come in
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# Allow localhost (never block loopback, many services depend on it)
iptables -A INPUT -i lo -j ACCEPT

# Allow SSH (get used to limiting the source to your own IP if possible)
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -j ACCEPT

# Allow HTTP and HTTPS
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -m state --state NEW -j ACCEPT

# Drop everything else
iptables -A INPUT -j DROP

# Save the rules
iptables-save > /etc/iptables/rules.v4

Note the order: default-deny at the start as policy, specific allows in the middle, and drop at the end.

How to Test a Firewall

  1. Make sure the rules have been applied: iptables -L -n -v (view the dropped packet counters).
  2. Scan from outside with nmap -sS <ip> and make sure unauthorized ports do not appear.
  3. Make sure allowed services are still accessible.
  4. Save a configuration snapshot before major changes, so you can roll back if something goes wrong.

Note: the configuration above is for a server; do not apply it as-is to a home router or a device running many services, because it can cut off connections the system needs.

Next in the curriculum: a firewall works best when your network is already divided with VLANs and segmentation.

References

  • NIST SP 800-41 Rev 1, Guidelines on Firewalls and Firewall Policy, csrc.nist.gov/pubs/sp/800/41/r1/final (official guidance on firewall concepts).
  • RFC 2979, Behavior of and Requirements for Internet Firewalls, rfc-editor.org/rfc/rfc2979 (expected firewall behavior).
  • Official iptables / nftables documentation (man pages iptables(8), nft(8)).
  • William Stallings, Network Security Essentials, Pearson (firewall chapter in the context of security).
Open for Contributors

Your ideas deserve to be read by thousands of people.

ByteNesia is built for everyone. If you have experience, tutorials, or opinions about technology, write and share them here. Your name will be credited as a contributor.

Write anything: tool reviews, homelab tutorials, security tips, or your tech stories.

Discussion & Comments