🚧 Under construction — VoTechWorks is being built. A free tech program — complimentary, as our commitment to get America working.
voTechWorksGrade 13 · a vocational technology learning center, by urbanicity
newSchoolWorks.com › VoTechWorks › Technology & Design › Cybersecurity › Linux for Cybersecurity › Lesson 7
TEC-CYBE44-04 · Lesson 7 · 14.5 clock hours · lab

Linux Firewall Configuration with iptables and ufw

Objective. Students write and apply firewall rules using ufw and iptables to control inbound and outbound traffic based on a defined security policy.

⚠ Pending professional review

This lesson, its worked examples and its practice questions has not yet been verified by an industry-certified professional or by a certification or approval body. It is a study draft and may contain errors. Do not use it as instructions for work on a job site or on live equipment. Always follow your instructor, your supervising professional, and current official references and regulations.

VoTechWorks program approval and accreditation are still in progress. Credentials are issued only by the certifying bodies named in each program, after their own exams.

Linux Firewall Configuration with iptables and ufw

Why Firewalls Matter in a Security Policy

A firewall enforces a written security policy by deciding which network traffic is allowed and which is blocked. Without a firewall, every port a service listens on is reachable by any host on the network. In cybersecurity, the principle of least privilege applies to network access just as it does to user accounts: open only the ports that are explicitly required, and deny everything else. NIST SP 800-41 Rev 1 provides federal guidance on firewall policy design and is a standard reference for this topic.

Linux provides two main tools for host-based firewall management: iptables, the traditional low-level interface to the kernel's netfilter subsystem, and ufw (Uncomplicated Firewall), a higher-level front end designed to make common tasks easier. Both tools write rules into the same kernel subsystem, but they differ in syntax and abstraction level.

---

Understanding iptables Chains and Tables

iptables organizes rules into tables (filter, nat, mangle, raw) and chains within each table. For basic host firewall work, the filter table is what you use. It contains three default chains:

Each chain has a default policy (ACCEPT or DROP). Best practice is to set default policies to DROP and then explicitly permit what is needed. Rules are evaluated top to bottom; the first matching rule wins.

iptables can operate in two modes of matching:

---

Using ufw for Policy-Based Rules

ufw translates plain-language commands into iptables rules. Before writing rules, enable ufw with `sudo ufw enable`. The default posture should be:

```

sudo ufw default deny incoming

sudo ufw default allow outgoing

```

To allow SSH only from a specific management subnet (for example, 10.0.1.0/24):

```

sudo ufw allow from 10.0.1.0/24 to any port 22 proto tcp

```

To allow HTTPS from anywhere:

```

sudo ufw allow 443/tcp

```

Check the full ruleset with:

```

sudo ufw status verbose

```

To delete a rule, use `sudo ufw delete` followed by the original rule syntax, or reference rules by number from `sudo ufw status numbered`.

---

Working Directly with iptables

Before using raw iptables alongside ufw, disable ufw with `sudo ufw disable`; running both simultaneously creates unpredictable rule ordering.

Flush existing rules first:

```

sudo iptables -F

sudo iptables -X

```

Set restrictive default policies:

```

sudo iptables -P INPUT DROP

sudo iptables -P FORWARD DROP

sudo iptables -P OUTPUT ACCEPT

```

Allow loopback traffic (always required):

```

sudo iptables -A INPUT -i lo -j ACCEPT

```

Allow established/related sessions (stateful):

```

sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

```

Allow SSH from management subnet only:

```

sudo iptables -A INPUT -p tcp -s 10.0.1.0/24 --dport 22 -j ACCEPT

```

Allow HTTPS from anywhere:

```

sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT

```

Save the ruleset so it survives a reboot:

```

sudo iptables-save > /etc/iptables/rules.v4

```

Restore it on boot with `iptables-restore < /etc/iptables/rules.v4`, or install the `iptables-persistent` package to automate this.

---

Auditing and Listing Rules

To review the current ruleset with line numbers and packet counts:

```

sudo iptables -L -v -n --line-numbers

```

The `-n` flag suppresses DNS lookups, which speeds up output and avoids misleading hostname resolution. When auditing a partner's configuration, read each rule against the written security policy and flag any rule that permits traffic not explicitly authorized.

---

Lab Safety and Best Practices

---

Verifying with nmap

From the second VM, run a targeted scan against the firewall host:

```

nmap -sV -p 22,23,80,443 <target-IP>

```

Expected output under a correct policy: port 22 open only when the scanning VM is in the management subnet, port 443 open, all others filtered or closed. Any deviation indicates a policy violation.

Key terms9

iptablesA Linux command-line utility that configures packet filtering rules in the kernel's netfilter subsystem, organized into tables and chains.
ufw (Uncomplicated Firewall)A higher-level front end for iptables on Linux that simplifies writing and managing firewall rules using plain-language commands.
ChainAn ordered list of iptables rules within a table; the three default filter-table chains are INPUT, OUTPUT, and FORWARD.
Default policyThe action (ACCEPT or DROP) that iptables applies to a packet when no rule in the chain matches it.
Stateful inspection (conntrack)A firewall technique that tracks the state of network connections (NEW, ESTABLISHED, RELATED, INVALID) so that reply packets for allowed sessions are automatically permitted.
Stateless packet filteringA firewall method that evaluates each packet independently based only on header fields such as source IP, destination port, and protocol, without tracking connection state.
iptables-save / iptables-restoreLinux utilities that export the current iptables ruleset to a text file and reload it, making firewall configuration persistent across reboots.
Least privilege (network)The principle that network access should be limited to only the ports and sources explicitly required by a defined security policy, with all other traffic denied by default.
NIST SP 800-41 Rev 1A National Institute of Standards and Technology publication providing guidelines on firewall technologies, policy, and deployment best practices.
In class: Given a written security policy (allow SSH from a management subnet only, allow HTTPS from anywhere, deny all other inbound), translate the policy into ufw rules; verify with nmap from a second VM that only the intended ports are reachable. · Drop to raw iptables: flush the ufw rules, implement the same policy using iptables -A INPUT/OUTPUT chains, save the ruleset with iptables-save, and explain the difference between stateless and stateful (conntrack) rule matching in a written comparison. · Intentionally introduce two policy violations in the ruleset (e.g., allow Telnet inbound, allow all outbound without restriction); swap VMs with a partner who must discover and fix the violations using iptables -L and ufw status verbose, documenting each finding.
Materials: Two Linux VMs, ufw, iptables, iptables-save/restore, nmap, CompTIA Security+ Study Guide (Network Security domain), NIST SP 800-41 Rev 1 (Guidelines on Firewalls)
Check: nmap scan results from the second VM must match only the allowed ports; partner must identify and correct both violations; pass = nmap output matches policy exactly and both violations documented and remediated.

Practice check8

Answer each question, then check it. Nothing is recorded; this is your own practice.

Network Security
1. A security policy states: allow SSH from 10.0.1.0/24 only; allow HTTPS from anywhere; deny all other inbound. Which ufw command correctly restricts SSH to the management subnet?
Network Security
2. After flushing iptables rules and setting the INPUT default policy to DROP, which rule must be added first to prevent the system from losing all connectivity to itself?
Threats, Attacks and Vulnerabilities
3. What is the primary advantage of using a stateful conntrack rule (-m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT) over a stateless rule that opens a broad port range for return traffic?
Network Security
4. An administrator saves iptables rules with the command: sudo iptables-save > /etc/iptables/rules.v4. What is the purpose of this step?
Threats, Attacks and Vulnerabilities
5. During a peer audit, a student finds the rule: iptables -A INPUT -p tcp --dport 23 -j ACCEPT. Why is this a policy violation?
Network Security
6. A student runs sudo iptables -L -v -n --line-numbers. What does the -n flag specifically do in this command?
Network Security
7. A nmap scan from the second VM against the firewall host returns port 443 as open and port 22 as filtered when run from outside the management subnet. What does this result indicate?
Architecture and Design
8. According to best practice and NIST SP 800-41 guidance, which default firewall posture should be applied to inbound traffic on a host firewall?
Record this lesson on my transcript. Sign in with your key and enrollment email to record lessons (enroll free if you have no key). This page stays open to read and practise.