To open a port on a Linux VPS server, we must first check with the netstat command whether the desired port is open or closed, then we decide to open or close each one through the Linux firewall.
Also, control of Linux server ports is very important for people who own their own businesses or use VPS servers because there is a possibility of exploitation and intrusion to the server, so we need to take this seriously. If you are one of those who need to change or close some public ports to keep your server secure, then this article could be useful for you.
The steps we need to take to open a port on Linux VPS are:
Now let’s check and open some common Linux VPS ports in practice.
To open a port on Ubuntu Linux vps, first use the open ports list to select a temporary port that is not on the list, so the “netstat” and “ss” commands can be used.
netstat -lntu
Note: The meaning of the words “TCP” corresponds for “Transfer Control Protocol” and “UDP” is abbreviation of “User Datagram Protocol”.
If you don’t get a response from the netstat command and you get the message “Netstat Command Not Found”, you can install the system request that is based on the “install net-tools” and then run the netstat command after the installation again.
Also, if you don’t get a response from the netstat command, you can use the following command, the output of both is the same:
ss -lntu
Note: The environment of above figure, has been taken after enter to the Linux Server by the corresponding username and password, if you don’t know how to enter this environment, so you can study the “Connect to Linux VPS Server” article.
Now you have to select the port number you want to open (for example, in this tutorial, port number 2000 is selected) and use the command “$ netstat -na | grep :2000” to make sure the port is closed and it is not being used by another service (figure below)
netstat -na | grep :2000
If something isn’t written in the command output, it means that the port is empty and ready to use.
Now is the time to insert the desired port number (in this case port number 2000) into the firewall table, so the UFW program must be installed on the Linux server first with the following command:
sudo apt install ufw
After installing the firewall, use the following command to add the desired port to the firewall table:
sudo ufw allow 2000
Now, it is important to activate the firewall with the following command:
sudo ufw enable
To initialize the server to connect through the desired port while waiting for a connection request from the client, use the following command to initialize it:
ls | nc -l -p 2000
In this case, the server waits to receive the request for communication from the client. The blinking mark shows this.
After establishing a connection between the Linux server and the client server, use the “nmap” command to check if port 2000 is open as follows:
nmap localhost -p 2000
If you encounter this image after executing this command, install the requested steps as requested by the system and proceed:
In this article, we explained how to open ports on Linux VPS.
According to this article, you can simply open a port on the Linux server via a few commands. If you are familiar with the Linux commands line, you can open a port in just a few minutes.
Good luck.