How to Open and Manage Linux Ports with firewalld and iptables
This guide walks through enabling the firewall, opening specific ports, and reloading rules on CentOS 7 using firewalld and on CentOS 6 using iptables, providing exact commands, optional parameters, and troubleshooting tips for successful network configuration.
Using firewalld on CentOS 7
Check the firewall status: firewall-cmd --state If the output is "not running", start the service first.
Start the firewalld service: systemctl start firewalld.service Verify that the service is now active.
Add a permanent port rule (example opens MySQL port 3306):
firewall-cmd --zone=public --add-port=3306/tcp --permanentThe command returns "success" when the rule is applied. The --zone=public flag limits the rule to the public zone, and --permanent makes it survive reboots.
Restart the firewall to apply changes immediately: systemctl restart firewalld.service No output indicates success.
Reload the firewall configuration (alternative to restart): firewall-cmd --reload A "success" message confirms the reload.
Additional useful commands:
List opened ports: firewall-cmd --list-ports Close a specific port (e.g., 8080):
firewall-cmd --zone=public --remove-port=8080/tcp --permanent
systemctl restart firewalld.service
firewall-cmd --reloadTemporarily stop the firewall: systemctl stop firewalld.service Permanently disable the firewall (must stop it first):
systemctl disable firewalld.serviceUsing iptables on CentOS 6
Edit the iptables configuration file: # vi /etc/sysconfig/iptables Add or modify rules as needed, then save the file.
Restart iptables to apply the new rules:
/etc/init.d/iptables restartImportant notes
Even after a port appears open in the firewall, remote telnet may still fail if no process is listening on that port. For example, opening port 3306 does not make MySQL reachable until the MySQL service is started and bound to the port.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Liangxu Linux
Liangxu, a self‑taught IT professional now working as a Linux development engineer at a Fortune 500 multinational, shares extensive Linux knowledge—fundamentals, applications, tools, plus Git, databases, Raspberry Pi, etc. (Reply “Linux” to receive essential resources.)
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
