announcement

Check our homepage for new, visually rich, fast and immersive experiences!

How to Open a Port in Linux?

In this article, you will learn how to open a port in Linux, as the users of this OS may need to know this for various purposes and go through 'iptables' and the firewalls to find out how can they access a particular port.
Techspirited Staff
Last Updated: Apr 9, 2018
How to open a port in Linux? The answer to this question is pretty simple. However, you need to first verify whether the particular port is already open or not. The command for opening a particular port varies, depending on whether you want to open it for TCP or UDP connection. So, let us first try to understand how to verify if a port is open or not. Here, in this article, I have taken up the command line approach to perform these tasks, after all, it is the command line approach that makes Linux the wonderful operating system that it is. If you are a keen enthusiast in learning it, you may find the following articles equally interesting.How to Verify if a Port is Open?

The command is as follows:

netstat -nap | grep : portno
where portno should be replaced with the numerical port number you want to check, if it is open or not.

From the output of this command, you can make out if a port is open or not in Linux. All Linux versions do not support this command.

There are few other commands that can be used to check whether a particular port is open or not. You can type:
netstat -tulp or netstat -tulpn
The output of this command will give you a table with a Local Address column where all the open ports will be mentioned.

nmap -sT -O localhost
The output of this command, when issued from the console will give a list of ports that are listening to the TCP connections on the network.

lsof -i
This command will give a list of all the open ports.

lsof -i | grep portno
Where portno is the number of the port that you wish to check.

If you wish to check the open ports from a remote machine, you can use the nmap or netcat commands.
netcat targetHost portno

How to Open a Port in Linux?

This can be done with the following commands. You can type the following command:

iptables -A INPUT -p tcp -dport portno -j ACCEPT
where portno needs to be replaced with the numerical port number that you want to open.

For opening a UDP port, type the following command:

iptables -A INPUT -p udp -sport portno -j ACCEPT
where portno needs to be replaced with the numerical port number that you want to open.

You may also find the following articles on Linux quite informative:Here, in this article, I have assumed that you have logged in as the root user. With this, I complete this article on Linux, how to open a port. Hope this article proves to be of some help to you.