本篇写的就是防火墙的ip tables的管理方式,在这之前,我们需要了解一下ip tables的定义
一、定义
防火墙在做数据包过滤决定时,有一套遵循和组成的规则,这些规则存储在专用的数据包过滤表中,而这些表集成在 Linux 内核中。在数据包过滤表中,规则被分组放在我们所谓的链(chain)中。而netfilter/iptables IP 数据包过滤系统是一款功能强大的工具,可用于添加、编辑和移除规则。
iptables 组件是一种工具,也称为用户空间(userspace),它使插入、修改和除去信息包过滤表中的规则变得容易。
ip tables中有三种表格,分别是raw,mangle,nat,filter
规则表的名称:
raw:高级功能,如:网址过滤。
mangle:数据包修改(QOS),用于实现服务质量。
nat:地址转换,用于网关路由器。
filter:包过滤,用于防火墙规则。
规则链名包括:
INPUT链:处理输入数据包。
OUTPUT链:处理输出数据包。
PORWARD链:处理转发数据包。
PREROUTING链:用于目标地址转换(DNAT)。
POSTOUTING链:用于源地址转换(SNAT)。
二、使用
首先设置项系统为ip tables方式管理火墙
systemctl stop firewalld.service ##停止firewalld服务
systemctl mask firewalld.service ##注销firewalld服务
systemctl disable firewalld.service ##禁止firewalld开机自启
yum install iptables-services -y ##一般都已经安装了
systemctl start iptables.service ##开始iptables服务
systemctl enable iptables.service ##设置iptables服务开机自启
1)查看ip tables
iptables -L -n -v ##查看iptables设置
iptables -t nat -nL ##查看iptables中nat设置
iptables -t filter -nL ##查看iptables中filter设置
iptables -t mangle -nL ##查看iptables中mangle设置
2)保存ip tables的规则
iptables的配置文件
vim /etc/sysconfig/iptables ##可以在文件内修改添加保存设置
如何删除和保存已经设置的iptables规则
iptables -F ##清除已有的规则
iptables -t nat -F INPUT ##清除nat表中的INPUT的规则
/etc/rc.d/init.d/iptables save ##保存设置好的规则
service iptables save ##保存设置好的规则
service iptables restart ##重启生效
systemctl restart iptables ##重启生效
3)添加修改删除表中的链
iptables -N nelws ##添加nelws链
iptables -E nelws NELWS ##修改nelws链路的名为NELWS
iptables -X NELWS ##删除NELWS链
4)表中规则的指令
iptables -A INPUT -s 172.25.254.2 -p tcp --dport 23 -j ACCEPT ##接受ip为172.25.254.2端口为23的访问
iptables -A INPUT -s ! 172.25.254.2 -p tcp --dport 23 -j REJECT ##只接受ip为172.25.254.2端口为23的访问
iptables -A INPUT -s 172.25.254.2 -p tcp --dport 23 -j DROP ##拒绝ip为172.25.254.2端口为23的访问,并且不回应
iptables -I INPUT 2 -p tcp --dport 23 -j DROP ##将这个策略插入到INPUT的第二条
iptables -D INPUT 3 ##删除INPUT中的第三条规则
iptables -D INPUT -s 172.25.254.2 -j DROP ##删除INPUT中包含172.25.254.2 -j DROP的内容
iptables -R INPUT 3 -j DROP ##将INPUT中第三条规则修改为-j DROP
iptables -P INPUT DROP ##设置INPUT中默认的规则为DROP
iptables -i eth0 ##从eth0口进入
iptables -o eth0 ##从eth0口出去
5)屏蔽ip
iptables -I INPUT -s 123.45.6.7 -j DROP #屏蔽单个IP的命令
iptables -I INPUT -s 123.0.0.0/8 -j DROP #封整个段即从123.0.0.1到123.255.255.254的命令
iptables -I INPUT -s 124.45.0.0/16 -j DROP #封IP段即从123.45.0.1到123.45.255.254的命令
iptables -I INPUT -s 123.45.6.0/24 -j DROP #封IP段即从123.45.6.1到123.45.6.254的命令是
6)源地址解析SNAT
iptables -t nat -A POSTROUTING -O eth0 -j SNAT --to-source 172.25.254.3 ##这个指令跟之前那篇firewalld中的端口转发效果是一样的,故不再赘述
7)目的地地址解析
iptables -t nat -A PREROUTING -i eth0 -j DNAT --to-dest 1.1.1.1 ##这个指令跟之前那篇firewalld中的伪装效果是一样的,故不再赘述
ok~!