Lab 13: Firewall with ufw
Time: 30 minutes | Level: Practitioner | Docker: docker run -it --rm ubuntu:22.04 bash
Overview
UFW (Uncomplicated Firewall) is Ubuntu's user-friendly frontend for iptables. This lab covers enabling UFW, managing allow/deny rules, working with numbered rules, setting default policies, and configuring logging. Since UFW requires kernel netfilter modules, we use --privileged Docker mode for live demos.
⚠️ Docker Note: Run with
docker run -it --privileged --rm ubuntu:22.04 bashfor live UFW commands. Standard Docker containers lack kernel module access. All examples below show verified output from--privilegedmode.
Step 1: Install and Check UFW Status
apt-get update -qq && apt-get install -y ufw
ufw version
ufw status💡 UFW is installed by default on Ubuntu Desktop but not Ubuntu Server minimal installs.
ufw statusshowsinactiveuntil you explicitly enable it. Never enable UFW on a remote server without first adding an SSH allow rule — you'll lock yourself out!
📸 Verified Output:
ufw 0.36.1
Copyright 2008-2021 Canonical Ltd.
Status: inactiveStep 2: Enable UFW and Set Default Policies
Default policies define what happens to traffic that doesn't match any rule.
💡 The golden rule: add SSH allow BEFORE enabling.
ufw allow sshis equivalent toufw allow 22/tcp. UFW's default after enabling isdeny incoming, allow outgoing— this is a secure starting point. On a remote server, SSH rules must come first or you'll be locked out.
📸 Verified Output:
Step 3: Allow Rules — Ports and Services
💡 Service names (
ssh,http,https) are resolved from/etc/services. Using names makes rules more readable.ufw allow sshcreates both IPv4 and IPv6 rules automatically.
📸 Verified Output:
Step 4: Deny Rules and IP-Specific Rules
💡 Rules are evaluated top-to-bottom. The first matching rule wins. This matters when combining broad
allowwith specificdenyrules — more specific rules should come first. IP-based rules are common for restricting admin services (SSH, databases) to trusted networks only.
📸 Verified Output:
Step 5: Working with Numbered Rules — Delete and Insert
💡 When deleting by number, always run
ufw status numberedfirst — rule numbers shift after deletions. Deleting rule 2 makes the old rule 3 become the new rule 2. For automation scripts, deleting by specification (ufw delete allow 80/tcp) is safer than by number.
📸 Verified Output:
Step 6: Default Policies
💡 The three policy directions:
incoming(traffic TO this host),outgoing(traffic FROM this host),routed(traffic THROUGH this host as a router). For servers: deny incoming + allow outgoing is the standard secure baseline. Only add allow rules for services you intentionally expose.
📸 Verified Output:
Step 7: UFW Logging and Application Profiles
💡 Application profiles (in
/etc/ufw/applications.d/) let packages register their own port definitions.ufw allow 'Nginx Full'would open both 80 and 443 if nginx is installed. Logging levelmediumlogs blocked packets + rate-limited connections — useful for intrusion detection.
📸 Verified Output:
Step 8: Capstone — Production Server Firewall Setup
Scenario: You're hardening a new web server that runs Nginx (ports 80/443) and needs SSH access only from a management subnet (10.10.0.0/24). All other traffic should be blocked.
💡 The order matters: SSH restriction before web traffic rules. With
deny incomingas default, only explicitly allowed traffic passes. In production: also considerufw limit ssh(rate-limiting) to prevent brute-force attacks — it auto-blocks IPs with 6+ connection attempts in 30 seconds.
📸 Verified Output:
Summary
ufw status
Show firewall status and rules
ufw status verbose
Show status with default policies
ufw status numbered
Show rules with index numbers
ufw enable
Activate firewall
ufw disable
Deactivate firewall (rules preserved)
ufw allow ssh
Allow by service name
ufw allow 80/tcp
Allow by port/protocol
ufw allow from IP to any port N
Allow from specific source
ufw deny 23
Deny port
ufw delete N
Delete rule by number
ufw default deny incoming
Block all inbound by default
ufw default allow outgoing
Allow all outbound by default
ufw logging medium
Set log verbosity
ufw app list
List application profiles
ufw limit ssh
Rate-limit SSH (anti-brute-force)
ufw --force reset
Reset all rules to defaults
Last updated
