一、开始配置
CentOS7默认的防火墙不是iptables,而是firewalle.
安装iptable iptable-service
#先检查是否安装了iptables
[[email protected] ~]# service iptables status
没有找到
#安装iptables
[[email protected] ~]# yum install -y iptables
(图片未截完全,只有开头和结尾部分)
#升级iptables
[[email protected] ~]# yum update iptables
#安装iptables-services
[[email protected] ~]# yum install iptables-services
禁用/停止自带的firewalld服务
#停止firewalld服务
[[email protected] ~]# systemctl stop firewalld
#禁用firewalld服务
[[email protected] ~]# systemctl mask firewalld
设置现有规则
#查看iptables现有规则
[[email protected] ~]# iptables -L -n
#先允许所有,不然有可能会杯具
[[email protected] ~]# iptables -P INPUT ACCEPT
#清空所有默认规则
[[email protected] ~]# iptables -F
#清空所有自定义规则
[[email protected] ~]# iptables -X
#所有计数器归0
[[email protected] ~]# iptables -Z
#允许来自于lo接口的数据包(本地访问)
[[email protected] ~]# iptables -A INPUT -i lo -j ACCEPT
#开放22端口
[[email protected] ~]# iptables -A INPUT -p tcp --dport 22 -j ACCEPT
#开放21端口(FTP)
[[email protected] ~]# iptables -A INPUT -p tcp --dport 21 -j ACCEPT
#开放80端口(HTTP)
[[email protected] ~]# iptables -A INPUT -p tcp --dport 80 -j ACCEPT
#开放443端口(HTTPS)
[[email protected] ~]# iptables -A INPUT -p tcp --dport 443 -j ACCEPT
#允许ping
[[email protected] ~]# iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT
#允许接受本机请求之后的返回数据 RELATED,是为FTP设置的
[[email protected] ~]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
#其他入站一律丢弃
[[email protected] ~]# iptables -P INPUT DROP
#所有出站一律绿灯
[[email protected] ~]# iptables -P OUTPUT ACCEPT
#所有转发一律丢弃
[[email protected] ~]# iptables -P FORWARD DROP
#保存上述规则
[[email protected] ~]# service iptables save
开启iptables服务
#注册iptables服务
#相当于以前的chkconfig iptables on
[[email protected] ~]# systemctl enable iptables.service
#开启服务
[[email protected] ~]# systemctl start iptables.service
#查看状态
[[email protected] ~]# systemctl status iptables.service
启动成功