For our Blog Visitor only Get Additional 3 Month Free + 10% OFF on TriAnnual Plan YSBLOG10
Grab the Deal

How to Monitor & Secure UFW on Linux Server – Easy Guide

To monitor and secure UFW on a Linux server, set a deny-by-default policy, explicitly allow required services (e.g., SSH), enable and tune logging, watch real-time events with journalctl or /var/log/ufw.log, apply rate limiting and per-IP rules, integrate Fail2ban for brute-force protection, and audit rules regularly with status and log reviews.

In this guide, you’ll learn how to monitor and secure UFW on a Linux server step-by-step. We’ll build a safe baseline, enable actionable logging, add hardening rules, integrate alerting, and troubleshoot with confidence. Whether you manage a single VPS or multiple nodes, these best practices work across Ubuntu and Debian-based distributions.

What Is UFW and Why It Matters

UFW (Uncomplicated Firewall) is a user-friendly interface for Linux’s netfilter firewall, using iptables or nftables under the hood depending on your distro.

What Is UFW and Why It Matters

It’s designed to make firewall management simple and predictable using human-readable commands, while still allowing granular controls like interfaces, IP ranges, and logging.

Before You Begin: Safety Checklist

  • Console access: Have cloud/VPS console or KVM/IPMI access in case a rule locks you out.
  • Know your SSH port: If using a non-default SSH port, note it before enabling UFW.
  • Confirm network details: Public interface name (e.g., eth0) and trusted IPs/ranges.
  • Backup: Snapshot the server or back up /etc/ufw.
  • Change window: Apply changes during a maintenance window if it’s a production system.

Install and Enable UFW

On Ubuntu and most Debian-based servers, UFW is available in the default repositories and often preinstalled. If not, install and enable it:

sudo apt update
sudo apt install ufw
sudo ufw status verbose

On first use, UFW is inactive. We’ll set a safe baseline and then enable it.

Build a Secure Baseline (Minimum Safe Rules)

Start with a deny-by-default posture and explicitly allow what you need. Ensure SSH is allowed before enabling UFW to avoid lockouts.

# 1) Enable IPv6 support if you use IPv6
sudo sed -i 's/^IPV6=.*/IPV6=yes/' /etc/default/ufw

# 2) Set default policies
sudo ufw default deny incoming
sudo ufw default allow outgoing

# 3) Allow SSH (22/tcp) or your custom port
sudo ufw allow ssh
# or, for a custom port (example: 2222)
# sudo ufw allow 2222/tcp

# 4) Enable UFW
sudo ufw enable

# 5) Verify
sudo ufw status verbose

If your server hosts web apps, allow HTTP/HTTPS:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Hardening UFW: Practical Security Enhancements

Rate Limit SSH and Critical Services

UFW’s “limit” reduces brute-force attempts by rate limiting new connections from the same IP. It’s ideal for SSH.

# Applies a simple rate limit on SSH
sudo ufw limit ssh comment 'Rate limit SSH to reduce brute force'

Allow Only from Trusted IPs (Admin and Database)

Restrict management ports (SSH, database admin, control panels) to specific IPs or ranges whenever possible.

# Allow SSH only from a trusted admin IP
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp comment 'Admin SSH'

# Allow MySQL/MariaDB only from app server subnet
sudo ufw allow from 10.10.0.0/24 to any port 3306 proto tcp comment 'App servers → DB'

Use Application Profiles

UFW ships with application profiles in /etc/ufw/applications.d that predefine ports and protocols.

sudo ufw app list
sudo ufw allow 'Apache Full'
# or
sudo ufw allow 'Nginx Full'

Control by Interface for Multi-NIC Hosts

Bind rules to an interface to avoid exposing services on unintended networks.

# Expose HTTP only on public interface
sudo ufw allow in on eth0 to any port 80 proto tcp

Tighten Outbound Traffic (Egress Filtering)

For hardened environments, restrict outbound traffic to reduce data exfiltration and malware callbacks. Start permissive, then narrow.

# Example: allow DNS and HTTP/S outbound only
sudo ufw default deny outgoing
sudo ufw allow out 53   # DNS
sudo ufw allow out 80/tcp
sudo ufw allow out 443/tcp

Logging: Levels and Best Practices

Enable logging at a level that provides visibility without flooding disks. Start with “low” or “medium,” increase temporarily for investigations.

# Enable logging (low|medium|high|full)
sudo ufw logging medium
sudo ufw status verbose | grep -i logging

Where UFW Logs Live and How to Monitor Them

Depending on your system, UFW logs are written to the system journal and/or /var/log/ufw.log via rsyslog. Use these commands to read and stream events:

# View UFW logs via journalctl (systemd)
sudo journalctl -u ufw -n 200 --no-pager

# Stream kernel firewall logs in real time
sudo journalctl -k -f | grep -i ufw

# If rsyslog writes a dedicated log
sudo tail -f /var/log/ufw.log

Look for lines containing UFW BLOCK or UFW ALLOW, source/destination IPs, ports, and interfaces. These are crucial for investigating rejected packets, scans, and misconfigurations.

Real-Time Monitoring and Alerting

Integrate Fail2ban with UFW

Fail2ban scans logs for suspicious patterns (e.g., failed SSH logins) and dynamically bans IPs using firewall actions. Use the UFW action to add bans as UFW rules.

sudo apt install fail2ban
# In /etc/fail2ban/jail.local (example)
[sshd]
enabled = true
port    = ssh
banaction = ufw
maxretry = 5
findtime = 600
bantime  = 3600

sudo systemctl restart fail2ban
sudo fail2ban-client status sshd

Use Log-Based Alerts

Ship UFW logs to a SIEM or logging stack (Elastic, Loki, Graylog) and alert on spikes in “UFW BLOCK” events, repeated hits to non-existent ports, or blocks against critical ports. For lightweight setups, a simple journalctl + mail script via cron works as a starter solution.

Rotate and Retain Logs

Ensure logs don’t fill disks. On many systems, /etc/logrotate.d/ufw handles rotation automatically. Verify retention fits your security policy (e.g., 30–90 days).

Auditing, Testing, and Troubleshooting

Inspect Rules and Order

UFW processes rules top-to-bottom. Use numbered output to review and delete rules safely.

# Show rules with numbers and details
sudo ufw status numbered verbose

# Delete a specific rule by number (example deletes rule #3)
sudo ufw delete 3

Dry Run and Reload Safely

Test changes before enforcing them and reload without dropping existing connections.

# Preview a rule without applying it
sudo ufw --dry-run allow 8080/tcp comment 'Preview rule'

# Apply changes and reload
sudo ufw allow 8080/tcp
sudo ufw reload

Diagnose Blocked Traffic

To confirm if UFW blocks a port, try connecting from a trusted remote host and watch logs in real time. Use curl, nc, or nmap for validation.

# From a remote machine
nc -vz your.server.ip 443
nmap -Pn -p 22,80,443 your.server.ip

# On the server, stream for UFW blocks while testing
sudo journalctl -k -f | grep -i ufw

Common Pitfalls

  • Forgetting IPv6: If IPV6 is disabled in UFW but your host has IPv6, services may bypass IPv4-only rules.
  • Cloud firewalls: Align UFW with your provider’s security groups to avoid conflicting rules.
  • Service not listening: A closed port may be due to the service not binding or listening only on localhost.
  • Rule order: A broad allow placed above a narrow deny can unintentionally expose services.

Ongoing Maintenance and Best Practices

  • Quarterly audits: Review ufw status verbose, prune unused rules, and rotate keys.
  • Change management: Use comments on every rule to document intent and ownership.
  • Staging first: Test new rules on a staging or replica server before production.
  • Baseline integrity: Keep a version-controlled copy of /etc/ufw and /etc/fail2ban.
  • Security scans: Periodically run nmap from outside to validate exposed ports.

UFW vs. Other Linux Firewalls (Quick Context)

UFW emphasizes simplicity and is ideal for single servers and small fleets. firewalld offers zone-based management common on RHEL-based systems. Native nftables provides maximum flexibility for advanced environments. Many teams choose UFW for straightforward, auditable rules with minimal overhead—especially on Ubuntu/Debian servers.

Production-Ready UFW Checklist

  • Default deny incoming; allow only necessary ports and sources.
  • Allow SSH from trusted IPs; apply ufw limit ssh.
  • Enable logging at medium; monitor via journalctl and/or /var/log/ufw.log.
  • Integrate Fail2ban with banaction = ufw for brute-force mitigation.
  • Review ufw status numbered monthly; remove stale rules.
  • Enable IPv6; ensure parity between IPv4 and IPv6 rules.
  • Rotate logs and forward to a central system for alerts.
  • Validate externally with nmap after every change window.

Example: Secure Web Server with Admin Access

# Defaults
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw logging medium

# Admin access (SSH) from office IP only
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp comment 'Office SSH'

# Web stack
sudo ufw allow 80/tcp comment 'HTTP'
sudo ufw allow 443/tcp comment 'HTTPS'

# Rate limit SSH
sudo ufw limit ssh comment 'Rate limit SSH'

# Enable and verify
sudo ufw enable
sudo ufw status numbered verbose

FAQs: Monitoring and Securing UFW on Linux

How do I check UFW logs and see what’s being blocked?

Enable logging with sudo ufw logging medium, then review events via sudo journalctl -k -f | grep -i ufw or tail -f /var/log/ufw.log if present. Look for “UFW BLOCK” lines to identify source IPs, destination ports, and interfaces.

Use sudo ufw default deny incoming and sudo ufw default allow outgoing, then explicitly allow required services (SSH, HTTP, HTTPS, database if needed). For high-security environments, restrict outgoing as well and permit only necessary destinations.

Does UFW support IPv6 and how do I enable it?

Yes. Set IPV6=yes in /etc/default/ufw, then sudo ufw reload. Ensure every IPv4 rule has a matching IPv6 rule to avoid unintentional exposure over IPv6.

How can I protect SSH with UFW from brute-force attacks?

Combine sudo ufw limit ssh with IP allowlists for admin networks. For advanced protection, deploy Fail2ban with banaction = ufw to dynamically ban abusive IPs based on failed login patterns.

What’s the difference between UFW and iptables/nftables?

UFW is a higher-level, user-friendly manager that writes rules to netfilter via iptables or nftables. It simplifies common tasks, provides readable status, and reduces human error. If you need extremely complex policies, nftables directly offers more flexibility but at the cost of complexity.

Prahlad Prajapati

Prahlad is a web hosting specialist and SEO-focused organic growth expert from India. Active in the digital space since 2019, he helps people grow their websites through clean, sustainable strategies. Passionate about learning and adapting fast, he believes small details create big success. Discover his insights on web hosting and SEO to elevate your online presence.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top