Open multiple firewall ports in CentOS7
Solution 1
You can define a service from an xml file containing all the ports you need, add a service from it and then enable it. Create the service.xml file like so:
<?xml version="1.0" encoding="utf-8"?>
<service>
<port port="port1" protocol="proto1"/>
<port port="port2" protocol="proto2"/>
<port port="port3" protocol="proto3"/>
<port port="port4" protocol="proto4"/>
</service>
Add new service:
# firewall-offline-cmd --new-service-from-file=service.xml --name=My_Service
Reload firewall-cmd:
# firewall-cmd --reload
Then add your service:
# firewall-cmd --add-service My_Service
Solution 2
You can always make a small script/one-liner:
#!/bin/bash
for i in 80 443 22 123 21 1337 31337
do
firewall-cmd --zone=public --add-port=${i}/tcp
done
Solution 3
If those open ports are in a range for example 2379-2385, you can do as follows:
firewall-cmd --zone=zone_name --add-port=2379-2385/tcp
To make it permanent add --permanent
option at end.
Solution 4
firewall-cmd --permanent --add-port={80/tcp,443/tcp,9200/tcp,5601/tcp,5044/tcp}
firewall-cmd --reload
Related videos on Youtube
Markus Wilhelm
Updated on September 18, 2022Comments
-
Markus Wilhelm over 1 year
I need to open multiple different ports (not in ranges) on a CentOS machine.
I know how to open a port with
firewall-cmd
, but that gets bothersome for opening like 40 and more ports.Is there a configuration file where I can define all open ports in one place? Sadly I didn't find anything regarding this.
-
Scott - Слава Україні about 3 yearsPlease explain what this does and how it solves the problem. … … … … … … … … … Please do not respond in comments; edit your answer to make it clearer and more complete. … … … P.S. Why not just do
--add-port={80,443,9200,5601,5044}/tcp
? -
Mohammad Faisal about 3 yearsThe trick
{}
used above is called Brace Expansion.