How to Allow Ports Through UFW Firewall in Ubuntu?

Ubuntu comes with a powerful and easy-to-use firewall application called Uncomplicated Firewall (UFW). The UFW utility allows or denies network traffic on specific ports. Allowing ports through the firewall can help secure your system by only allowing network traffic on specific ports. This can help prevent unauthorized access or attacks from malicious users attempting to exploit open ports on your system.

This article will discuss in detail how a user can allow ports through a firewall using port numbers. 

  • Allow Port Through Port Number
  • Allow Port Through Service Name
  • Allow Ports in Ranges
  • Allow Ports to a Specific IP Address
  • Allow Ports through Specific Network Interface

How to Allow Port Through Port Number?

The below command can be executed to add a new rule in UFW that allows incoming traffic on port 22. This means that any incoming traffic that is attempting to connect to your system on port 22 will be allowed, such as an SSH connection from a remote system:

$ sudo ufw allow 22

How to Allow Port Through Service Name? 

A user can also execute the below command to allow SSH connection as shown below:

$ sudo ufw allow ssh

You can verify if these rules were added in the UFW or not by typing the below command:

$ sudo ufw status

Allowing HTTP Port

Running any of the below commands will add a new rule in UFW that allows incoming traffic on port 80 for HTTP. This means that any incoming traffic that is attempting to connect to your system on port 80 will be allowed, such as when you’re browsing the internet or accessing a web application:

$ sudo ufw allow http
$ sudo ufw allow 80

Allowing HTTPS Port

Running any of the below commands will add a new rule in UFW that allows incoming traffic on port 443 for HTTPS. This means that any incoming traffic that is attempting to connect to your system on port 443 will be allowed, such as when you’re accessing a website that uses HTTPS:

$ sudo ufw allow https
$ sudo ufw allow 443

How to Allow Ports in Ranges?

The below commands will add a rule to the UFW firewall configuration after execution to allow incoming traffic on the specified range of TCP and UDP ports. Any incoming traffic on ports 10000 through 12000 will be allowed to pass through the firewall:

$ sudo ufw allow 10000:12000/tcp
$ sudo ufw allow 10000:12000/udp

You can also run the below command to allow these ports on TCP and UDP connections:

$ sudo ufw allow 10000:12000/tcp udp

How to Allow Ports to a Specific IP Address?

The below command can be used to allow the traffic from IP address 192.168.1.100 to connect to port 22:

$ sudo ufw allow from 192.168.1.100 to any port 22

The below command allows traffic from any IP address in the range of “192.168.1.1” to “192.168.1.254” to connect to port 80 on your system:

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

How to Allow Ports through Specific Network Interface?

A user can also allow ports through their network interface as well which can be found by executing the below command:

$ ip addr

In this case, the “ens33” is the network interface, and a user can allow ports through the UFW firewall by executing the below command:

$ sudo ufw allow in on ens33 to any port 80

Conclusion

Allowing ports through the UFW firewall in Ubuntu is a necessary step for enhancing network security and enabling access to specific services. There are various methods to allow ports through the UFW firewall in Ubuntu which have been discussed in detail in this article. These methods allow port Through Port Number, Service Name, Ports in Ranges, Specific IP Address, and Specific Network Interface.