UFW Essentials: Common Firewall Rules and Commands

Introductio for UFW Essentials

UFW (uncomplicated firewall) is a firewall configuration tool that runs on top of iptables, included by default within Ubuntu distributions. It provides a streamlined interface for configuring common firewall use cases via the command line.

This cheat sheet-style guide provides a quick reference to common UFW use cases and commands, including examples of how to allow and block services by port, network interface, and source IP address.

How To Use This Guide for UFW Essentials

  • This guide is in cheat sheet format with self-contained command-line snippets.
  • Jump to any section that is relevant to the task you are trying to complete.
  • Highlighted text in commands should refer to IP addresses from your network.
  • Check your current UFW ruleset with sudo ufw status or sudo ufw status verbose.

Verify UFW Status

To check if UFW is enabled, run:

Output:

Enable UFW

If the firewall is inactive, enable it using:

Output:

Firewall is active and enabled on system startup

Use sudo ufw status verbose to see what is blocked or allowed:

Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), deny (routed)
New profiles: skip

Disable UFW

To disable the firewall service, run:

Note: This will fully disable the firewall service on your system.

UFW Essentials: Block an IP Address

To block all network connections from a specific IP address:

sudo ufw deny from 203.0.113.100

Output:

Allow an IP Address

To allow all network connections from a specific IP address:

sudo ufw allow from 203.0.113.101

Output:

Delete UFW Rule

To delete a rule, use:

sudo ufw delete allow from 203.0.113.101

Alternatively, use rule ID:

Allow HTTP and HTTPS Traffic

To allow both HTTP and HTTPS traffic:

sudo ufw allow proto tcp from any to any port 80,443

Output:

Conclusion for UFW Essentials

UFW is a powerful tool that enhances server security when configured correctly. It simplifies managing firewall rules efficiently. Use this guide as a quick reference for common firewall rules. With UFW, you can easily allow or block specific ports and IP addresses. Customize your server’s traffic to meet unique needs securely. Proper configuration ensures reliable protection against unauthorized access attempts.