ufw
ufw (Uncomplicated Firewall) is an easy command-line front end for the Linux netfilter firewall. It does not filter packets itself: it turns simple commands like ufw allow 80/tcp into iptables rules and loads them into the kernel.
106.1 Why is it used?
- Writing raw iptables is error-prone; ufw gives short commands: allow, deny, reject, limit plus default policies.
- Rules are saved in /etc/ufw/user.rules and loaded into ufw-user-* chains.
- limit slows SSH brute force; application profiles allow ports by name.
- Linux only: installed by default on Ubuntu, apt install ufw on Debian. Windows has Defender Firewall, macOS its own firewall/pf.
106.2 How it works
- Step 1
1.Admin runs sudo ufw allow ...
- Step 2
2.Rule saved in /etc/ufw/user.rules, loaded into ufw-user-input
- Step 3
3.HTTPS packet from client 192.168.126.20 checks rules top to bottom
- Step 4
4.Rule 2 matches -> packet reaches the web server
- Step 5
5.Port 3306: no rule matches -> default deny incoming -> dropped
First match wins; nothing matched = default policy
| Action | iptables target | What the client sees |
|---|---|---|
| allow | ACCEPT | Connection works |
| deny | DROP | No reply, waits until timeout |
| reject | REJECT --reject-with tcp-reset | Immediate Connection refused |
| limit | recent match: 6+ new connections in 30 s -> ufw-user-limit | Brute force slowed down |
106.3 Lab check and firewall state
Lab: ufw in an Alpine container without NET_ADMIN, because enabling ufw needs xt_limit/recent/LOG modules that are not loaded and modules are never loaded into the VM. So ufw status gives permission denied, but the rule files and generated iptables lines are real.
# n106-ufw: Alpine, no NET_ADMINufw --versiongrep -cE 'xt_(limit|recent|LOG)' /proc/moduleszcat /proc/config.gz | \ grep -E 'H_(LIMIT|RECENT)=|ET_LOG='ufw status$ ufw --versionufw 0.36.2Copyright 2008-2023 Canonical Ltd.$ grep -cE 'xt_(limit|recent|LOG)' /proc/modules0CONFIG_NETFILTER_XT_TARGET_LOG=mCONFIG_NETFILTER_XT_MATCH_LIMIT=mCONFIG_NETFILTER_XT_MATCH_RECENT=m$ ufw status!ERROR: problem running iptables: iptables v1.8.10 (nf_tables): Could not fetch rule set generation id: Permission denied (you must be root)| Command / option | Meaning |
|---|---|
| --dry-run | No change, only preview |
| --force | Skip the confirmation prompt (enable, reset, delete) |
| enable / disable | Load + on at boot / unload + off at boot |
| reload | Load rules again |
| reset | Back to installation defaults (prompt) |
$ ufw --dry-run enable> running ufw-initFirewall is active and enabled on system startup$ ufw --dry-run disableFirewall stopped and disabled on system startup$ ufw reloadFirewall not enabled (skipping reload)106.4 Default policy: safe setup order
ufw limit SSHufw default deny incomingufw default allow outgoingufw default deny routedufw logging mediumgrep recent /etc/ufw/user.rulesgrep -E 'ENABLED|LOGLEVEL' /etc/ufw/ufw.conf$ ufw limit SSHRules updatedSkipping unsupported IPv6 'limit' rule$ ufw default deny incomingDefault incoming policy changed to 'deny'$ ufw default allow outgoingDefault outgoing policy changed to 'allow'$ ufw default deny routedDefault routed policy changed to 'deny'$ grep recent /etc/ufw/user.rules-A ufw-user-input -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --set -m comment --comment 'dapp_SSH'-A ufw-user-input -p tcp --dport 22 -m conntrack --ctstate NEW -m recent --update --seconds 30 --hitcount 6 -j ufw-user-limit -m comment --comment 'dapp_SSH'$ ufw logging mediumLogging enabled$ grep -E 'ENABLED|LOGLEVEL' /etc/ufw/ufw.confENABLED=noLOGLEVEL=medium- Allow/limit SSH first, otherwise enable locks your own remote session.
- default reject sends an error reply instead of a drop; default allow routed allows forwarding.
106.5 Status, reports and logging
| Command | Shows |
|---|---|
| status / status verbose | State + rules / + defaults and logging level |
| status numbered | [1] [2] numbers for delete/insert |
| show raw / builtins / before-rules / user-rules / after-rules | Running iptables chains |
| show listening | Open ports with their rules |
| show added | Rules as you added them |
| logging on (low) | Blocked packets, rate limited |
| logging medium | + allowed, INVALID, new connections |
| logging high | full | Less / no rate limit |
106.6 Rule syntax
ufw allow 80,443/tcpufw deny 8080/tcpufw reject 8081/tcpufw reject telnet comment 'unencrypted'ufw allow log 25/tcpgrep '^-A ufw-user-input' /etc/ufw/user.rules$ ufw allow 80,443/tcpRules updatedRules updated (v6)$ grep '^-A ufw-user-input' /etc/ufw/user.rules-A ufw-user-input -p tcp -m multiport --dports 80,443 -j ACCEPT-A ufw-user-input -p tcp --dport 8080 -j DROP-A ufw-user-input -p tcp --dport 8081 -j REJECT --reject-with tcp-reset-A ufw-user-input -p tcp --dport 23 -j REJECT --reject-with tcp-reset-A ufw-user-input -p tcp --dport 25 -j ufw-user-logging-input-A ufw-user-input -p tcp --dport 25 -j ACCEPTufw allow from 192.168.126.0/24 to any port 53ufw allow in on eth1 to any port 3306ufw allow proto udp to any port 123ufw --dry-run allow to 10.0.0.1 proto gregrep '^-A ufw-user-input' /etc/ufw/user.rules-A ufw-user-input -p tcp --dport 53 -s 192.168.126.0/24 -j ACCEPT-A ufw-user-input -p udp --dport 53 -s 192.168.126.0/24 -j ACCEPT-A ufw-user-input -i eth1 -p tcp --dport 3306 -j ACCEPT-A ufw-user-input -i eth1 -p udp --dport 3306 -j ACCEPT-A ufw-user-input -p udp --dport 123 -j ACCEPT$ ufw --dry-run allow to 10.0.0.1 proto gre### tuple ### allow gre any 10.0.0.1 any 0.0.0.0/0 in-A ufw-user-input -p gre -d 10.0.0.1 -j ACCEPT- A port with /tcp makes a TCP-only rule; without proto both TCP and UDP lines are created.
- 80,443 becomes one multiport rule; ranges use a colon (8080:8090), max 15 ports.
- Service names map to ports (telnet -> 23); comment is saved with the rule; log / log all add LOG lines.
- A rule without in/out applies to incoming traffic; ah, esp, gre, vrrp take no port; IPv6 needs IPV6=yes.
106.7 Delete, insert, prepend and routed rules
ufw insert 1 deny from 203.0.113.5ufw prepend deny from 203.0.113.0/24ufw route allow in on eth1 out on eth0ufw delete 2 < /dev/nullufw --force delete 2ufw delete deny 8080/tcpufw show added$ ufw route allow in on eth1 out on eth0Rules updatedRules updated (v6)$ grep '^-A ufw-user-' /etc/ufw/user.rules-A ufw-user-forward -i eth1 -o eth0 -j ACCEPT$ ufw delete 2 < /dev/nullDeleting: deny from 203.0.113.5Proceed with operation (y|n)? Aborted$ ufw --force delete 2Rules updated$ ufw delete deny 8080/tcpRules updatedRules updated (v6)$ ufw show addedufw deny from 203.0.113.0/24ufw limit SSHufw allow 80,443/tcpufw reject 8081/tcpufw route allow in on eth1 out on eth0- insert NUM puts a rule at that position; prepend puts it on top of the same IP version.
- delete NUM removes only that one rule; delete RULE removes both the IPv4 and IPv6 copies.
- route rules go into the FORWARD chain (ufw-user-forward); also set net/ipv4/ip_forward=1 in ufw's sysctl.conf.
106.8 Application profiles
ufw app list | head -12ufw app info SSHufw app info 'WWW Full'ufw app default skip$ ufw app list | head -12Available applications: AIM Bonjour CIFS DNS Deluge IMAP IMAPS$ ufw app info SSHProfile: SSHTitle: SSH serverPort: 22/tcp$ ufw app info 'WWW Full'Profile: WWW FullPorts: 80,443/tcp$ ufw app default skipDefault application policy changed to 'skip'- Profiles are INI files in /etc/ufw/applications.d with title, description and ports; on Ubuntu SSH is named OpenSSH.
- app update NAME refreshes rules after editing a profile; app default (skip by default) decides what --add-new does; allow is a security risk.
106.9 Real-world example: deny vs reject on real packets
# srv 192.168.126.10: lines ufw generatediptables -A INPUT -p tcp --dport 8080 -j DROPiptables -A INPUT -p tcp --dport 8081 \ -j REJECT --reject-with tcp-reset# cli 192.168.126.20time nc -zv -w 3 192.168.126.10 8080time nc -zv -w 3 192.168.126.10 8081iptables -L INPUT -v -n$ time nc -zv -w 3 192.168.126.10 8080!nc: connect to 192.168.126.10 port 8080 (tcp) timed out: Operation in progressreal 0m 3.00s$ time nc -zv -w 3 192.168.126.10 8081!nc: connect to 192.168.126.10 port 8081 (tcp) failed: Connection refusedreal 0m 0.00s$ iptables -L INPUT -v -n pkts bytes target prot opt in out source destination 3 180 DROP 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8080 1 60 REJECT 6 -- * * 0.0.0.0/0 0.0.0.0/0 tcp dpt:8081 reject-with tcp-reset106.10 Common mistakes
- Step 1
1.Container published with -p 8080:80; ufw has deny 8080
- Step 2
2.Docker inserts its own NAT and FORWARD rules before ufw's rules
- Step 3
3.Internet packet is DNATed and forwarded, never hitting ufw-user-input
- Step 4
4.It reaches the container web :80 directly
Fix: bind published ports (127.0.0.1:8080:80) or filter in DOCKER-USER
- Enabling ufw over SSH before allowing SSH: you lock yourself out.
- deny 8080 but the client gets Connection refused at once: deny should time out, so a reject rule or closed port is answering and your deny is not matching.
- Forgetting the order: first match wins, so a broad allow above a deny makes the deny useless; use insert/prepend.
- Using route rules without enabling IP forwarding.
106.11 Interview tips
- Safe enable on a remote server: 1) limit/allow SSH, 2) default deny incoming + allow outgoing + needed ports, 3) --dry-run, enable, verify with status verbose / numbered.
- deny = silent DROP (timeout), reject = TCP reset (refused), limit = 6 new connections in 30 s.
- ufw is only a front end: the real filtering is iptables/netfilter in the kernel.
- Mention the Docker bypass and DOCKER-USER chain.