Problem overview
Firewalls are crucial for blocking unauthorized access and potential attacks, but they can be deactivated or misconfigured without notice. This script helps IT Professionals and MSPs verify that at least one firewall solution is actively configured, reducing the risk of security gaps going unnoticed.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-linux-monitor-firewall
#!/bin/bash
# Initialize a flag to track if any firewall is active or configured
FIREWALL_ACTIVE=0
# Function to check UFW status
check_ufw() {
if command -v ufw >/dev/null 2>&1; then
echo "Checking UFW..."
STATUS=$(ufw status)
if [[ "$STATUS" == *"Status: active"* ]]; then
echo "UFW is ENABLED."
FIREWALL_ACTIVE=1
else
echo "UFW is DISABLED or not configured."
fi
fi
}
# Function to check iptables status
check_iptables() {
if command -v iptables >/dev/null 2>&1; then
echo "Checking iptables..."
RULES=$(iptables -L | wc -l)
if [ "$RULES" -gt 8 ]; then # Assuming base rule count is 8 for the default chains
echo "iptables has rules set (may be ENABLED)."
FIREWALL_ACTIVE=1
else
echo "iptables does not have many rules set (may be DISABLED or not configured)."
fi
fi
}
# Function to check nftables status
check_nftables() {
if command -v nft >/dev/null 2>&1; then
echo "Checking nftables..."
TABLES=$(nft list tables | wc -l)
if [ "$TABLES" -gt 0 ]; then
echo "nftables has tables configured (may be ENABLED)."
FIREWALL_ACTIVE=1
else
echo "nftables does not have any tables configured (may be DISABLED or not configured)."
fi
fi
}
# Run the firewall checks
echo "Checking firewall status..."
check_ufw
check_iptables
check_nftables
# If no firewall is active or configured, report an error and exit with 1
if [ $FIREWALL_ACTIVE -eq 0 ]; then
echo "ERROR: No active or configured firewall detected."
exit 1
else
echo "SUCCESS: An active or configured firewall is detected."
exit 0
fi
The script systematically checks the status of three common Linux firewall solutions—UFW, iptables, and nftables—to confirm whether any of them are enabled and have rules in place. If it finds no active configurations, it flags the issue by returning an error code, allowing you to take timely corrective action.
By integrating this script into a script-based monitor in Level, you can immediately detect disabled or missing firewalls. You can also schedule it through an Automation in Level to run at regular intervals, guaranteeing ongoing oversight of your system’s security posture.
Use cases
- Monitoring firewall status across multiple Linux servers
- Verifying that at least one firewall solution remains active after system updates
- Ensuring consistent security standards in regulated environments
- Proactively checking for accidental or malicious firewall changes
Recommendations
- Test in a controlled environment before deploying broadly
- Use a script-based monitor in Level to trigger alerts for any missing firewall
- Set up a recurring schedule via an Automation in Level for ongoing verification
- Double-check your firewall rules and ensure you have only one primary firewall solution or that they coexist properly
- Review script output frequently to catch “ERROR” messages early