How to Set Up a Firewall with UFW

1. Introduction

The firewall configuration tool that ships with Ubuntu is called UFW (Uncomplicated Firewall). UFW is a user-friendly front end for managing iptables firewall rules. Its main purpose is to make managing iptables easier. This guide uses UFW to configure and manage a firewall on Ubuntu 20.04.

2. Installing UFW

UFW usually comes preinstalled on Ubuntu 20.04. If for some reason it is not installed, you can install it with the following commands:

sudo apt update
sudo apt install ufw

3. Checking the UFW Status

The installation does not activate the firewall automatically, so that you don't get locked out of your server. You can check the UFW status by running:

sudo ufw status verbose

The output looks like this:

Status: inactive

If UFW is active, the output will look similar to this:

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

To                         Action      From
--                         ------      ----      
22                         ALLOW IN    Anywhere        
22 (v6)                    ALLOW IN    Anywhere (v6)

4. UFW Default Policies

By default, UFW blocks all incoming connections and allows all outgoing connections. This means nobody can reach your server unless you open a port, while the applications and services running on the server can still reach the outside world.

The default policies are defined in the /etc/default/ufw file and can be changed with the sudo ufw default <policy> <chain> command.

Firewall policies are the foundation for building more detailed, user-defined rules.

5. Application Profiles

Most applications ship with an application profile that describes the service and contains UFW settings. These profiles are created automatically in the /etc/ufw/applications.d directory when the package is installed.

To list all application profiles on your system, run:

sudo ufw app list

Depending on the packages installed on your system, the output should look something like this:

Available applications:
  OpenSSH

To find more information about a specific profile and the rules it contains, use the following command:

sudo ufw app info 'OpenSSH'

The output shows that the "OpenSSH" profile opens port "22".

Profile: OpenSSH
Title: Secure shell server, an rshd replacement
Description: OpenSSH is a free implementation of the Secure Shell protocol.

Port:
  22/tcp

You can also create custom profiles for your own applications.

6. Enabling UFW

If you are connecting to your Ubuntu machine remotely, you must allow SSH connections before enabling the UFW firewall. Otherwise, you will no longer be able to connect to the machine.

To configure the UFW firewall to allow remote SSH connections, run:

sudo ufw allow ssh

Output:

Rules updated
Rules updated (v6)

If SSH runs on a non-standard port, you need to open that port.

For example, if your SSH daemon listens on 8022, run the following command to allow connections on that port:

sudo ufw allow 7722/tcp

Now that the firewall is configured to allow incoming SSH connections, you can enable UFW with:

sudo ufw enable

The output looks like this:

#你将会被警告启用防火墙可能会中断现有的 SSH 连接,输入"y”,并且回车。
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

7. Opening Ports

Depending on the applications running on your server, you will need to open different ports for different services.

The general syntax for opening a port is:

ufw allow port_number/protocol

Here are a few ways to allow HTTP connections.

The first option is to use the service name. UFW checks the /etc/services file, which specifies the port and protocol for each service:

sudo ufw allow http

You can also specify the port number and protocol:

sudo ufw allow 80/tcp

When no protocol is given, UFW creates rules for both tcp and udp.

Another option is to use an application profile, in this case "OpenSSH":

sudo ufw allow 'OpenSSH'

UFW also supports another syntax that uses the proto keyword to specify the protocol.

  1. Port ranges

UFW lets you open a range of ports. Separate the start and end ports with a colon, and when you specify the protocol, it must be either tcp or udp.

For example, to allow ports 7100 through 7200 on both tcp and udp, run the following commands:

sudo ufw allow 7100:7200/tcp
sudo ufw allow 7100:7200/udp
  1. Allowing a specific IP address to access a specific port

To allow all connections on all ports from a specific source IP, use the from keyword followed by the source address.

Here is an example of whitelisting an IP address:

sudo ufw allow from 1.1.1.1

If you only want to allow a given IP to access a specific port, use the to any port keywords followed by the port number.

For example, to allow the machine with IP 1.1.1.1 to connect on port 22, run:

sudo ufw allow from 1.1.1.1 to any port 22
  1. Allowing subnets

The command for allowing access from a subnet is the same as for a single IP address. The only difference is that you need to specify the netmask.

Here is an example showing how to allow IP addresses (192.168.1.1 to 192.168.1.254) to access 3360 (MySQL); you can use this command:

sudo ufw allow from 192.168.1.0/24 to any port 3306
  1. Allowing connections on a specific network interface

To allow connections through a specific network interface, use allow in on followed by the name of the network interface:

sudo ufw allow in on eth0 to any port 3306

8. Denying Connections

The default policy for all incoming connections is set to deny. If you haven't changed it, UFW blocks all incoming connections unless you explicitly open them.

Writing deny rules is the same as writing allow rules; all you need to do is replace the allow keyword with deny.

Say you have opened ports 80 and 443, and your server is under attack from the 2.2.2.0/24 network. To deny all connections from 2.2.2.0/24, use the following command:

sudo ufw deny from 2.2.2.0/24

To deny access to ports 80 and 443 only from 2.2.2.0/24, you can use the following command:

sudo ufw deny proto tcp from 23.24.25.0/24 to any port 80,443

9. Deleting UFW Rules

There are two different ways to delete UFW rules: by rule number and by specifying the rule itself.

To delete by rule number, you first need to find the number of the rule you want to delete. Run the following command:

sudo ufw status numbered

Output:

Status: active

     To                         Action      From
     --                         ------      ----         
[ 1] 22                         ALLOW IN    Anywhere         
[ 2] 22 (v6)                    ALLOW IN    Anywhere (v6)

To delete rule number 2, which allows connections to port 22 over IPv6, you can use the following command:

sudo ufw delete 2

The second way to delete a rule is to specify the actual rule. For example, if you added a rule to open port 80, you can delete it with the following command:

sudo ufw delete allow 80

10. Reloading UFW

After changing UFW rules, you need to reload UFW with the following command:

sudo ufw reload

11. Disabling UFW

If for any reason you need to stop UFW and deactivate all rules, you can run:

sudo ufw disable

Later, if you want to re-enable UFW and activate all rules, run:

sudo ufw enable

12. Resetting UFW

Resetting UFW disables it and deletes all active rules. This is useful if you want to revert all applied rules and start over.

To reset UFW, simply run the following command:

sudo ufw reset

3. FAQ

Question 1: Running a command shows the error ERROR: You need to be root to run this script.

Answer 1: You need to run the command with an account that has administrator privileges.