前言
CentOS 默认防火墙状态是开启的,所有端口状态都是关闭的,无法直接使用。因此很多时候为了服务能正常使用,我们需要手动关闭防火墙,或者打开指定防火墙端口以供使用。
CentOS 7 防火墙为 firewalld,旧版本的 CentOS 6 防火墙则是用的 iptables,因此操作指令也有所不同。
CentOS 开启/关闭防火墙
CentOS 7
关闭防火墙
# 关闭
systemctl stop firewalld
# 禁止开机自启
sytemctl disable firewalld
开启防火墙
# 开启
systemctl start firewalld
# 设置开机自启
systemctl enable firewalld
查看防火墙状态
# 查看状态
firewall-cmd --state
# 重启
firewall-cmd --reload
CentOS 6
关闭防火墙
# 关闭
service iptables stop
# 禁止开机自启
chkconfig iptables off
开启防火墙
# 开启
service iptables start
# 设置开机自启
chkconfig iptables on
查看防火墙状态
# 查看状态
service iptables status
# 重启
service iptables restart
CentOS 开放/关闭防火墙端口
CentOS 7
开放/关闭指定端口
# 开启 80 端口,协议 tcp,--permanent 永久生效
firewall-cmd --zone=public --add-port=80/tcp --permanent
# 关闭端口
firewall-cmd --zone=public --remove-port=80/tcp --permanent
# 查看端口状态
firewall-cmd --query-port=80/tcp
CentOS 6
开放/关闭指定端口
# 开启 80 端口,协议 tcp
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
# 关闭端口
iptables -I INPUT -p tcp --dport 80 -j DROP
# 保存
service iptables save