Since 2016 · 50,000+ customers · ★ 5.0 on Google Live chatWhatsAppTelegramOpen a ticket
Get started
Linux

How to Open a Firewall Port on CentOS 7, AlmaLinux and Rocky Linux

How to open a firewall port on CentOS guide by 99RDP

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.

Diagram of firewalld zones: interfaces belong to a zone, and the zone rules decide which services and ports are open
How firewalld zones work

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.

Comparison of firewalld runtime and permanent rules: why you add --permanent and reload
Runtime vs permanent rules

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.

Diagram of firewalld runtime and permanent rules and how --permanent and --reload work together
Runtime vs permanent rules

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

  1. Check the firewall: sudo firewall-cmd --list-ports and --list-services.
  2. Check something is listening: sudo ss -tulpn | grep 8080. An open firewall port with no program listening still won't answer.
  3. Test from another machine: nc -zv SERVER_IP 8080 or, 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.

Nishant Kumar, Sr. Server Engineer at 99RDP
Written byNishant KumarSr. Server Engineer, 99RDP. Part of the team that runs our servers and answers support.
Keep reading

More Linux guides

All Linux guides

Need a server for this?

✓ Setup in minutes✓ Full admin access✓ 7-day refund on new services