1 iptables安装
1.1 iptables和iptables-service的关系
iptables 是基于内核的,和 iptables-services 没有关系,不用安装任何工具包就可以使用 iptable 命令添加的防火墙规则,
但是iptables添加的规则是临时的,基于内存的,在系统重启后会消失,所以需要 iptables.service 服务来对添加的规则进行保存,
这样在系统重启后重载对应的防火墙规则。
1.2 安装iptables和iptables-service
第一步:1、关闭防火墙
[root@localhost ~]# systemctl status firewalld
[root@localhost ~]# systemctl stop firewalld
[root@localhost ~]# systemctl disable firewalld
第二步:查看 iptables 是否安装,安装iptables-services
[root@localhost ~]# rpm -qa iptables
iptables-1.4.21-34.el7.x86_64 # 系统默认装有 iptables
[root@localhost ~]# yum install iptables # 有新版本会安装最新版本
[root@localhost ~]# rpm -qa iptables
iptables-1.4.21-35.el7.x86_64
[root@localhost ~]# yum install iptables-services # 安装 iptables-services
[root@localhost ~]# rpm -qa iptables-services
iptables-services-1.4.21-35.el7.x86_64
第三步:启动iptables
[root@localhost ~]# systemctl enable iptables.service # 设置 iptables 开机启动
[root@localhost ~]# systemctl start iptables # 开启 iptables
[root@localhost ~]# systemctl status iptables # 查看 iptables 状态
● iptables.service - IPv4 firewall with iptables
Loaded: loaded (/usr/lib/systemd/system/iptables.service; disabled; vendor preset: disabled)
Active: active (exited) since Fri 2023-10-06 13:05:53 CST; 2s ago
第四步:#防火墙相关模块 加载到内核中,写入到开机自启动.
cat >> /etc/rc.local <<EOF
modprobe ip_tables
modprobe iptable_filter
modprobe iptable_nat
modprobe ip_conntrack
modprobe ip_conntrack_ftp
modprobe ip_nat_ftp
modprobe ipt_state
EOF
sysctl -p
2 什么是iptables
iptables是Linux的防火墙管理工具而已,真正实现防火墙功能的是Netfilter,我们配置了iptables规则后Netfilter通过这些规则来进行防火墙过滤等操作
Netfilter模块:
它是主要的工作模块,位于内核中,在网络层的五个位置(也就是防火墙四表五链中的五链)注册了一些钩子函数,用来抓取数据包;把数据包的信息拿出来匹配各个各个链位置在对应表中的规则:匹配之后,进行相应的处理ACCEPT、DROP等等。
2.1 iptables的工作流程
- 防火墙是层层过滤的,实际是按照配置规则的顺序从上到下,从前到后进行过滤的。
- 如果匹配成功规则,即明确表示是拒绝(DROP)还是接收(ACCEPT),数据包就不再向下匹配新的规则。
- 如果规则中没有明确表明是阻止还是通过的,也就是没有匹配规则,向下进行匹配,直到匹配默认规则得到明确的阻止还是通过。
- 防火墙的默认规则是所有规则都匹配完才会匹配的。
3 具体的四表五链
3.1 四表五链定义
四表:filter、nat、managle、raw,默认是filter表。表的处理优先级:raw>managle>nat>filter
四表 |
filter |
过滤数据包 |
nat |
网络地址转换(端口映射、地址映射等。) |
|
mangle |