iptables防火墙具有4表5链,4表分别是filter表、nat表、raw表、mangle表,5链分别是INPUT链、OUTPUT链、FORWARD链、PREROUTING链、POSTROUTING链。防火墙规则要求写在特定表的特定链中。

centos7防火墙升级命令 centos7.5防火墙配置_centos7防火墙升级命令

iptables.png

centos7防火墙升级命令 centos7.5防火墙配置_linux_02

filter1.png

关闭firewalld服务器,启动iptables服务

systemctl stop firewalld.service 
systemctl disable firewalld.service

iptables命令的基本使用方法

iptables [-t 表名] 选项 [链名] [条件] [-j 目标操作]

示例:

iptables  -t  filter  -I  INPUT -p  icmp  -j  REJECT

#注意事项与规律:
#可以不指定表,默认为filter表
#可以不指定链,默认为对应表的所有链
#按顺序匹配,匹配即停止,如果没有找到匹配条件,则执行防火墙默认规则
#选项/链名/目标操作用大写字母,其余都小写

#目标操作:
# ACCEPT:允许通过/放行
# DROP:直接丢弃,不给出任何回应
# REJECT:拒绝通过,必要时会给出提示
# LOG:记录日志,然后传给下一条规则

iptables常用的管理选项:

centos7防火墙升级命令 centos7.5防火墙配置_IP_03

常用管理选项.png

一、iptables命令的使用

创建规则

iptables -F #清空所有规则:

追加规则至filter表中的INPUT链的末尾,允许任何人使用TCP协议访问本机

iptables -t filter -A INPUT -p tcp -j ACCEPT

插入规则至filter表中的INPUT链的开头,允许任何人使用UDP协议访问本机

iptables -I INPUT -p udp -j ACCEPT

插入规则至filter表中的INPUT链的第2行,允许任何人使用ICMP协议访问本机

iptables -I INPUT 2 -p icmp -j ACCEPT

查看iptables防火墙规则

iptables  -nL  INPUT                                #仅查看INPUT链的规则
iptables  -L  INPUT  --line-numbers         #查看规则,显示行号

删除规则,清空所有规则:

iptables  -D  INPUT  3          #删除filter表中INPUT链的第3条规则
iptables  -F                           #清空filter表中所有链的防火墙规则
iptables  -t  nat  -F               #清空nat表中所有链的防火墙规则
iptables  -t  mangle  -F         #清空mangle表中所有链的防火墙规则
iptables  -t  raw  -F              #清空raw表中所有链的防火墙规则

设置防火墙默认规则

设置INPUT链默认规则为DROP:

iptables  -t  filter  -P  INPUT  DROP

iptables  -nL
Chain INPUT (policy DROP)
… …

设置INPUT链默认规则为ACCEPT

iptables  -t  filter  -P  INPUT  ACCEPT

二、filter过滤和转发控制

主机型防火墙,主要保护的是服务器本机(过滤威胁本机的数据包)。
网络防火墙,主要保护的是防火墙后面的其他服务器,如web服务器、FTP服务器等。

iptables防火墙可以根据很多规则进行过滤行为

centos7防火墙升级命令 centos7.5防火墙配置_IP_04

过滤条件.png

1)主机型防火墙

iptables -I INPUT -p tcp --dport 80 -j REJECT
iptables -I INPUT -s 192.168.2.100 -j REJECT
iptables -I INPUT -d 192.168.2.5 -p tcp --dport 80 -j REJECT
iptables -I INPUT -i eth0 -p tcp --dport 80 -j REJECT

丢弃192.168.2.0/24网络中所有主机发送给本机的所有数据包:

iptables  -A  INPUT -s  192.168.2.0/24  -j  DROP

拒绝114.212.33.12使用tcp协议远程连接本机ssh(22端口):

iptables -A  INPUT -s  114.212.33.12  -p tcp --dport 22 -j  REJECT

2)网络型防火墙

要把proxy主机的路由转发功能打开

centos7防火墙升级命令 centos7.5防火墙配置_html_05

网络型防火墙.png

1)client主机配置IP、添加网关(网卡名称仅供参考
注意:如果client主机有2网段IP的网卡,必须要关闭该网卡

nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.10/24 autoconnect yes
nmcli connection modify eth0 ipv4.gateway 192.168.4.5
nmcli connection up eth0
iptables -F

2)proxy主机配置IP、添加网关、开启路由转发(网卡名称仅供参考)

nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.4.5/24 autoconnect yes
nmcli connection up eth0
nmcli connection modify eth1 ipv4.method manual ipv4.addresses 192.168.2.5/24 autoconnect yes
nmcli connection up eth1
iptables -F

Linux内核默认支持软路由功能,通过修改内核参数即可开启或关闭路由转发功能。

echo 0 > /proc/sys/net/ipv4/ip_forward            #关闭路由转发
echo 1 > /proc/sys/net/ipv4/ip_forward            #开启路由转发

#注意以上操作仅当前有效,计算机重启后无效

#修改/etc/sysctl.conf配置文件,可以实现永久有效规则

echo 'net.ipv4.ip_forward=1' >> /etc/sysctl.conf

3)web1主机配置IP、添加网关(网卡名称仅供参考)
注意:如果web1主机有4网段IP的网卡,必须要关闭该网卡

nmcli connection modify eth0 ipv4.method manual ipv4.addresses 192.168.2.100/24 autoconnect yes
nmcli connection modify eth0 ipv4.gateway 192.168.2.5
nmcli connection up eth0

4)确认不同网络的联通性

ping 192.168.2.100
ping 192.168.4.10

5)在web1主机上启动http服务

yum -y install httpd
echo "test page" > /var/www/html/index.html
systemctl restart httpd

没有防火墙的情况下client访问web服务(成功)

curl http://192.168.2.100

设置proxy主机的防火墙规则,保护防火墙后面的Web服务器

iptables -I FORWARD -s 192.168.4.10 -p tcp --dport 80 -j DROP

设置完防火墙规则后,再次使用client客户端访问测试效果(失败)

curl http://192.168.2.100

三、禁ping的相关策略

ping的流程(A主机pingB主机):

centos7防火墙升级命令 centos7.5防火墙配置_html_06

ping.png

禁止其他主机ping本机,允许本机ping其他主机,仅禁止入站的ping请求,不拒绝入站的ping回应包。

iptables  -A  INPUT  -p icmp  --icmp-type echo-request  -j  DROP

注意:关于ICMP的类型,可以参考help帮助,参考命令如下:

iptables -p icmp --help

所有iptables规则都是临时规则,如果需要永久保留规则需要执行如下命令:
安装iptables-services并启动服务,保存防火墙规则。

yum -y install iptables-services
systemctl start iptables.service
systemctl enable iptables.service
service  iptables save