Level Verified

Linux Monitor - Firewall Script

Checks for active firewall configurations (UFW, iptables, nftables) in Linux environments. Alerts if no firewall is detected, ensuring critical security measures remain in place. Perfect for script-based monitoring and automated remediation in Level.

Import into Level

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.

Bash 100s timeout Runs as Local system Linux
#!/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

Frequently asked questions.

How do I run this script in Level?

Import it into Level and assign it to a script-based monitor or an Automation schedule. The script runs with System or Root privileges, ensuring accurate checks.

Will this script enable a disabled firewall?

No. It only checks the status of UFW, iptables, or nftables. Enabling firewall rules must be handled manually or with a separate remediation script.

What if my distribution doesn’t use any of these firewalls?

The script will report no active firewall, prompting you to install or configure a suitable firewall tool.

Can multiple firewalls run simultaneously?

Typically, it’s best to stick to one primary firewall solution to avoid rule conflicts, though some configurations may allow multiple solutions if managed carefully.

Does this script replace the need for manual firewall checks?

It automates routine checks, but you should still review firewall rules and configurations periodically to maintain best security practices.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.