Ports are how you send and receive traffic in Linux. All applications and services rely on the communication ports to connect and communicate with each other. In the case of remote connection, such as establishing an HTTP or telnet connection, the firewall is responsible for managing the traffic through the ports. So, if you want to open a port in Linux, you must configure your firewall to allow traffic through the specified port. Only then can you have a port in Linux open for incoming connections. This post guides you on understanding what steps and commands you should use to open a port in Linux. Read on!
Prerequisites
We’ve mentioned that opening a port in Linux involves configuring the firewall rules. As such, you must understand which firewall tool is required for your Linux distro.
Most Linux distributions rely on ‘iptables”’ufw’ or ‘firewalld’ tools. The commands to open the ports will differ depending on which tool you use. For this tutorial, we use Ubuntu, which uses the ufw tool. However, we will still mention the commands to use if using either ‘iptables’ or firewalld.’
How to Open a Port on Linux
Every Linux system has endpoints that facilitate network connectivity for applications and services. There are 65,536 endpoints in Linux, and these endpoints are what we term “ports.” A firewall tool is used to manage what traffic goes through the port, and you can have the port in different states. For instance, the port can be closed or set to a listening state.
Each port is referenced based on its port number or the services that run in it. However, not all ports are reserved for specific applications or services. Ports 0 to 1023 are the commonly known ports used by services depending on the protocol. For instance, SSH is reserved to use port 22.
Here are the steps to follow when you want to open a port on Linux.
Step 1: View the Open Ports
Assuming you don’t know the exact port that you want to open. The best approach is to list the open ports and decide on which closed port (not listed) to open. The Linux netstat command lets users list all the open ports and gives related information.
Moreover, you can combine it with other flags for better results. The -l flag shows the listening ports, the -n shows the port number, -t shows the TCP ports, and -u shows the UDP ports. You can run your netstat command as follows.
After running the command, you will get a result like the one below. Under the local address, the number that comes after the colon, such as 33060 in the first row, is the port number.
The list size depends on how many ports you have opened for your case. For a case of a long list, the ideal way to check if a target port is open is to filter the results using the grep command. Let’s use port 8080 for this example.
Running the command, we get a blank result confirming that the port is not opened. In that case, we can go ahead and open it.
Step 2: Editing the Firewall Rule
If using the ufw firewall tool, you can open port 8080 for TCP connections with the command below.
If using firewalld, such as the case of CentOS, run your command as follows.
Lastly, for someone using iptables, use the command below.
With that, you’ve managed to open a port on Linux.
Step 3: Check the Firewall Rules
To confirm that we’ve managed to open the target port on Linux, we will access the firewall rules, which contain the added rules and their state.
Run the ufw command below.
The output below shows that our port 8080 is open and can accept connections from anywhere.
For other systems, you can still use the netstat command we used earlier and check if the port is now listed in the open ports.
Step 4: Persist the Changes
If you use ufw, your firewall rules are permanent and won’t rest on reboot. However, those systems using firewalld and iptables must specify that they want to persist the changes. Otherwise, the port will be closed upon reboot. You can read more on how to set up firewalld for your system, or how to set up iptables to learn how to persist the changes.
Conclusion
There are different reasons why you want to open a port on Linux. The common reason is when you want to establish network connectivity for an application or service. To open a port on Linux, use the firewall tool to add a rule that opens the port. You can then verify the port is opened by checking its state and then proceed to establish any connection through the port. All the steps you need to open a port on Linux are detailed in this post.