Secure Your Linux System with UFW: A Guide to Uncomplicated Firewall

Richard Gray
2 min readApr 7, 2023

--

UFW (Uncomplicated Firewall) is a powerful and easy-to-use firewall tool for Linux. It provides a simple and intuitive interface for configuring firewall rules, making it easy for anyone to manage their firewall settings. In this blog post, we’ll cover everything you need to know about UFW, from installation to advanced features.

Installation

UFW is included in many Linux distributions and can be installed using the package manager. To install UFW on Debian-based systems, use the following command:

sudo apt-get install ufw

To install UFW on Red Hat-based systems, use the following command:

sudo yum install ufw

Basic Usage

After installing UFW, you can start using it to manage your firewall rules. The first step is to enable the firewall:

sudo ufw enable

By default, UFW denies all incoming traffic and allows all outgoing traffic. To allow incoming traffic, you need to configure firewall rules.

Configuring Firewall Rules

Firewall rules determine which traffic is allowed and which is blocked. UFW uses a simple syntax to define firewall rules. The basic syntax is:

sudo ufw [allow|deny] [proto] [from] [to] [port]

Here’s what each of the options means:

  • allow or deny: Specifies whether to allow or deny traffic
  • proto: Specifies the protocol (e.g. tcp or udp)
  • from: Specifies the source IP address or subnet (e.g. 192.168.1.0/24)
  • to: Specifies the destination IP address or subnet
  • port: Specifies the port number or port range (e.g. 22 or 22:24)

Here are some examples of firewall rules:

  • sudo ufw allow 22/tcp: Allows incoming SSH traffic
  • sudo ufw allow from 192.168.1.0/24 to any port 80: Allows incoming HTTP traffic from the 192.168.1.0/24 subnet
  • sudo ufw deny 3306/tcp: Blocks incoming MySQL traffic

You can also use service names instead of port numbers. For example, sudo ufw allow ssh allows incoming SSH traffic.

Advanced Usage

UFW also supports advanced firewall rules and options, such as:

  • Logging: You can enable logging for UFW to log firewall activity. Use the following command to enable logging:
sudo ufw logging on
  • Application Profiles: UFW includes application profiles for common services, such as Apache, MySQL, and PostgreSQL. These profiles allow you to quickly and easily configure firewall rules for these services. Use the following command to list available application profiles:
sudo ufw app list
  • Custom Chains: UFW allows you to create custom chains to define more complex firewall rules. Use the following command to create a custom chain:
sudo ufw route [route-name] [route-number] [route-action]

Conclusion

UFW is a powerful and easy-to-use firewall tool for Linux. By following these steps, you can install UFW, configure firewall rules, and use advanced features like logging and custom chains. With UFW, you can protect your system from unauthorized access and keep your data safe and secure.

--

--