
CentOS 7 and its successors (AlmaLinux, Rocky Linux and RHEL 8, 9 and 10) use firewalld as the firewall. To make a web server, game server or database reachable, you have to open its port, and the most common mistake is opening it only until the next reload. This guide shows how to check the firewall, open and close ports and services permanently, allow a port only from one IP, use iptables on older setups, and verify that the port is really open.
Quick answer
sudo firewall-cmd --permanent --add-port=8080/tcp
sudo firewall-cmd --reload
sudo firewall-cmd --list-ports
A note on CentOS 7
CentOS 7 reached end of life on 30 June 2024 and no longer receives security updates. The firewall commands below are identical on AlmaLinux and Rocky Linux, so they also apply after you migrate, which you should do for any internet-facing server.
Step 1: Check that firewalld is running
sudo systemctl status firewalld
sudo firewall-cmd --state
If it's stopped, start and enable it. Allow SSH first if you're working remotely (it's allowed by default in the public zone):
sudo systemctl enable --now firewalld
sudo firewall-cmd --list-all
--list-all shows the active zone (usually public), the interfaces in it, and the services and ports already allowed.
Step 2: Open a port
# one TCP port
sudo firewall-cmd --permanent --add-port=8080/tcp
# a UDP port
sudo firewall-cmd --permanent --add-port=27015/udp
# a range
sudo firewall-cmd --permanent --add-port=30000-30100/tcp
# apply the permanent rules
sudo firewall-cmd --reload
Without --permanent, the rule takes effect immediately but disappears at the next reload or reboot. With --permanent, it's saved but only active after --reload. Using both steps, as above, is the reliable pattern.
Step 3: Open a service instead of a port number
firewalld knows the ports of common services by name:
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo firewall-cmd --permanent --add-service=mysql
sudo firewall-cmd --reload
firewall-cmd --get-services # list all known service names
Service names make the configuration easier to read than a list of numbers.
Step 4: Allow a port only from one IP address
For databases, admin panels and RDP, don't open the port to everyone. Use a rich rule:
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="198.51.100.25" port port="3306" protocol="tcp" accept'
sudo firewall-cmd --reload
Or put trusted addresses in the trusted zone, which allows everything from them:
sudo firewall-cmd --permanent --zone=trusted --add-source=198.51.100.25
sudo firewall-cmd --reload
Step 5: Close a port
sudo firewall-cmd --permanent --remove-port=8080/tcp
sudo firewall-cmd --permanent --remove-service=http
sudo firewall-cmd --reload
Step 6: Verify the port is open
- Check the firewall:
sudo firewall-cmd --list-portsand--list-services. - Check something is listening:
sudo ss -tulpn | grep 8080. An open firewall port with no program listening still won't answer. - Test from another machine:
nc -zv SERVER_IP 8080or, from Windows PowerShell,Test-NetConnection SERVER_IP -Port 8080.
Still closed? Check your hosting provider's firewall or security group, which sits in front of the server, and SELinux (see below).
If you use iptables instead of firewalld
Some older CentOS 7 servers have firewalld removed and use the iptables-services package. Then:
sudo iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
sudo service iptables save # writes /etc/sysconfig/iptables
Don't run firewalld and iptables-services at the same time; pick one.
SELinux and non-standard ports
If you move a service to a non-standard port, for example SSH to 2222 or Apache to 8081, SELinux may block the service from binding even though the firewall is open. Label the port for that service:
sudo semanage port -a -t ssh_port_t -p tcp 2222
sudo semanage port -a -t http_port_t -p tcp 8081
(semanage comes from the policycoreutils-python package on CentOS 7, or policycoreutils-python-utils on EL8+.)
Frequently asked questions
How do I open a port in CentOS 7?
Run sudo firewall-cmd --permanent --add-port=PORT/tcp and then sudo firewall-cmd --reload.
Why did my opened port close after a reboot?
You added the rule without --permanent. Add it again with --permanent and reload.
How do I see which ports are open in firewalld?
Run sudo firewall-cmd --list-all, which shows open ports, services and rich rules for the active zone.
Are these commands the same on AlmaLinux and Rocky Linux?
Yes. AlmaLinux, Rocky Linux and RHEL 8, 9 and 10 all use firewalld with the same commands.


