Skip to content

CentOS 7 和 6 防火墙

CentOS 7 防火墙 firewalld 开放端口

bash
#查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state

# 查看已经开放的端口:
firewall-cmd --list-ports

# 重启防火墙
firewall-cmd --reload
# 启动 firewall
systemctl start firewalld.service
# 停止 firewall
systemctl stop firewalld.service
# 开机时启动firewall
systemctl enable firewalld.service
# 禁止firewall开机启动
 systemctl disable firewalld.service

# 开放端口(这里开放 3306和3307 端口)
# –zone 作用域;–add-port=80/tcp 添加端口,格式为:端口/通讯协议;–permanent #永久生效,没有此参数重启后失效
firewall-cmd --zone=public --add-port=8080/tcp --permanent
firewall-cmd --zone=public --add-port=3306/tcp --add-port=3307/tcp --permanent
firewall-cmd --reload

# 开放端口范围
firewall-cmd --zone=public --add-port=20000-20010/tcp --permanent
firewall-cmd --reload

# 给固定IP开放端口:
firewall-cmd --permanent --add-rich-rule 'rule family=ipv4 source address=192.168.0.1/2 port port=80 protocol=tcp accept'

# 关闭一个端口
firewall-cmd --zone=public --remove-port=3306/tcp --permanent
firewall-cmd --reload

更多参考:https://blog.csdn.net/s_p_j/article/details/80979450

CentOS 6 防火墙 iptables 开放端口

查看防火墙状态及开启关闭命令

shell
# 开启/关闭防火墙(永久)
# 开启: 
chkconfig iptables on
# 关闭: 
chkconfig iptables off

# 查看防火墙状态
[root@localhost ~]# service iptables status
iptables:未运行防火墙。
# 开启防火墙
[root@localhost ~]# service iptables start
# 关闭防火墙
[root@localhost ~]# service iptables stop
# 重启防火墙
[root@localhost ~]# service iptables restart

centos6.x 添加防火墙端口

shell
# 查看已开放的端口
[root@localhost ~]# /etc/init.d/iptables status

# 开放 3306 和 8080 端口
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 3306 -j ACCEPT
[root@localhost ~]# /sbin/iptables -I INPUT -p tcp --dport 8080 -j ACCEPT
# 保存
[root@localhost ~]# /etc/rc.d/init.d/iptables save

# 查看已开发的端口
[root@localhost ~]# /etc/init.d/iptables status