注意:firewalld和iptables不能同时打开,一定要关闭firewalld,再打开iptables,不然会对下面的实验有影响
防火墙的工作机制(三表五链)
链: 链就是位置,共有五个 : 数据包进路由之前(PREROUTING)、目的地址为本机,进系统(INPUT) 、转发(FORWARD)、原地址为本机,向外发送,出系统(OUTPUT)、发送到网卡之前,出路由(POSTROUTING); 数据包到了该链处,会去对应表中查询设置的规则,然后决定是否放行、丢弃、转发还是修改等等操作。每个链都是一个规则列表,对对应的包进行匹配。
表:具有相同功能的规则的集合叫做表。filter表:负责过滤功能,经过内核的; nat表:网络地址转换,不经过内核的 ,实现数据包转发,修改源地址 端口 目标地址 端口,实现地址转换;mangle表:拆解报文,作出修改,封装报文;raw表: 关闭nat表上启用的链接追踪机制。
表链关系: 我们所有自定义的规则都是这四种分类中的规则,或者说,所有的规则都存在于这4张表中。
filter表格:放的是经过内核的ip input output forward
nat表格:放的不是经过内核的服务 input output postrouting prerouting
备用表格mangle: input output forward postrouting prerouting
iptables的参数
含义 | 参数 |
---|---|
不作解析 | iptables -t |
列出指定表中的策略 | iptables -L |
增加策略 | iptables -A |
网络协议 | iptables -P |
端口 | iptables –dport |
数据来源 | iptables -s |
动作:ACCEPT允许;REJECT拒绝;DROP丢弃 | iptables -j |
增加链 | iptables -N |
修改链名称 | iptables -E |
删除链 | iptables -X |
删除指定策略 | iptables -D |
插入 | iptables -I |
修改策略 | iptables -R |
修改默认策略 | iptables -P |
##不作解析 查看filter表格里面的链( 不跟-t,默认为filter表格)——iptables -t filter -nL
[root@firewall_server ~]# systemctl stop firewalld.service
[root@firewall_server ~]# systemctl start iptables.service
[root@firewall_server ~]# iptables -t filter -nL
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
##作解析** 查看filter表格里面的链——iptables -t filter - L
[root@firewall_server ~]# iptables -t filter -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT icmp -- anywhere anywhere
ACCEPT all -- anywhere anywhere
ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain FORWARD (policy ACCEPT)
target prot opt source destination
REJECT all -- anywhere anywhere reject-with icmp-host-prohibited
Chain OUTPUT (policy ACCEPT)
target prot opt source