Opening a port on Linux With Examples

Before we learn about opening a port on Linux, let’s understand what network ports are. A port is a communication endpoint. Within an operating system, a port allows the data packets specific processes or network services.

Typically, ports identify a specific network service assigned to them. This can be changed by manually configuring the service to use a different port, but in general, the defaults can be used.

The first 1024 ports (Ports 0-1023) are referred to as well-known port numbers and are reserved for the most commonly used services include SSH (port 22), HTTP and HTTPS (port 80 and 443), etc. Port numbers above 1024 are referred to as ephemeral ports.

Among ephemeral ports, Port numbers 1024-49151 are called the Registered/User Ports. The rest of the ports, 49152-65535 are called as Dynamic/Private Ports.

In this tutorial, we will show how we can open an ephemeral port on Linux, since the most common services use the well-known ports.


List all open ports

Before opening a port on Linux, let us first check the list of all open ports, and choose an ephemeral port to open from that list.

We can use the netstat command to list all open ports, including those of TCP, UDP, which are the most common protocols for packet transmission in the network layer.

NOTE: If your distribution doesn’t have netstat, it is not a problem. You can use the ss command to display open ports via listening sockets.

This will print all listening sockets (-l) along with the port number (-n), with TCP ports (-t) and UDP ports (-u) also listed in the output.

list-open-ports

List Open Ports

Just to ensure that we are getting consistent outputs, let’s verify this using the ss command to list listening sockets with an open port.

List Listening Sockets
list-open-ports

This gives more or less the same open ports as netstat, so we are good to go!


Opening a port on Linux to Allow TCP Connections

Let’s open a closed port and make it listen to TCP Connections, for the sake of this example.

Since port 4000 is not being used in my system, I choose to open port 4000. If that port is not open in your system, feel free to choose another closed port. Just make sure that it’s greater than 1023!

Again, just to make sure, let’s ensure that port 4000 is not used, using the netstat or the ss command.

The output must remain blank, thus verifying that it is not currently used, so that we can add the port rules manually to the system iptables firewall.


For Ubuntu Users and ufw firewall based Systems

Ubuntu has a firewall called ufw, which takes care of these rules for ports and connections, instead of the old iptables firewall. If you are a Ubuntu user, you can directly open the port using ufw

You can skip the next few steps, and directly test your newly opened port!


For CentOS and firewalld based Systems

For these types of systems, if you have firewalld as your primary firewall, it is recommended that you use the firewall-cmd to update your firewall rules, instead of the old iptables firewall.

NOTE: This will reset the firewalld rules to default on a reboot, so if you want to modify this setting permanently, add the --permanent flag to the command.

You can skip the next few steps, and directly test your newly opened port!


For Other Linux Distributions

So let’s add this new port to our system iptables rules, using the iptables command.

If this command is not yet installed, get it using your package manager.

This sets the firewall to append (-A) the new rule to accept input packets via protocol (-p) TCP where the destination port (--dport) is 4000, and specifies the target jump (-j) rule as ACCEPT.

To update the firewall rules, restart the iptables service.

OR using systemctl if you have it.


Test the newly opened port for TCP Connections

Now that we have successfully opened a new TCP port (Port 4000 in my case), let’s test it out.

First, we will start netcat (nc) and listen on port 4000, while sending the output of ls to any connected client. So after a client has opened a TCP connection on port 4000, they will receive the output of ls.

This makes netcat listen on port 4000. Leave this session alone for now.

Open another terminal session on the same machine.

Since I’ve opened a TCP port, I’ll use telnet to check for TCP Connectivity. If the command doesn’t exists, again, install it using your package manager.

Format for telnet:

So input your server IP and the port number, which is 4000 in my case, and run this command.

This tries to open a TCP connection on localhost on port 4000.

You’ll get an output similar to this, indicating that a connection has been established with the listening program (nc).

check-open-port

Check Port Using Telnet

As you can see, the output of ls (while.sh in my case) has also been sent to the client, indicating a successful TCP Connection!

To show you that the port is indeed open, we can use nmap to check this.

Check Open Port
check-open-port

Indeed, our port has been opened! We have successfully opened a new port on our Linux system!

NOTE: nmap only lists opened ports which have a currently listening application. If you don’t use any listening application such as netcat, this will display the port 4000 as closed, since there isn’t any application listening on that port currently. Similarly, telnet won’t work either, since it also needs a listening application to bind to. This is the reason why nc is such a useful tool. This simulates such environments in a simple command.

But this is only temporary, as the changes will be reset every time we reboot the system.


Need to update rules after every reboot

The approach presented in this article will only temporarily update the firewall rules until the system shuts down/reboots. So similar steps must be repeated to open the same port again after a restart.

For ufw Firewall

The ufw rules are not reset on reboot, so if you’re a Ubuntu user, you need not worry about this part!

This is because it is integrated into the boot process and the kernel saves the firewall rules using ufw, via appropriate config files.

For firewalld

As mentioned earlier, firewalld also suffers from the same problem, but this can be avoided by appending a --permananent flag to the initial command, when opening a port or setting any other rule.

For example, you can open the TCP Port 4000 permanently using the below command:

For iptables

For the iptables firewall, although this inconvenience cannot be avoided, we could minimize the hassle.

We can save the iptables rules to a config file, such as /etc/iptables.conf.

We can then retrieve it from the config file after we reboot, using the below command:

Now, the iptables rules are now updated, and our ports are opened again!


Conclusion

In this tutorial, we showed you how you could open a new port on Linux and set it up for incoming connections.


By admin

Leave a Reply

%d bloggers like this: