Implementation of UFW Firewall Commands

By Anurag Singh

Updated on Jul 29, 2024

Implementation of UFW Firewall Commands

In this tutorial, we'll explain the implementation of UFW firewall commands.

Starting with the basics, we guide you through the installation and initial configuration of UFW, ensuring your system is secure by default. We then delve into more advanced commands and features, allowing you to fine-tune your firewall settings for specific needs, such as managing port ranges, IP addresses, and network interfaces. 

You'll also learn how to enable logging, create custom application profiles, and implement rate limiting to protect against brute-force attacks. By the end of this tutorial, you'll have a robust firewall configuration tailored to your server's requirements, enhancing your overall system security.

Update Your System

Before setting up UFW, ensure your system is up-to-date.

sudo apt update && sudo apt upgrade -y

Install UFW

UFW is typically installed by default on Ubuntu, but you can install it if it's not already present.

sudo apt install ufw

Basic UFW Commands

Check UFW Status

Check the status of UFW to see if it's active.

sudo ufw status

If UFW is inactive, you'll see:

Status: inactive

Enable UFW

Enable UFW to start using it.

sudo ufw enable

Default Policies

It's important to set default policies before configuring specific rules. By default, UFW denies incoming connections and allows outgoing connections.

sudo ufw default deny incoming
sudo ufw default allow outgoing

Allow SSH Connections

To prevent locking yourself out of your server, allow SSH connections.

sudo ufw allow ssh

If your SSH server uses a different port (e.g., port 2222), use:

sudo ufw allow 2222/tcp

Allow Specific Ports

Allow HTTP (port 80) and HTTPS (port 443) traffic if you run a web server:

sudo ufw allow http
sudo ufw allow https

Allow Specific IP Address

Allow traffic from a specific IP address, for example, to allow traffic from 192.168.1.100:

udo ufw allow from 192.168.1.100

Deny Specific IP Address

Deny traffic from a specific IP address:

sudo ufw deny from 192.168.1.200

Enabling UFW Logging

Enable logging to monitor UFW activities. This helps in troubleshooting and ensures that you can review what traffic is being allowed or blocked.

sudo ufw logging on

Reload UFW to Apply Changes

Reload UFW to apply any changes:

sudo ufw reload

Disable UFW (If Needed)

If you need to disable UFW temporarily, you can do so with:

sudo ufw disable

Advanced UFW Commands:

Checking UFW Status with Detailed Output

To get a more detailed output of the UFW status, including all configured rules:

sudo ufw status verbose

Allowing/Denying Specific Port Ranges

To allow or deny a range of ports, use the following commands:

sudo ufw allow 1000:2000/tcp
sudo ufw deny 3000:4000/udp

Allowing/Denying Specific IP Ranges

To allow or deny traffic from a specific IP range:

sudo ufw allow from 192.168.1.0/24
sudo ufw deny from 10.0.0.0/8

Allowing/Denying Traffic to a Specific Network Interface

If your server has multiple network interfaces, you can allow or deny traffic on a specific interface:

sudo ufw allow in on eth0 to any port 22
sudo ufw deny in on eth1 to any port 80

Allowing/Denying Traffic to a Specific IP and Port

To allow or deny traffic from a specific IP address to a specific port:

sudo ufw allow from 192.168.1.100 to any port 443
sudo ufw deny from 10.0.0.200 to any port 25

Rate Limiting Connections

To prevent brute-force attacks, you can rate limit connections. This allows a maximum of six connections within 30 seconds:

sudo ufw limit ssh

Creating Application Profiles

UFW allows you to create application profiles to manage complex rule sets easily. Application profiles are stored in /etc/ufw/applications.d/. Here's how to create a custom application profile:

Create a new file in the /etc/ufw/applications.d/ directory:

sudo nano /etc/ufw/applications.d/myservice

Add the following content to define your service:

[MyService]
title=My Custom Service
description=Allow My Custom Service traffic
ports=1234/tcp

Save the file and exit the editor.

Now, you can allow traffic for this custom service:

sudo ufw allow MyService

Enabling IPv6 Support

If your server uses IPv6, ensure that UFW is configured to support it. Open the UFW configuration file:

sudo nano /etc/default/ufw

Find the line IPV6=yes and ensure it is uncommented:

IPV6=yes

Save the file and restart UFW to apply the changes:

sudo ufw disable
sudo ufw enable

Resetting UFW

If you need to reset UFW to its default state, removing all rules:

sudo ufw reset

This will disable UFW and remove all existing rules. You will need to re-enable UFW and reconfigure your rules.

Managing UFW Profiles

UFW allows you to manage profiles, which can simplify the process of applying complex sets of rules. To create and use profiles, follow these steps:

Create a new profile file in /etc/ufw/applications.d/:

sudo nano /etc/ufw/applications.d/myprofile

Add your custom rules in the following format:

[MyProfile]
title=My Custom Profile
description=Description of my custom profile
ports=80,443/tcp|1000:2000/tcp|3000:4000/udp

Save and close the file. Now, you can enable the profile:

sudo ufw allow MyProfile

Conclusion

These advanced UFW commands and features allow you to fine-tune your firewall rules to meet specific security requirements. Regularly reviewing and updating your firewall configuration can help maintain the security and performance of your server.