How to Open a Port in CentOS?

Opening a port in CentOS/RHEL involves configuring the firewall to allow traffic to a specific port. It allows a specific service, such as a web server or database, to listen for incoming connections on that port. The purpose of opening a port is to allow external clients to connect to the service running on that port while maintaining security.

This article will briefly illustrate the step-by-step instructions to open a port in CentOS/RHEL. 

How To Open A Port In CentOS?

The firewall used in CentOS is called “firewalld“, which allows you to configure the firewall using a command-line interface.

Here are the steps to open a port in CentOS using the command-line interface:

Step 1: Check the Current Status of firewalld

To check the firewalld services, the “systemctl” command is utilized with the “status” utility as below:

$ systemctl status firewalld

The output shows that the services are in active state.

Step 2: Start the firewalld Services

If firewalld is not running, start it by running the following command:

$ systemctl start firewalld

Step 3: Check the Current firewalld Zones

To check the current firewalld zones, the “firewall-cmd” command is utilized with the “get-zones” option as below:

$ firewall-cmd --get-zones

The output returns the “public” zone in the terminal.

Step 4: Open the Ports

The ports can be opened in various ways to serve the purpose: 

Open a Port for All Zones

By default, the firewall has a “public” zone. If you want to open a port for all zones, you can use the “–permanent” option. In our case, specify the “80/tcp” to open a port for all zone:

$ firewall-cmd --permanent --add-port=80/tcp

The output shows that port 80 has been opened for the HTTP service

Open a Port on a Specific Zone

To open a port in a specific zone, use the “–zone” option. For instance, the “public” value is assigned to the zone and the “80/tcp” to “add-port”:

$ firewall-cmd --permanent --zone=public --add-port=80/tcp

The output returns the “success” status in the terminal.

Open a Port for a Service

To open a port for a service, use the “–add-service” option by specifying the services. In our case, specify the “http” services as below:

$ firewall-cmd --permanent --add-service=http

The outcome of the above command opens a port for “http” services.

Step 5: Reload the firewall 

After adding the port or service, you need to reload the firewall to apply the changes:

$ firewall-cmd --reload

The output shows that the firewall has been successfully reloaded.

Step 6: Verify the Open Ports

To check the open ports, use the “list-ports” option with the “firewall-cmd” command:

$ firewall-cmd --list-ports

The output shows the open ports as “443/tcp” and “80/tcp” in the terminal.

Conclusion

CentOS offers the “firewall-cmd –permanent –add-port=80/tcp” command to open a port in the operating system. The users can open a port for all zones, specific zones, and services. This article has explained the step-by-step procedure to open a port in CentOS.