Back to Resources
Level
Script
Security
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.
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.
1#!/bin/bash
2
3# This resource is provided as a convenience for Level users. We cannot
4# guarantee it will work in all environments. Please test before deploying
5# to your production environment. We welcome contributions to our community
6# library
7
8# Level Library
9# https://level.io/library/script-linux-monitor-firewall
10
11#!/bin/bash
12
13# Initialize a flag to track if any firewall is active or configured
14FIREWALL_ACTIVE=0
15
16# Function to check UFW status
17check_ufw() {
18 if command -v ufw >/dev/null 2>&1; then
19 echo "Checking UFW..."
20 STATUS=$(ufw status)
21 if [[ "$STATUS" == *"Status: active"* ]]; then
22 echo "UFW is ENABLED."
23 FIREWALL_ACTIVE=1
24 else
25 echo "UFW is DISABLED or not configured."
26 fi
27 fi
28}
29
30# Function to check iptables status
31check_iptables() {
32 if command -v iptables >/dev/null 2>&1; then
33 echo "Checking iptables..."
34 RULES=$(iptables -L | wc -l)
35 if [ "$RULES" -gt 8 ]; then # Assuming base rule count is 8 for the default chains
36 echo "iptables has rules set (may be ENABLED)."
37 FIREWALL_ACTIVE=1
38 else
39 echo "iptables does not have many rules set (may be DISABLED or not configured)."
40 fi
41 fi
42}
43
44# Function to check nftables status
45check_nftables() {
46 if command -v nft >/dev/null 2>&1; then
47 echo "Checking nftables..."
48 TABLES=$(nft list tables | wc -l)
49 if [ "$TABLES" -gt 0 ]; then
50 echo "nftables has tables configured (may be ENABLED)."
51 FIREWALL_ACTIVE=1
52 else
53 echo "nftables does not have any tables configured (may be DISABLED or not configured)."
54 fi
55 fi
56}
57
58# Run the firewall checks
59echo "Checking firewall status..."
60check_ufw
61check_iptables
62check_nftables
63
64# If no firewall is active or configured, report an error and exit with 1
65if [ $FIREWALL_ACTIVE -eq 0 ]; then
66 echo "ERROR: No active or configured firewall detected."
67 exit 1
68else
69 echo "SUCCESS: An active or configured firewall is detected."
70 exit 0
71fi
72
Linux Monitor - Firewall
This script checks if UFW (Uncomplicated Firewall) is installed and verifies its status, alerting if the firewall is inactive or disabled to ensure proper system security.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.