How do you open a specific port in CentOS firewall?

Learn how to open a specific port in CentOS firewall by following these step-by-step instructions. Keep your system secure while allowing necessary connections.

How do you open a specific port in CentOS firewall?

How to Open a Specific Port in CentOS Firewall

CentOS comes with a built-in firewall known as firewalld which allows you to manage the firewall rules easily. If you need to open a specific port on your CentOS server, you can do so by following these steps:

Step 1: Check the Current Firewall Status

Before opening a port, it's a good idea to check the current status of the firewall to ensure that the port you want to open is not already blocked. You can do this by running the following command:

        sudo firewall-cmd --state
    

If the firewall is running, you will see the output as running. If it's not running, you can start it using:

        sudo systemctl start firewalld
    

Step 2: Open the Specific Port

To open a specific port, you need to use the firewall-cmd command with the --add-port option followed by the port number and protocol. For example, to open port 80/tcp, you would run the following command:

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

The --permanent flag makes the rule persistent, so it will survive a firewall reload or server reboot. If you want to open a UDP port, you can specify the protocol as udp like this:

        sudo firewall-cmd --add-port=123/udp --permanent
    

After adding the port, you need to reload the firewall for the changes to take effect:

        sudo firewall-cmd --reload
    

Step 3: Verify the New Firewall Rule

To verify that the port is now open in the firewall, you can list all the open ports using the following command:

        sudo firewall-cmd --list-ports
    

This will display a list of all the open ports on your server. You should see the port you just opened in the list.

Step 4: Additional Firewall Configuration

If you need to restrict access to the newly opened port to specific IP addresses or networks, you can do so by adding additional rules. For example, to allow access to port 22 (SSH) only from a specific IP address, you can use the following command:

        sudo firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port port=22 protocol=tcp accept'
    

This command allows access to port 22 from the IP address 192.168.1.100 only. You can modify the source address and port number as needed.

Step 5: Save the Firewall Configuration

Once you have configured the firewall rules as needed, it's important to save the configuration to ensure that the changes persist across reboots. You can do this by running:

        sudo firewall-cmd --runtime-to-permanent
    

This command will make the runtime configuration permanent.

Step 6: Check the Status of the Firewall

Finally, you can check the status of the firewall to ensure that the changes have been applied successfully. Run the following command:

        sudo firewall-cmd --list-all
    

This will display a detailed overview of all the firewall rules currently in effect on your CentOS server.

What's Your Reaction?

like

dislike

love

funny

angry

sad

wow