Objective. Students write and apply firewall rules using ufw and iptables to control inbound and outbound traffic based on a defined security policy.
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.
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.
---
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:
---
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`.
---
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.
---
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.
---
---
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.
Answer each question, then check it. Nothing is recorded; this is your own practice.
A free tech program — complimentary, as our commitment to get America working. Help us build VoTechWorks. Tell us what works, what doesn’t, and what you want to learn.