How Will You Be Able To Locate And Close An Open Port In Linux?

open port linix

On machines that run on Linux, the task of locating and closing an open port is actually quite simple. Besides that, this work is something that each and every network admin should know to do.

If you find out that there are some odd traffic bouncing about the network and it can provoke your curiosity level.

This might happen because the traffic might be using an open port on a machine. In any such cases, you need to find out the port and close it.

This article here will actually let you understand the process of locating any open port and close the port in Linus.

So, without further ado, let us get head straight into the process that will let you know how to close an open port.

What are the prerequisites here?

For doing this task you need to have some stuff stacked up in advance. You will require a running instance of Ubuntu Server and a user account with sudo privileges.

How can you locate a listening port?

Let us get into the process of locating a listening port. Here, luckily, you will not be needing any external application software for making this task happen.

Here is the reason for that – it is because the use of the ss command will be made in order to view the listening ports on your server.

This will be operated from the command line. Therefore, you will either be logging into your server or utilize a safe shell in order to gain access.

The moment you are at the bash prompt, write the following command mentioned below.

sudo ss -tulwn | grep LISTEN

Here are the options listed below:

  1. -t Show only TCP sockets on Linux
  2. -u Display only UDP sockets on Linux
  3. -l Show listening sockets (for example, SSHD server opens TCP port 22)
  4. -p List process name that made sockets open
  5. -n Do not sort out names of service

The output that you will get in the end will mainly list down the listening ports so, here is it. This is exactly how you will be able to get to locate the listening ports.

In case you are not sure enough of what port maps to what service, what you can always do is look for in the /etc/services file. This is the command you need to enter:

less /etc/services

Now, you will be able to get to see a long list of each and every port that is obtainable to Linux.

The Procedure of Closing a Port

Now, let us get into the process that will let you know how to close a port that is open. Let us suppose that you are organizing a web server on the system but you do not require port 8080 listening.

But, instead of that you only need the traffic to move through ports 80 (HTTP) and 433 (HTTPS).

In order to bring closure to the port 8080, the use of ufw command which stands for Uncomplicated Firewall command will be used. The command goes like the following.

sudo ufw deny 8080

You will be finding out that the rules have been upgraded and the port is completely blocked.

Do not worry as you can re-open this block as well in case you discover any issue with a service. The command for opening a blocked port is mentioned below.

sudo ufw allow 8080

So that is all you need to do for locating a port that is open and then closing it. This is the process for Ubuntu Server 18.04.

This is the technique that will be functioning on most of the distributions. But, one thing that differs is how to block a port and that is because not each and every distribution makes the usage of ufw.

Concluding Note

These were all the steps that you need to follow to discover an open port and then close it. In Linux close port without any trouble and get the odd traffic reduced.

A comment section is attached below to let you all fill up with your views and also if you have followed this process let us know how much useful it was.