====================================================================================
说明: Centos6.5与Centos7下开放防火墙端口操作是截然不同的,本文件简单记录下两个版本操作防火墙的常见命令。
====================================================================================
一、Centos6.5下开放防火墙端口
(1)修改防火墙配置,添加要开放的端口
[root@localhost user]# vi /etc/sysconfig/iptables
要添加的代码(3306 是要开放的端口号):
-A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
附全文 :
==========================================================================
1 # Firewall configuration written by system-config-firewall
2 # Manual customization of this file is not recommended.
3 *filter
4 :INPUT ACCEPT [0:0]
5 :FORWARD ACCEPT [0:0]
6 :OUTPUT ACCEPT [0:0]
7 -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
8 -A INPUT -p icmp -j ACCEPT
9 -A INPUT -i lo -j ACCEPT
10 -A INPUT -m state --state NEW -m tcp -p tcp --dport 22 -j ACCEPT
11 -A INPUT -m state --state NEW -m tcp -p tcp --dport 8080 -j ACCEPT
12 -A INPUT -m state --state NEW -m tcp -p tcp --dport 3306 -j ACCEPT
14 -A INPUT -j REJECT --reject-with icmp-host-prohibited
15 -A FORWARD -j REJECT --reject-with icmp-host-prohibited
16 COMMIT
==========================================================================
(2)重启防火墙服务:
[root@localhost user]# service iptables restart
二、Centos7下开放防火墙端口
7版本的配置地址:
cat /etc/firewalld/zones/public.xml
centos7防火墙相关操作:
查看防火墙状态: systemctl status firewalld
启动: systemctl start firewalld
停止: systemctl stop firewalld
禁用(重启默认关闭防火墙): systemctl disable firewalld
(1)如:开放3306端口
[root@localhost java]# firewall-cmd --zone=public --add-port=3306/tcp --permanent
(2)重新加载防火墙配置
[root@localhost java]# firewall-cmd --reload
success
[root@localhost java]#
(3)查看已开放的端口列表
[root@localhost java]# firewall-cmd --list-port
3306/tcp
[root@localhost java]#
【附】其他相关命令:
① 关闭指定端口(如:关闭3306端口)
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
② 查看端口状态(如:查看3306端口)
firewall-cmd --zone=public --query-port=3306/tcp