Skip to main content

How To Configure Firewall with UFW on Ubuntu 20.04 LTS

do I set up and configure firewall with UFW on Ubuntu 20.04 LTS server?

UFW is an acronym for an uncomplicated firewall. Securing a network with an uncomplicated firewall is super easy and highly recommended. This page explains how to set up and secure your Ubuntu 20.04 LTS server with ufw.

Step 1 – Set Up default UFW policies

To view status of ufw, type:
sudo ufw status
Sample outputs:
Status: inactive
The default policy firewall works out great for both the servers and desktop. It is always a good policy to closes all ports on the server and open only required ports one by one. Let us block all incoming connection and only allow outgoing connections from the Ubuntu 20.04 LTS box:
sudo ufw default allow outgoing
sudo ufw default deny incoming

Step 2 – Open SSH TCP port 22 connections

The next logical step is to allow incoming SSH ports. We can easily open SSH TCP port 22 using UFW as follows:
sudo ufw allow ssh
If you are running ssh on TCP port 2222, enter:
sudo ufw allow 2222/tcp
Some sysadmins have a static IP address (such as 202.54.2.5) at home or office location. In that case, only allow ssh access from the static IP address such as 202.54.2.5 to Ubuntu server IP address 172.24.13.45:
sudo ufw allow proto tcp from 202.54.2.5 to 172.24.13.45 port 22

Step 3 – Turn on firewall

Now we got basic configuration enabled. In other words, the firewall will drop all incoming traffic except for ssh TCP port 22. Let us true it on the UFW, enter:
sudo ufw enable

Remember, once UFW enabled, it runs across system reboots too. We can verify that easily as follows using the systemctl command:
sudo systemctl status ufw.service

Want to disable the UFW based firewall? Try

If you need to stop the firewall and disable on system startup, eenter:
sudo ufw disable
Sample outputs:
Firewall stopped and disabled on system startup

Step 4 – Open specific incoming connections/ports

Let us add more rules. Say you want to open ports and allow IP address with ufw. The syntax is as follows to open TCP port 80 and 443:
sudo ufw allow 80/tcp comment 'accept Apache'
sudo ufw allow 443/tcp comment 'accept HTTPS connections'

Open UDP/1194 (OpenVPN) server:
sudo ufw allow 1194/udp comment 'OpenVPN server'

Allow port ranges via ufw

We can allow port ranges too say, tcp and udp 3000 to 4000:
sudo ufw allow 3000:4000/tcp
sudo ufw allow 3000:4000/udp

In this example, you want to allow ALL connections from an IP address called 104.22.10.214, enter:
sudo ufw allow from 104.22.10.214
Let us allow connections from an IP address called 104.22.11.213 to our port 25, enter:
sudo ufw allow from 104.22.11.213 to any port 25 proto tcp
We can set dest IP 222.222.222.222 for port 25 too:
sudo ufw allow from 104.22.11.213 to 222.222.222.222 port 25 proto tcp

Step 5 – Block and deny incoming connections/ports

Do you want to close ports and block certain IP addresses? The syntax is as follows to deny access. In other words, simply ignoring access to port 25:
sudo ufw deny 25/tcp
Make sure we deny all connections from an IP address called 203.5.1.43, enter:
sudo ufw deny from 203.5.1.43
Deny all connections from an IP/subnet called 103.13.42.13/29, enter:
sudo ufw deny from 103.13.42.13/29
Want to deny access to 1.1.1.2 (say bad guys IP) on port 22? Try:
sudo ufw deny from 1.1.1.2 to any port 22 proto tcp

Step 6 – Verify status of UFW

Use the status command as follows:
sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                   # accept Apache
443/tcp                    ALLOW       Anywhere                   # accept HTTPS connections
1194/udp                   ALLOW       Anywhere                   # OpenVPN server
3000:4000/tcp              ALLOW       Anywhere                  
3000:4000/udp              ALLOW       Anywhere                  
Anywhere                   ALLOW       104.22.10.214             
25/tcp                     ALLOW       104.22.11.213             
222.222.222.222 25/tcp     ALLOW       104.22.11.213             
Anywhere                   DENY        203.5.1.43                
Anywhere                   DENY        103.13.42.8/29            
22/tcp                     DENY        1.1.1.2                   
22/tcp (v6)                ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)              # accept Apache
443/tcp (v6)               ALLOW       Anywhere (v6)              # accept HTTPS connections
1194/udp (v6)              ALLOW       Anywhere (v6)              # OpenVPN server
3000:4000/tcp (v6)         ALLOW       Anywhere (v6)             
3000:4000/udp (v6)         ALLOW       Anywhere (v6)             
Want verbose outputs? Try:
sudo ufw status verbose

Ubuntu 20.04 LTS UFW delete rules

So far we learned how to add, deny, and list the firewall rules. It is time to delete unwanted rules. The syntax is as follows to list all of the current rules in a numbered list format:
sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    Anywhere                  
[ 2] 80/tcp                     ALLOW IN    Anywhere                   # accept Apache
[ 3] 443/tcp                    ALLOW IN    Anywhere                   # accept HTTPS connections
[ 4] 1194/udp                   ALLOW IN    Anywhere                   # OpenVPN server
[ 5] 3000:4000/tcp              ALLOW IN    Anywhere                  
[ 6] 3000:4000/udp              ALLOW IN    Anywhere              
To delete 6th rule type the command:
sudo ufw delete 6
sudo ufw status numbered

Other command used to configure firewall with UFW

Let us learn a few more important commands.

Reset the ufw

sudo ufw reset

Reload the ufw

sudo ufw reload

View the firewall logs

By default all UFW entries are logged into /var/log/ufw.log file. Use the NA command/more command/tail command and other commands to view the ufw logs:
sudo more /var/log/ufw.log
sudo tail -f /var/log/ufw.log

Let us print a list of all IP address trying to log in via SSH port but dropped by the UFW:
grep 'DPT=22' /var/log/ufw.log |\
egrep -o 'SRC=([0-9]{1,3}[\.]){3}[0-9]{1,3}' |\
awk -F'=' '{ print $2 }' | sort -u

Show the list of rules

sudo ufw show added
sudo ufw show listening

Comments

Popular posts from this blog

4 Ways to Communicate Across Browser Tabs in Realtime

1. Local Storage Events You might have already used LocalStorage, which is accessible across Tabs within the same application origin. But do you know that it also supports events? You can use this feature to communicate across Browser Tabs, where other Tabs will receive the event once the storage is updated. For example, let’s say in one Tab, we execute the following JavaScript code. window.localStorage.setItem("loggedIn", "true"); The other Tabs which listen to the event will receive it, as shown below. window.addEventListener('storage', (event) => { if (event.storageArea != localStorage) return; if (event.key === 'loggedIn') { // Do something with event.newValue } }); 2. Broadcast Channel API The Broadcast Channel API allows communication between Tabs, Windows, Frames, Iframes, and  Web Workers . One Tab can create and post to a channel as follows. const channel = new BroadcastChannel('app-data'); channel.postMessage(data); And oth...

Certbot SSL configuration in ubuntu

  Introduction Let’s Encrypt is a Certificate Authority (CA) that provides an easy way to obtain and install free  TLS/SSL certificates , thereby enabling encrypted HTTPS on web servers. It simplifies the process by providing a software client, Certbot, that attempts to automate most (if not all) of the required steps. Currently, the entire process of obtaining and installing a certificate is fully automated on both Apache and Nginx. In this tutorial, you will use Certbot to obtain a free SSL certificate for Apache on Ubuntu 18.04 and set up your certificate to renew automatically. This tutorial will use a separate Apache virtual host file instead of the default configuration file.  We recommend  creating new Apache virtual host files for each domain because it helps to avoid common mistakes and maintains the default files as a fallback configuration. Prerequisites To follow this tutorial, you will need: One Ubuntu 18.04 server set up by following this  initial ...

Working with Node.js streams

  Introduction Streams are one of the major features that most Node.js applications rely on, especially when handling HTTP requests, reading/writing files, and making socket communications. Streams are very predictable since we can always expect data, error, and end events when using streams. This article will teach Node developers how to use streams to efficiently handle large amounts of data. This is a typical real-world challenge faced by Node developers when they have to deal with a large data source, and it may not be feasible to process this data all at once. This article will cover the following topics: Types of streams When to adopt Node.js streams Batching Composing streams in Node.js Transforming data with transform streams Piping streams Error handling Node.js streams Types of streams The following are four main types of streams in Node.js: Readable streams: The readable stream is responsible for reading data from a source file Writable streams: The writable stream is re...