Batch 5: Protocols & Security Networking Apache & NGINX Cloud ← Full TOC

← Networking Index

🔥 Firewalls & iptables — iptables, nftables, ufw, firewalld — packet filtering

Linux firewalls use the netfilter kernel framework. iptables is the classic userspace tool. nftables is the modern replacement. ufw (Ubuntu) and firewalld (RHEL) provide friendlier interfaces.

📋 Reference

ItemSyntax / ValueDescription
iptables -Liptables -L -n -vList all rules with counters
iptables -A INPUTiptables -A INPUT -p tcp --dport 22 -j ACCEPTAppend rule to INPUT chain
iptables -Iiptables -I INPUT 1 -p tcp --dport 80 -j ACCEPTInsert rule at position 1
iptables -Diptables -D INPUT -p tcp --dport 80 -j ACCEPTDelete specific rule
iptables -Fiptables -FFlush (delete) all rules in all chains
iptables -Piptables -P INPUT DROPSet default policy for chain
ACCEPTiptables -A INPUT -j ACCEPTAllow packet
DROPiptables -A INPUT -j DROPSilently discard packet
REJECTiptables -A INPUT -j REJECTDiscard and send error to sender
-p tcp/udpiptables -A INPUT -p tcpProtocol filter
--dportiptables -A INPUT -p tcp --dport 443Destination port
--sportiptables -A INPUT -p tcp --sport 1024:65535Source port range
-s source-s 192.168.1.0/24Source IP/range filter
-d dest-d 10.0.0.5Destination IP filter
-i interface-i eth0Input interface filter
-o interface-o eth0Output interface filter
-m state--state ESTABLISHED,RELATEDConnection state matching
iptables-saveiptables-save > /etc/iptables/rules.v4Save rules to file
iptables-restoreiptables-restore < /etc/iptables/rules.v4Restore rules from file
ufw enablesudo ufw enableEnable Ubuntu firewall
ufw allowsudo ufw allow 22/tcpAllow port
ufw denysudo ufw deny 3306/tcpDeny port
ufw statussudo ufw status verboseShow all rules
ufw deletesudo ufw delete allow 80/tcpDelete rule
ufw limitsudo ufw limit 22/tcpRate limit — blocks brute force
firewalldsudo firewall-cmd --permanent --add-service=httpsRHEL/CentOS firewall
nftablesnft list rulesetModern iptables replacement
nft add rulenft add rule inet filter input tcp dport 22 acceptnftables rule syntax

💡 Example

# ── iptables — complete server setup ─────────────────────────
#!/bin/bash
# Basic web server firewall rules

# Flush existing rules
iptables -F
iptables -X
iptables -t nat -F

# Default policies — deny everything
iptables -P INPUT   DROP
iptables -P FORWARD DROP
iptables -P OUTPUT  ACCEPT   # allow all outbound

# Allow loopback (localhost) traffic
iptables -A INPUT -i lo -j ACCEPT

# Allow established and related connections
iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# Allow SSH (port 22) — from specific IP only
iptables -A INPUT -p tcp --dport 22 -s 203.0.113.50 -j ACCEPT  # office IP
iptables -A INPUT -p tcp --dport 22 -m conntrack --ctstate NEW -m limit   --limit 3/min --limit-burst 10 -j ACCEPT  # rate-limited for others

# Allow HTTP and HTTPS
iptables -A INPUT -p tcp --dport 80  -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# Allow ICMP ping
iptables -A INPUT -p icmp --icmp-type echo-request -m limit   --limit 1/s --limit-burst 10 -j ACCEPT

# Log and drop everything else
iptables -A INPUT -m limit --limit 5/min -j LOG   --log-prefix "iptables-dropped: " --log-level 7
iptables -A INPUT -j DROP

# Save rules (Ubuntu)
iptables-save > /etc/iptables/rules.v4
# Make persistent: apt install iptables-persistent

# ── ufw — Ubuntu Uncomplicated Firewall ──────────────────────
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp    comment 'SSH'
sudo ufw allow 80/tcp    comment 'HTTP'
sudo ufw allow 443/tcp   comment 'HTTPS'
sudo ufw limit 22/tcp    # rate limit SSH brute force
sudo ufw allow from 203.0.113.0/24 to any port 5432  # PostgreSQL from office
sudo ufw deny 3306/tcp   # block MySQL from internet
sudo ufw --force enable
sudo ufw status verbose
sudo ufw status numbered  # show with numbers for deletion
sudo ufw delete 4          # delete rule number 4

# ── nftables — modern replacement ────────────────────────────
# /etc/nftables.conf
cat > /tmp/nftables.conf << 'EOF'
flush ruleset

table inet filter {
    chain input {
        type filter hook input priority 0; policy drop;

        iif lo accept comment "loopback"
        ct state established,related accept comment "established"
        ct state invalid drop comment "invalid packets"

        ip protocol icmp  icmp type echo-request limit rate 1/second accept
        ip6 nexthdr icmpv6 icmpv6 type echo-request limit rate 1/second accept

        tcp dport 22  ct state new limit rate 3/minute accept comment "SSH"
        tcp dport {80, 443} accept comment "HTTP/S"

        limit rate 5/minute log prefix "nft-drop: " drop
    }
    chain forward { type filter hook forward priority 0; policy drop; }
    chain output  { type filter hook output priority 0; policy accept; }
}
EOF
sudo nft -f /tmp/nftables.conf
sudo nft list ruleset
sudo systemctl enable nftables  # persist across reboots

← Network Diagnostics  |  🏠 Index  |  VPN & WireGuard →