firewalld: How to Open and Close Ports

#firewalld #technology

If you are using firewalld[1] to protect your system, you may at times need to open local ports to let traffic into your machine.

Before making any changes, you can check your active zones:

sudo firewall-cmd --get-active-zones

And then find all open ports in a given zone (public used in this example).

sudo firewall-cmd --zone=public --list-ports

To open a new port (let's say port 8080), run the following. You may substitute tcp for udp if you require that protocol.

sudo firewall-cmd --zone=public --add-port=8080/tcp

After testing, you may elect to make the change persist by setting the --permanent flag per the below:

sudo firewall-cmd --permanent --zone=public --add-port=8080/tcp

Inversely, if you need to close a port:

sudo firewall-cmd --zone=public --remove-port=8080/tcp

# Make the change persist using the `--permanent` flag
sudo firewall-cmd --permanent --zone=public --remove-port=8080/tcp

Remember to reload the active config with sudo firewall-cmd --reload.

  1. https://firewalld.org/documentation/index.html ↩


Comments