• iptables初步接触


    0、iptables命令选项输入顺序:
    iptables -t 表名 <-A/I/D/R> 规则链名 [规则号] <-i/o 网卡名> -p 协议名 <-s 源IP/源子网> --sport 源端口 <-d 目标IP/目标子网> --dport 目标端口 -j 动作
    添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。
    表名包括:
    raw:高级功能,如:网址过滤。
    mangle:数据包修改(QOS),用于实现服务质量。
    net:地址转换,用于网关路由器。
    filter:包过滤,用于防火墙规则。
    规则链名包括:
    INPUT链:处理输入数据包。
    OUTPUT链:处理输出数据包。
    PORWARD链:处理转发数据包。
    PREROUTING链:用于目标地址转换(DNAT),路由前。
    POSTOUTING链:用于源地址转换(SNAT),路由后

    1、查看
    iptables -nvL --line-numbers
    -L 查看当前表的所有规则,默认查看的是filter表,如果要查看NAT表,可以加上-t NAT参数
    -n 不对ip地址进行反查,加上这个参数显示速度会快很多
    -v 输出详细信息,包含通过该规则的数据包数量,总字节数及相应的网络接口
    --line-numbers 显示规则的序列号,这个参数在删除或修改规则时会用到

    2、添加
    添加规则有两个参数:-A和-I。其中-A是添加到规则的末尾;-I可以插入到指定位置,没有指定位置的话默认插入到规则的首部。
    当前规则:

    [root@test ~]# iptables -nL --line-numbers
    Chain INPUT (policy ACCEPT)
    num target prot opt source destination
    1 DROP all -- 192.168.1.1 0.0.0.0/0
    2 DROP all -- 192.168.1.2 0.0.0.0/0
    3 DROP all -- 192.168.1.4 0.0.0.0/0
    添加一条规则到尾部:
    [root@test ~]# iptables -A INPUT -s 192.168.1.5 -j DROP

    再插入一条规则到第三行,将行数直接写到规则链的后面:
    [root@test ~]# iptables -I INPUT 3 -s 192.168.1.3 -j DROP
    查看:

    [root@test ~]# iptables -nL --line-numbers
    Chain INPUT (policy ACCEPT)
    num target prot opt source destination
    1 DROP all -- 192.168.1.1 0.0.0.0/0
    2 DROP all -- 192.168.1.2 0.0.0.0/0
    3 DROP all -- 192.168.1.3 0.0.0.0/0
    4 DROP all -- 192.168.1.4 0.0.0.0/0
    5 DROP all -- 192.168.1.5 0.0.0.0/0
    可以看到192.168.1.3插入到第三行,而原来的第三行192.168.1.4变成了第四行。
    添加多个端口
    iptables -I INPUT -p tcp -m multiport --ports 5900:6100 -m comment --comment "vnc ports" -j ACCEPT

    3、删除
    删除用-D参数
    删除之前添加的规则(iptables -A INPUT -s 192.168.1.5 -j DROP):
    [root@test ~]# iptables -D INPUT -s 192.168.1.5 -j DROP
    有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用--line-numbers找出该条规则的行号,再通过行号删除规则。
    [root@test ~]# iptables -nL --line-number
    Chain INPUT (policy ACCEPT)
    num target prot opt source destination
    1 DROP all -- 192.168.1.1 0.0.0.0/0
    2 DROP all -- 192.168.1.2 0.0.0.0/0
    3 DROP all -- 192.168.1.3 0.0.0.0/0

    删除第二行规则
    [root@test ~]# iptables -D INPUT 2

    4、修改
    修改使用-R参数
    先看下当前规则:
    [root@test ~]# iptables -nL --line-numbers
    Chain INPUT (policy ACCEPT)
    num target prot opt source destination
    1 DROP all -- 192.168.1.1 0.0.0.0/0
    2 DROP all -- 192.168.1.2 0.0.0.0/0
    3 DROP all -- 192.168.1.5 0.0.0.0/0
    将第三条规则改为ACCEPT:

    [root@test ~]# iptables -R INPUT 3 -j ACCEPT
    再查看下:

    [root@test ~]# iptables -nL --line-numbers
    Chain INPUT (policy ACCEPT)
    num target prot opt source destination
    1 DROP all -- 192.168.1.1 0.0.0.0/0
    2 DROP all -- 192.168.1.2 0.0.0.0/0
    3 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
    第三条规则的target已改为ACCEPT。

    5、清理防火墙
    iptables -F

  • 相关阅读:
    COGS727 [网络流24题] 太空飞行计划
    Bzoj1692 洛谷P2870 [Usaco2007 Dec]队列变换
    Bzoj1029 [JSOI2007]建筑抢修
    Bzoj3168 [Heoi2013]钙铁锌硒维生素
    Bzoj4566 [Haoi2016]找相同字符
    Bzoj4771 七彩树
    Bzoj2597 [Wc2007]剪刀石头布
    Bzoj4773 负环
    HDU5772 String problem
    Bzoj1324 Exca王者之剑
  • 原文地址:https://www.cnblogs.com/potato-chip/p/11737985.html
Copyright © 2020-2023  润新知