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.
24.1 Firewall Concepts & Architecture
Section titled “24.1 Firewall Concepts & Architecture”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.
Core Terminology
Section titled “Core Terminology”- 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).
24.2 The Linux Firewall Ecosystem
Section titled “24.2 The Linux Firewall Ecosystem”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) └────────────────────────┘firewalld vs UFW Comparison
Section titled “firewalld vs UFW Comparison”| 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) |
24.3 Managing firewalld (firewall-cmd)
Section titled “24.3 Managing firewalld (firewall-cmd)”firewalld is the enterprise standard on Red Hat and CentOS-based systems. It groups rules into Zones assigned to network interfaces.
1. Zone Concepts
Section titled “1. Zone Concepts”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.
# Check overall firewall statesudo firewall-cmd --state
# View default and active zonesfirewall-cmd --get-default-zonefirewall-cmd --get-active-zones
# Change zone assignment for an interfacesudo firewall-cmd --zone=internal --change-interface=eth12. Inspecting Rules
Section titled “2. Inspecting Rules”# List all configured services and ports in the public zonesudo firewall-cmd --zone=public --list-all
# List all open portssudo firewall-cmd --list-ports
# List all available predefined service definitionsfirewall-cmd --get-services3. Opening and Closing Ports & Services
Section titled “3. Opening and Closing Ports & Services”[!IMPORTANT] By default,
firewall-cmdapplies rules only to the runtime memory. To ensure rules survive system reboots, always append the--permanentflag, followed bysudo firewall-cmd --reload.
# Allow predefined services permanentlysudo firewall-cmd --zone=public --add-service=http --permanentsudo firewall-cmd --zone=public --add-service=https --permanent
# Allow custom TCP/UDP portssudo firewall-cmd --zone=public --add-port=8080/tcp --permanentsudo firewall-cmd --zone=public --add-port=7000-7010/tcp --permanent
# Remove a portsudo firewall-cmd --zone=public --remove-port=8080/tcp --permanent
# Commit runtime modifications directly to permanent configurationsudo firewall-cmd --runtime-to-permanent
# Reload configuration from disksudo firewall-cmd --reload4. Advanced: Rich Rules & Port Forwarding
Section titled “4. Advanced: Rich Rules & Port Forwarding”# Allow MySQL (3306) ONLY from a specific trusted IPsudo 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 subnetsudo 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 locallysudo firewall-cmd --zone=public --add-forward-port=port=80:proto=tcp:toport=8080 --permanent
# Masquerading (NAT): Allow private LAN clients to route through this serversudo firewall-cmd --zone=public --add-masquerade --permanent5. Emergency Panic Mode
Section titled “5. Emergency Panic Mode”If a server is under active compromise or DDoS attack, panic mode immediately drops all incoming and outgoing network traffic:
# Enable panic mode (cuts all network connections immediately)sudo firewall-cmd --panic-on
# Disable panic modesudo firewall-cmd --panic-off
# Query panic statefirewall-cmd --query-panic24.4 Managing UFW (Uncomplicated Firewall)
Section titled “24.4 Managing UFW (Uncomplicated Firewall)”UFW is the standard firewall interface on Ubuntu and Debian systems.
1. Basic Lifecycle Commands
Section titled “1. Basic Lifecycle Commands”# Check status (verbose shows default policies)sudo ufw statussudo ufw status verbose
# Enable or disable UFWsudo ufw enablesudo ufw disable
# Reload rulessudo ufw reload
# Reset all rules to factory defaultssudo ufw reset2. Default Policies
Section titled “2. Default Policies”Set default behavior for inbound and outbound traffic:
sudo ufw default deny incomingsudo ufw default allow outgoing3. Port & Service Rules
Section titled “3. Port & Service Rules”# Allow services by name or port numbersudo ufw allow sshsudo ufw allow 22/tcpsudo ufw allow 80/tcpsudo ufw allow 443/tcp
# Allow port rangessudo ufw allow 1000:2000/tcpsudo ufw allow 3000:4000/udp
# Deny a specific portsudo ufw deny 80804. IP, Subnet & Interface Restrictions
Section titled “4. IP, Subnet & Interface Restrictions”# Allow all traffic from a specific IP addresssudo ufw allow from 192.168.1.100
# Allow traffic from an IP to a specific portsudo ufw allow from 192.168.1.100 to any port 3306 proto tcp
# Allow an entire CIDR subnetsudo ufw allow from 192.168.1.0/24 to any port 22
# Allow traffic only on a specific network interfacesudo ufw allow in on eth1 to any port 5432 proto tcp5. Managing Rules by Number
Section titled “5. Managing Rules by Number”# List all active rules with their index numberssudo ufw status numbered
# Delete rule #2sudo 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 tcp6. Logging
Section titled “6. Logging”# Toggle loggingsudo ufw logging onsudo ufw logging off
# Set logging verbosity (low, medium, high, full)sudo ufw logging mediumUFW log records appear in /var/log/ufw.log or /var/log/syslog.
24.5 Firewall Operations Reference Guide
Section titled “24.5 Firewall Operations Reference Guide”| 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) |