
Here, 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.
The answer to the 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 we 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.
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.
★ 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.
★ netcat targetHost portno
If you wish to check the open ports from a remote machine, you can use the nmap or netcat commands.
How to Open a Port in Linux?
This can be done with 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.
Here, we have assumed that you have logged in as the root user and have the superuser access. This should clear your concepts on how to open a port in Linux.