Skip to content

24. Firewalls

A server exposed to a network is scanned by automated vulnerability crawlers within minutes of booting. A properly configured, stateful firewall is your first and most critical perimeter defense.

A firewall acts as an access controller, inspecting incoming and outgoing network packets and deciding whether to accept, reject, or drop them based on defined security policies.

  • Inbound (Ingress) vs Outbound (Egress):
    • Inbound: Traffic initiated from outside heading into your server. Highly restricted in production.
    • Outbound: Traffic originating from your server heading out (e.g., pulling software packages). Typically permitted by default.
  • Stateful Packet Inspection (SPI): Modern firewalls track active TCP/UDP connections. When your server initiates an outbound query to an update repository, the firewall automatically permits the incoming response traffic without requiring an explicit inbound rule.
  • Packet Actions:
    • ACCEPT: Packet is allowed through to the target service.
    • REJECT: Packet is blocked, and an ICMP “port unreachable” error is sent back to the sender.
    • DROP: Packet is silently discarded with zero response, forcing the sender to wait for connection timeouts (ideal against malicious scanners).

The Linux kernel executes packet filtering directly within its Netfilter subsystem. Administrators configure Netfilter via user-space utilities:

User Space Tools (Frontends)
┌────────────────────────┐
│ firewalld │ UFW │
└─────┬───────────┴──────┘
│ │
┌─────▼───────────▼──────┐
│ nftables │ iptables │ (Low-level packet filter utilities)
└─────┬───────────┴──────┘
│
┌─────▼──────────────────┐
│ Netfilter (Kernel) │ (Kernel packet inspection engine)
└────────────────────────┘

| Feature | firewalld (firewall-cmd) | UFW (Uncomplicated Firewall) | |||| | Primary Distribution | Default in RHEL, CentOS, Rocky, Fedora | Default in Ubuntu, Debian | | Backend Engine | nftables (or legacy iptables) | iptables / nftables | | Configuration Model | Zone-based (dynamic trust levels) | Rule-based (port/IP list) | | Runtime Rule Updates | Dynamic (updates without dropping active sessions) | May require full reload on complex updates | | Advanced Networking | Rich rules, IP sets, direct interface, zones | Basic port/subnet filtering | | Ease of Use | Moderate (enterprise feature set) | Very simple (beginner-friendly CLI) |

firewalld is the enterprise standard on Red Hat and CentOS-based systems. It groups rules into Zones assigned to network interfaces.

  • public: The default zone for untrusted networks. Allows only explicitly permitted services.
  • trusted: All incoming traffic is accepted.
  • drop / block: Unsolicited incoming packets are discarded immediately.
  • internal / dmz: Used for private networks and exposed public web servers.
Terminal window
# Check overall firewall state
sudo firewall-cmd --state
# View default and active zones
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones
# Change zone assignment for an interface
sudo firewall-cmd --zone=internal --change-interface=eth1
Terminal window
# List all configured services and ports in the public zone
sudo firewall-cmd --zone=public --list-all
# List all open ports
sudo firewall-cmd --list-ports
# List all available predefined service definitions
firewall-cmd --get-services

[!IMPORTANT] By default, firewall-cmd applies rules only to the runtime memory. To ensure rules survive system reboots, always append the --permanent flag, followed by sudo firewall-cmd --reload.

Terminal window
# Allow predefined services permanently
sudo firewall-cmd --zone=public --add-service=http --permanent
sudo firewall-cmd --zone=public --add-service=https --permanent
# Allow custom TCP/UDP ports
sudo firewall-cmd --zone=public --add-port=8080/tcp --permanent
sudo firewall-cmd --zone=public --add-port=7000-7010/tcp --permanent
# Remove a port
sudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent
# Commit runtime modifications directly to permanent configuration
sudo firewall-cmd --runtime-to-permanent
# Reload configuration from disk
sudo firewall-cmd --reload
Terminal window
# Allow MySQL (3306) ONLY from a specific trusted IP
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port port=3306 protocol=tcp accept'
# Drop all traffic from a malicious subnet
sudo firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="10.10.10.0/24" drop'
# Port Forwarding: Redirect incoming traffic on port 80 to port 8080 locally
sudo firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
# Masquerading (NAT): Allow private LAN clients to route through this server
sudo firewall-cmd --zone=public --add-masquerade --permanent

If a server is under active compromise or DDoS attack, panic mode immediately drops all incoming and outgoing network traffic:

Terminal window
# Enable panic mode (cuts all network connections immediately)
sudo firewall-cmd --panic-on
# Disable panic mode
sudo firewall-cmd --panic-off
# Query panic state
firewall-cmd --query-panic

24.4 Managing UFW (Uncomplicated Firewall)

Section titled “24.4 Managing UFW (Uncomplicated Firewall)”

UFW is the standard firewall interface on Ubuntu and Debian systems.

Terminal window
# Check status (verbose shows default policies)
sudo ufw status
sudo ufw status verbose
# Enable or disable UFW
sudo ufw enable
sudo ufw disable
# Reload rules
sudo ufw reload
# Reset all rules to factory defaults
sudo ufw reset

Set default behavior for inbound and outbound traffic:

Terminal window
sudo ufw default deny incoming
sudo ufw default allow outgoing
Terminal window
# Allow services by name or port number
sudo ufw allow ssh
sudo ufw allow 22/tcp
sudo ufw allow 80/tcp
sudo ufw allow 443/tcp
# Allow port ranges
sudo ufw allow 1000:2000/tcp
sudo ufw allow 3000:4000/udp
# Deny a specific port
sudo ufw deny 8080
Terminal window
# Allow all traffic from a specific IP address
sudo ufw allow from 192.168.1.100
# Allow traffic from an IP to a specific port
sudo ufw allow from 192.168.1.100 to any port 3306 proto tcp
# Allow an entire CIDR subnet
sudo ufw allow from 192.168.1.0/24 to any port 22
# Allow traffic only on a specific network interface
sudo ufw allow in on eth1 to any port 5432 proto tcp
Terminal window
# List all active rules with their index numbers
sudo ufw status numbered
# Delete rule #2
sudo ufw delete 2
# Insert rule at the very top of the list (position 1)
sudo ufw insert 1 allow from 10.0.0.50 to any port 22 proto tcp
Terminal window
# Toggle logging
sudo ufw logging on
sudo ufw logging off
# Set logging verbosity (low, medium, high, full)
sudo ufw logging medium

UFW log records appear in /var/log/ufw.log or /var/log/syslog.

| Task | firewall-cmd (RHEL / Rocky / Fedora) | ufw (Ubuntu / Debian) | |||| | Check Status | sudo firewall-cmd --state | sudo ufw status | | List All Active Rules | sudo firewall-cmd --list-all | sudo ufw status verbose | | Open Single Port | sudo firewall-cmd --permanent --add-port=9000/tcp | sudo ufw allow 9000/tcp | | Open Port Range | sudo firewall-cmd --permanent --add-port=7000-7010/tcp | sudo ufw allow 7000:7010/tcp | | Allow Traffic from IP | sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="X.X.X.X" accept' | sudo ufw allow from X.X.X.X | | Allow IP to Specific Port | sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="X.X.X.X" port port=443 protocol=tcp accept' | sudo ufw allow from X.X.X.X to any port 443 proto tcp | | Delete Port Rule | sudo firewall-cmd --permanent --remove-port=9000/tcp | sudo ufw delete allow 9000/tcp | | Port Forwarding | sudo firewall-cmd --permanent --add-forward-port=port=80:proto=tcp:toport=8080 | Requires manual /etc/ufw/before.rules NAT | | Reload Rules | sudo firewall-cmd --reload | sudo ufw reload | | Emergency Lockdown | sudo firewall-cmd --panic-on | N/A (Disable external NIC or use iptables drop) |