check open ports using ufw firewall

How to Check Open Ports in UFW

UFW (Uncomplicated Firewall) is a popular firewall system in Linux that allows you to easily manage inbound and outbound traffic using firewall rules. It comes pre-installed in many Linux distributions and is very easy to use. Sometimes you may need to check open ports in UFW to understand from which ports your system is receiving traffic. In this article, we will look at how to check open ports in UFW.


How to Check Open Ports in UFW

In this article, we will look at how to list open ports in UFW. We will also learn how to open and close ports in UFW.


1. List Open Ports in UFW

Open terminal and run the following command to get a list of all firewall rules in UFW, along with the ports they open.

$ sudo ufw status

If you want additional information such as user profile & logging then run the following command.

$ sudo ufw status verbose

The following command will list all firewall rules along with rule number.

$ sudo ufw status numbered

If you want to delete a rule using their serial number, just run the following command specifying rule number (e.g. 7).

$ sudo ufw delete 7


2. Open Port using UFW

There are several ways to open ports in UFW – using port number, protocol, ip address, service name.

Here is an example to open port 8080 in UFW.

$ sudo ufw allow 8080

Here is an example to open HTTPS port.

$ sudo ufw allow https

Here is how to allow traffic from IP 54.43.32.21 to all ports.

$ sudo ufw allow from 54.43.32.21

Here is how to allow traffic from IP 54.43.32.21 to port 22.

$ sudo ufw allow from 54.43.32.21 to any port 22

Here is how to allow traffic for port used by a specific service e.g. nginx

$ sudo ufw allow nginx
OR
$ sudo ufw allow nginx/tcp

After you open port in UFW run the following command to apply changes, if they have not been applied already.

$ sudo ufw enable


3. Close Port Using UFW

There are several ways to close ports in UFW – using port number, protocol, ip address, service name.

$ sudo ufw deny http # block http port 80
$ sudo ufw deny https # block https port 443
$ sudo ufw deny ssh # block ssh port 22
$ sudo ufw deny 3939/tcp # block custom port 3939
$ sudo ufw deny from 34.43.21.12 # block access from 34.43.21.12
$ sudo ufw deny from 34.43.21.0/24 # block access from 34.43.21.1-34.43.21.254
$ sudo ufw deny nginx # deny access to port used by nginx service

That’s it. In this article, we have learnt different ways to find out open ports in UFW, how to open & close ports.

UFW is a very useful firewall for Linux users that provides a utility to manage netfilter, and also a command line interface to manage firewall inbound and outbound rules. Also as you can see above, it is very easy to use. It is smart enough to understand whether you are allowing/denying a port, protocol, or service using the same command but different argument. Also, it is directly implemented upon iptables which is the main firewall for every Linux distribution. So it allows you to easily interface with its rules using command line.

Also read:

How to Debug Shell Script
How to Sort Text File in Python
How to Check UFW Log & Status
How to Create Symbolic Links in Linux
Ubuntu Change Terminal Font Size & Color

Leave a Reply

Your email address will not be published. Required fields are marked *