Hello, friends. In this post, you will learn how to open ports in the Firewall using Linux. This way you will be able to have a base to continue working with your applications without problems. For this guide, we will use the terminal although there are graphical applications such as GUFW.
Why is it important to use a Firewall?
Although Linux is a very secure system, it is not perfect and can be attacked by an external network. Especially critical applications that make use of data such as database managers or others. Therefore, it is advisable to install a Firewall and establish adequate policies to protect your computer.
In addition to security, there is something that many people overlook and that is that in a Firewall we can not only block incoming connections but also outgoing ones. In this way, we will be able to know which applications are making connections with the Internet and make our decisions.
In Linux, there are a variety of Firewalls that can be classified as domestic or professional like PFSense. I believe that the domestic ones can work in many cases and we will work on them in this post.
How to Open Ports in Linux – Debian, Ubuntu, and derivatives
In these distributions, UFW is present by default. In case it is not then you can install it without problems.
sudo apt install ufw
After that, you can start opening ports.
The easiest way is to follow this syntax
sudo ufw allow [port]
This way, if you want to open port 26 for all protocols, just run.
sudo ufw allow 26
You can also specify a specific protocol for that port like UDP
or TCP
.
sudo ufw allow 26/tcp
In this case, only port 26 will be opened for the TCP protocol.
If you have to open many consecutive ports, then you can specify a range
sudo ufw allow 4000:4007/tcp
This will open all ports from 4000 to 4007 for TCP. Remember that you can change tcp
to udp
and enable them for that protocol.
To apply the changes run
sudo ufw reset
So, this way, you can open the ports in Debian, Ubuntu, and derivatives.
How to Open Ports in Linux – RHEL, CentOS and Fedora
Unlike Debian, Ubuntu, and its derivatives, in the RHEL family, another similar application is used to manage the Firewall and it is firewalld
.
In case it is not installed, you can do it as follows
sudo dnf install firewalld
And after that, initialize it and make it run with the system.
sudo systemctl enable firewalld sudo systemctl start firewalld
This application works with zones with public
being the default zone. If we assume that this is the current zone, we can open a port as follows:
sudo firewall-cmd --zone=public --add-port=[port]/[protocol]
If you would like to open port 2122
of the TCP protocol, then you should run
sudo firewall-cmd --zone=public --add-port=2122/tcp
Unlike UFW in this case we do have to specify the protocol.
Similarly, you can specify a range of ports but still specify the protocol.
sudo firewall-cmd --zone=public --permanent --add-port=3220-3225/udp
In this case, I have opened ports from 3220
to 3225
in the UDP protocol.
To apply the changes run
sudo firewall-cmd --reload
This way you can open ports in Linux
Conclusion
Opening ports in Linux is very easy and can help you with a network problem. Also having a Firewall always increases the security of the system.