How to Setup UFW Firewall on Ubuntu and Debian

A correctly functioning firewall is the most crucial part of the complete Linux system security. By default, Debian and Ubuntu distribution comes with a firewall configuration tool called UFW (Uncomplicated Firewall), is a most...

A correctly functioning firewall is the most crucial part of the complete Linux system security. By default, Debian and Ubuntu distribution comes with a firewall configuration tool called UFW (Uncomplicated Firewall), is a most popular and easy-to-use command line tool for configuring and managing a firewall on Ubuntu and Debian distributions.

In this article, we will explain how to install and setup a UFW firewall on Ubuntu and Debian distributions.

Prerequisites

Before you begin with this article, make sure you have logged into your Ubuntu or Debian server with sudo user or with the root account. If you don’t have a sudo user, you can create one using the following instructions as root user.

Install UFW Firewall on Ubuntu and Debian

The UFW (Uncomplicated Firewall) should be installed by default in Ubuntu and Debian, if not, install it using the APT package manager using following command.

$ sudo apt install ufw

Check UFW Firewall

Once the installation is completed you can check the status of UFW by typing.

$ sudo ufw status verbose

On first install, the UFW firewall is disabled by default, the output will similar to below.

Status: inactive

Enable UFW Firewall

You can activate or enable UFW firewall using the following command, which should load the firewall and enables it to start on boot.

$ sudo ufw enable

To disable UFW firewall, use the following command, which unloads the firewall and disables it from starting on boot.

$ sudo ufw disable

UFW Default Policies

By default, the UFW firewall denies every incoming connections and only allow all outbound connections to server. This means, no one can access your server, unless you specifically open the port, while all the running services or applications on your server can be able to access the outside network.

The default UFW firewall polices are placed in the /etc/default/ufw file and can be altered using the following command.

$ sudo ufw default deny incoming
$ sudo ufw default allow outgoing

UFW Application Profiles

When installing a software package using APT package manager, it will include an application profile in /etc/ufw/applications.d directory that defines the service and hold UFW settings.

You can list all available application profiles on your server using the following command.

$ sudo ufw app list

Depending upon software package installations on your system the output will look similar to the following:

Enable Specific Ports on UFW

You can also open a specific port in the firewall to allow connections via it to a certain service. For example, if you want to setup a web server which listens on port 80 (HTTP) and 443 (HTTPS) by default.

Below are the few examples of how to allow incoming connections to Apache services.

Open Port 443 HTTPS on UFW

Allow Port Ranges on UFW

Assuming you have some applications that you want to run on a range of ports (5000-5003), you can add all these ports using following commands.

sudo ufw allow 5000:5003/tcp
sudo ufw allow 5000:5003/udp

Allow Specific IP Addresses

If you want to allow connections on all ports from specific IP address 192.168.56.1, then you need to specify from before the IP address.

$ sudo ufw allow from 192.168.56.1

Allow Specific IP Addresses on Specific Port

To allow connection on a specific port (for example port 22) from your home machine with IP address of 192.168.56.1, then you need to add any port and the port number after the IP address as shown.

$ sudo ufw allow from 192.168.56.1 to any port 22

Allow Network Subnets to Specific Port

To allow connections for particular IP addresses ranging from 192.168.1.1 to 192.168.1.254 to port 22 (SSH), run the following command.

$ sudo ufw allow from 192.168.1.0/24 to any port 22

Allow Specific Network Interface

To allow connections to specific network interface eth2 for a particular port 22 (SSH), run the following command.

$ sudo ufw allow in on eth2 to any port 22

Deny Connections on UFW

By default, all incoming connections are blocked, unless you have specifically open the connection on UFW. For example, you have opened the ports 80 and 443 and your web server is under attack from the unknown network 11.12.13.0/24.

To block all connections from this particular 11.12.13.0/24 network range, you can use the following command.

$ sudo ufw deny from 11.12.13.0/24

If you only want to block connections on ports 80 and 443, you can use the following commands.

$ sudo ufw deny from 11.12.13.0/24 to any port 80
$ sudo ufw deny from 11.12.13.0/24 to any port 443

Delete UFW Rules

There are 2 ways to delete UFW rules, by rule number and by actual rule.

To delete a UFW rules by using rule number, first you need to list rules by numbers using the following command.