CrackOps
Interview PrepTracksLinuxAWS LabSoonInterview RoomSoon
Sign inStart free
CrackOps

Interview prep for the people who keep production up.

[email protected]
ProductInterview PrepTracksDashboardInterview Room
RolesDevOpsSREPlatformCloud
CompanyAboutContactPrivacyTerms
© CrackOps
Linux / Security & Hardening / ufw

ufw

Manage the host firewall the simple way

Beginnerfirewallsecurityubuntu

ufw (Uncomplicated Firewall) is Ubuntu's front end to the kernel's netfilter firewall. With one-line rules like 'allow 443/tcp' or 'allow from 10.0.0.0/16 to any port 5432' you set a default-deny policy for incoming traffic and open only what the server needs.

Syntaxufw [--dry-run] enable|disable|status [verbose|numbered] | default allow|deny incoming|outgoing | [insert N] allow|deny|limit|reject RULE | delete RULE|N

Watch it run

A fresh Ubuntu server will run nginx. You want only SSH, HTTP and HTTPS reachable from the internet.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
ufw status

Even reading the firewall status needs root.

Anatomy of a command

$
commandsudo

Firewall rules change kernel state; ufw refuses to run without root.

How it works

ufw is a Python program that manages netfilter, the packet filter inside the kernel. On Ubuntu 24.04 it drives iptables-nft, so the rules end up as nftables rules. Your rules are stored in /etc/ufw/user.rules and user6.rules (IPv6 gets a copy of each rule unless you give an IPv4 address), defaults in /etc/default/ufw, and fixed rules that run before yours (loopback, established connections, ICMP, DHCP) in /etc/ufw/before.rules. 'ufw enable' loads everything into the kernel and enables ufw.service so it's reapplied at boot. Every incoming packet walks the chains: before rules first, then your rules in order, and the first match wins; if nothing matches, the default policy (deny incoming) applies. The connection tracking rule in before.rules accepts replies to connections the host started, which is why 'default allow outgoing' works without opening return ports. 'limit' uses the recent module to reject an IP that opens 6 or more connections within 30 seconds.

Options you'll actually use

status [verbose|numbered]Show rules; verbose adds defaults and logging, numbered adds rule numbers
enable / disableLoad rules and start at boot / flush and stop
default deny incomingPolicy for traffic no rule matches
allow PORT[/PROTO]Open a port: allow 443/tcp, allow 51820/udp
allow from CIDR to any port NOpen a port only for a source network
limit sshAllow, but reject sources opening 6+ connections in 30 s
deny / reject from IPBlock a source silently / with an ICMP or TCP reset
insert N RULEPut a rule at position N (order matters: first match wins)
delete N / delete RULERemove by number (from status numbered) or by repeating the rule
app list / allow 'Nginx Full'Use application profiles from /etc/ufw/applications.d
--dry-runPrint the resulting rules without applying them
show addedYour rules as the commands that created them

Common mistakes

sudo ufw enable on a remote server before allowing SSH

Default deny incoming applies immediately, and new SSH connections (sometimes even yours) are dropped: you're locked out.

sudo ufw allow OpenSSH (or limit ssh) first, then enable; keep a second session open while testing
Appending a deny rule after existing allow rules

Rules are evaluated in order and the first match wins, so the earlier allow still accepts the traffic.

sudo ufw insert 1 deny from IP (delete any existing copy first)
ufw allow 5432 to open PostgreSQL

Opens TCP and UDP from the entire internet.

sudo ufw allow from 10.0.0.0/16 to any port 5432 proto tcp
Trusting ufw to protect ports published by Docker

Docker's own iptables rules handle forwarded traffic before ufw sees it, so -p 8080:80 is exposed regardless of ufw.

Bind published ports to 127.0.0.1, use the DOCKER-USER chain, or a cloud security group

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the firewall status with rule numbers and the default policies.

  2. 2

    Preview, without applying, what allowing 8080/tcp would change.

  3. 3

    Allow SSH (rate-limited) and HTTPS, deny everything else incoming, enable the firewall, then remove the HTTPS rule by number.

Related commands

iptablesFilter and NAT packets with netfilter rulesfirewall-cmdManage firewalld zones, services and portsfail2ban-clientInspect and control fail2ban's brute-force bansssSee listening ports and open connections
Previousfail2ban-clientNextauditctl
Linux / Security & Hardening / ufw

ufw

Manage the host firewall the simple way

Beginnerfirewallsecurityubuntu

ufw (Uncomplicated Firewall) is Ubuntu's front end to the kernel's netfilter firewall. With one-line rules like 'allow 443/tcp' or 'allow from 10.0.0.0/16 to any port 5432' you set a default-deny policy for incoming traffic and open only what the server needs.

Syntaxufw [--dry-run] enable|disable|status [verbose|numbered] | default allow|deny incoming|outgoing | [insert N] allow|deny|limit|reject RULE | delete RULE|N

Watch it run

A fresh Ubuntu server will run nginx. You want only SSH, HTTP and HTTPS reachable from the internet.

devops@devops-lab: ~ — zsh — 96×28
➜ ~
Step 1 of 5
ufw status

Even reading the firewall status needs root.

Anatomy of a command

$
commandsudo

Firewall rules change kernel state; ufw refuses to run without root.

How it works

ufw is a Python program that manages netfilter, the packet filter inside the kernel. On Ubuntu 24.04 it drives iptables-nft, so the rules end up as nftables rules. Your rules are stored in /etc/ufw/user.rules and user6.rules (IPv6 gets a copy of each rule unless you give an IPv4 address), defaults in /etc/default/ufw, and fixed rules that run before yours (loopback, established connections, ICMP, DHCP) in /etc/ufw/before.rules. 'ufw enable' loads everything into the kernel and enables ufw.service so it's reapplied at boot. Every incoming packet walks the chains: before rules first, then your rules in order, and the first match wins; if nothing matches, the default policy (deny incoming) applies. The connection tracking rule in before.rules accepts replies to connections the host started, which is why 'default allow outgoing' works without opening return ports. 'limit' uses the recent module to reject an IP that opens 6 or more connections within 30 seconds.

Options you'll actually use

status [verbose|numbered]Show rules; verbose adds defaults and logging, numbered adds rule numbers
enable / disableLoad rules and start at boot / flush and stop
default deny incomingPolicy for traffic no rule matches
allow PORT[/PROTO]Open a port: allow 443/tcp, allow 51820/udp
allow from CIDR to any port NOpen a port only for a source network
limit sshAllow, but reject sources opening 6+ connections in 30 s
deny / reject from IPBlock a source silently / with an ICMP or TCP reset
insert N RULEPut a rule at position N (order matters: first match wins)
delete N / delete RULERemove by number (from status numbered) or by repeating the rule
app list / allow 'Nginx Full'Use application profiles from /etc/ufw/applications.d
--dry-runPrint the resulting rules without applying them
show addedYour rules as the commands that created them

Common mistakes

sudo ufw enable on a remote server before allowing SSH

Default deny incoming applies immediately, and new SSH connections (sometimes even yours) are dropped: you're locked out.

sudo ufw allow OpenSSH (or limit ssh) first, then enable; keep a second session open while testing
Appending a deny rule after existing allow rules

Rules are evaluated in order and the first match wins, so the earlier allow still accepts the traffic.

sudo ufw insert 1 deny from IP (delete any existing copy first)
ufw allow 5432 to open PostgreSQL

Opens TCP and UDP from the entire internet.

sudo ufw allow from 10.0.0.0/16 to any port 5432 proto tcp
Trusting ufw to protect ports published by Docker

Docker's own iptables rules handle forwarded traffic before ufw sees it, so -p 8080:80 is exposed regardless of ufw.

Bind published ports to 127.0.0.1, use the DOCKER-USER chain, or a cloud security group

Interview questions

Practice

Try these on a Linux machine of your own. Each task has a hint and a solution.

  1. 1

    Show the firewall status with rule numbers and the default policies.

  2. 2

    Preview, without applying, what allowing 8080/tcp would change.

  3. 3

    Allow SSH (rate-limited) and HTTPS, deny everything else incoming, enable the firewall, then remove the HTTPS rule by number.

Related commands

iptablesFilter and NAT packets with netfilter rulesfirewall-cmdManage firewalld zones, services and portsfail2ban-clientInspect and control fail2ban's brute-force bansssSee listening ports and open connections
Previousfail2ban-clientNextauditctl