CentOS/RHEL: How to Open Port in Linux Firewall
This post will guide you how to open a port in CentOS/RHEL Linux. How do I open a port in the firewall on CentOS/RHEL Linux system. How to open tcp ports 80 and 443 in your Linux system.
- Open TCP Port 80/443 on CentOS/RHEL 5/6
- Open TCP Port 80/443 on CentOs/RHEL 7 /8
- Check Current Firewall Rules on CentOs/RHEL
Open TCP Port 80/443 on CentOS/RHEL 5/6
On CentOs/RHEL 5/6, you can use iptables command to add one firewall policy to open a TCP port. Or you can modify the iptables configuration file to add firewall rules.
Type the following commands to open TCP port 80 and 443:
# iptables -I INPUT -p tcp -m tcp -dport 80 -j ACCEPT # iptables -I INPUT -p tcp -m tcp -dport 443 -j ACCEPT # service iptables save
Then you need to restart the iptables service, type:
# service iptables restart
You can also add one new firewall rule into iptables configuration file located in /etc/sysconfig/iptables to open a port in your current CentOS/RHEL 4/5/6 Linux. Do the following steps:
#1 edit file /etc/sysconfig/iptables via vi/vim text editor.
# vi /etc/sysconfig/iptables
#2 appending the following line:
-A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 80 -j ACCEPT -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 443 -j ACCEPT
#3 save and close the file.
#4 restart iptables service, type:
# service iptables restart
Or
# /etc/init.d/iptables restart
If you need to open other tcp ports, you just change the above port as you need.
Open TCP Port 80/443 on CentOs/RHEL 7 /8
For CentOS/RHEL 7 Linux, the firewall rule settings are managed by firewalld service damemon. and if you want to open a tcp port, you need to use firewall-cmd command to achieve it. To open tcp ports 80 and 443, just use the following commands:
# firewall-cmd --zone=public --add-port=80/tcp --permanent # firewall-cmd --zone=public --add-port=443/tcp --permanent # firewall-cmd --reload
Then you need to restart firewalld service, type:
# systemctl restart firewalld.service
Outputs:
[root@osetc ~]# firewall-cmd --zone=public --add-port=80/tcp --permanent success [root@osetc ~]# firewall-cmd --zone=public --add-port=443/tcp --permanent success [root@osetc ~]# firewall-cmd --reload success [root@osetc ~]# systemctl restart firewalld.service
If you want to check if those two tcp ports are opened.Just use the following command:
# firewall-cmd --list-port
Outputs:
[root@osetc ~]# firewall-cmd --list-port 443/tcp 80/tcp
From the above outputs, we’ll see that both 80 and 443 ports are opened.
Check Current Firewall Rules on CentOS/RHEL
If you want to check current firewall rules on your centos or RHEL Linux, you can type the following command:
# iptables -L
Outptus:
[root@osetc ~]# iptables -L Chain INPUT (policy ACCEPT) target prot opt source destination ACCEPT all -- anywhere 192.168.1.1 ACCEPT all -- anywhere anywhere ACCEPT all -- anywhere anywhere ctstate RELATED,ESTABLISHED ACCEPT all -- anywhere anywhere INPUT_direct all -- anywhere anywhere INPUT_ZONES_SOURCE all -- anywhere anywhere