当前位置:   article > 正文

iptables防火墙详解、相关命令示例_iptables命令详解

iptables命令详解

目录

Linux包过滤防火墙

包过滤的工作层次

iptables的链结构

规则链

默认包括5中规则链(对数据包控制的时机)

iptables的表结构

规则表

默认包括4个规则表

数据包过滤的匹配流程

规则表之间的顺序

规则链之间的顺序

规则链内的匹配顺序

匹配流程示意图

入站

出站

转发

语法书写格式分类

通用匹配

隐含匹配

显式匹配

示例

(1)从filter表的INPUT链,添加一条匹配规则:拒绝icmp访问

(2)从filter表的INPUT链中删除指定匹配的规则,添加丢弃规则

(3)列出iptable指定链中的规则

(4)只指定链而不指定表添加规则,默认添加到filter表中


Linux包过滤防火墙

  • netfilter
    • 位于Linux内核中的包过滤功能体系
    • 成为Linux防火墙的“内核态”
  • iptables
    • 位于/sbin/iptables,用来管理防火墙规则的工具
    • 称为Linux防火墙的“用户态”

包过滤的工作层次

  • 主要在网络层,针对IP数据包
  • 体现在对数据包内的IP地址、端口等信息的处理上

iptables的链结构

  • 规则链

    • 规则的作用:对数据包进行过滤或处理
    • 链的作用:容纳各种防火墙规则
    • 链的分类依据:处理数据包的不同时机
  • 默认包括5中规则链(对数据包控制的时机)

    • INPUT:处理入站数据包
    • OUTPUT:处理出站数据包
    • FORWARD:处理转发数据包
    • PREROUTING:在进行路由选择前处理数据包
    • POSTROUTING链:在进行路由选择后处理数据包
  • 入站 ——》路由 ——》NAT转换 ——》出站

iptables的表结构

  • 规则表

    • 表的作用:容纳各种规则链
    • 表的划分依据:防火墙规则的作用相似
  • 默认包括4个规则表

    • raw表:确定是否对该数据包进行状态跟着
    • mangle:为数据包设置标记
    • nat表:修改数据包中的源,目标IP地址或端口(用于地址转换)
    • filter表:确定是否放行该数据包(过滤)

数据包过滤的匹配流程

  • 规则表之间的顺序

    • raw ——》mangle ——》nat ——》filter
  • 规则链之间的顺序

    • 入站:PREROUTING ——》INPUT
    • 出站:OUTPUT ——》POSTROUTING
    • 转发:PREROUTING ——》FORWARD ——》POSTROUTING
  • 规则链内的匹配顺序

    • 按顺序依次检查,匹配即停止(LOG策略例外)
    • 若找不到相匹配的规则,则按照该链的默认策略处理

匹配流程示意图

入站

出站

转发


语法书写格式分类

按照匹配条件进行分类

  1. 通用匹配

    1. 协议匹配:iptables -t filter -I INPUT -p icmp -j DROP
    2. 地址匹配:iptables -t filter -I FORWARD -s 192.168.10.102 -j DROP
    3. 接口匹配:iptables -t filter -I FORWARD -i ens33 -j DROP
  2. 隐含匹配

    1. 端口匹配:iptables -A FORWARD -s 192.168.20.0/24 -p udp --dport 53 -j DROP
    2. icmp类型匹配:iptables -t filter -I INPUT -p icmp --icmp-type 8 -j DROP
      1. 应答(reply)-- 0:我要ping别人
      2. 请求(request)-- 8:别人不能ping我
      3. 不可达(unreachable)-- 3:禁止别人对你的主机进行 ICMP 目标不可达的回复
  3. 显式匹配

    1. 多端口:iptables -A INPUT -p tcp -m multiport --dport 20,21,80,443 -j ACCEPT
    2. ip范围:iptables -A FORWARD -m iprange --src-range 192.168.20.10-192.168.20.20 -j DROP
    3. MAC地址:iptables -A INPUT -m mac --mac-source 00:0c:29:76:02:95 -j DROP
    4. 状态匹配:iptables -A INPUT -p tcp -m state --state ESTABLISHED -j DROP

示例

使用yum -y install iptables-services命令安装iptables管理工具

使用systemctl start iptables命令启动iptables服务

(1)从filter表的INPUT链,添加一条匹配规则:拒绝icmp访问

在INPUT后面加上编号,表示将该规则添加链中的到第几行

  1. [root@localhost ~]# iptables -t filter -I INPUT -p icmp -j REJECT
  2. [root@localhost ~]# iptables -t filter -I INPUT 3 -p icmp -j REJECT

(2)从filter表的INPUT链中删除指定匹配的规则,添加丢弃规则

  1. [root@localhost ~]# iptables -t filter -D INPUT -p icmp -j REJECT
  2. [root@localhost ~]# iptables -t filter -I INPUT -p icmp -j DROP

删除默认表中INPUT链的第几行规则

[root@localhost ~]# iptables -D INPUT 3

清空默认表

[root@localhost ~]# iptables -F

清空指定表

[root@localhost ~]# iptables -F -t nat

(3)列出iptable指定链中的规则

  1. [root@localhost ~]# iptables -L INPUT
  2. Chain INPUT (policy ACCEPT)
  3. target prot opt source destination
  4. ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
  5. REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
  6. ACCEPT icmp -- anywhere anywhere
  7. ACCEPT all -- anywhere anywhere
  8. ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
  9. REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

列出指定链中的规则,并显示行号

  1. [root@localhost ~]# iptables -L INPUT --line-numbers
  2. Chain INPUT (policy ACCEPT)
  3. num target prot opt source destination
  4. 1 ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
  5. 2 REJECT icmp -- anywhere anywhere reject-with icmp-port-unreachable
  6. 3 ACCEPT icmp -- anywhere anywhere
  7. 4 ACCEPT all -- anywhere anywhere
  8. 5 ACCEPT tcp -- anywhere anywhere state NEW tcp dpt:ssh
  9. 6 REJECT all -- anywhere anywhere reject-with icmp-host-prohibited

列出指定链中的规则,将source以数字形式显示

  1. [root@localhost ~]# iptables -nL INPUT
  2. Chain INPUT (policy ACCEPT)
  3. target prot opt source destination
  4. ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 state RELATED,ESTABLISHED
  5. REJECT icmp -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-port-unreachable
  6. ACCEPT icmp -- 0.0.0.0/0 0.0.0.0/0
  7. ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
  8. ACCEPT tcp -- 0.0.0.0/0 0.0.0.0/0 state NEW tcp dpt:22
  9. REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited

(4)只指定链而不指定表添加规则,默认添加到filter表中

[root@localhost ~]# iptables -I INPUT -p icmp -j REJECT

声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号