zoukankan      html  css  js  c++  java
  • iptables中增加/删除/查询/修改的基本操作

    虽然在Ubuntu使用了UFW来简化iptables的操作,但是UFW只针对防火墙方面,转发方面没有涉及,所以要弄懂其中的原理,还是必须回归到iptables中。CentOS也是如此。下面是针对iptables的基本操作,无论CentOS还是Ubuntu都是一致的。

    前提:先熟悉iptables的基本命令参数:http://www.cnblogs.com/EasonJim/p/6847874.html

    说明:经过测试,一些OUTPUT的规则使用下面命令是查看不到的,但是可以通过sudo iptables-save命令进行查看,是因为一些nat表的没给出来,可以使用sudo iptables -t tablename去查询,对于要知道哪些tablename时,可以使用man iptables进行查询。

    操作:

    1、查看

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

    2、添加

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

    当前规则:

    sudo 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.4          0.0.0.0/0

    添加一条规则到尾部:

    sudo iptables -A INPUT -s 192.168.1.5 -j DROP

    再插入一条规则到第三行,将行数直接写到规则链的后面:

    sudo iptables -I INPUT 3 -s 192.168.1.3 -j DROP

    查看:

    sudo 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
    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变成了第四行。

    3、删除

    删除用-D参数

    删除之前添加的规则(sudo iptables -A INPUT -s 192.168.1.5 -j DROP):

    iptables -D INPUT -s 192.168.1.5 -j DROP

    有时候要删除的规则太长,删除时要写一大串,既浪费时间又容易写错,这时我们可以先使用–line-number找出该条规则的行号,再通过行号删除规则。

    sudo 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

    删除第二行规则

    sudo iptables -D INPUT 2

    删除所有规则(慎用)

    sudo iptables -F 
    sudo iptables -X
    sudo iptables -Z
    #针对某张表(nat)进行删除
    sudo iptables -F -t nat
    sudo iptables -X -t nat
    sudo iptables -Z -t nat

    说明:要知道是哪张表时,可以使用sudo iptables-save输出全部规则,# Generated by下一行开头带星号的就是表面。

    技巧:比如一些nat转发的规则不会展示,则查询时可以使用sudo iptables -nL --line-number -t nat查询,然后通过sudo iptables -D INPUT 2 -t nat进行删除。

    4、修改

    修改使用-R参数

    先看下当前规则:

    sudo 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.5          0.0.0.0/0

    将第三条规则改为ACCEPT:

    sudo iptables -R INPUT 3 -j ACCEPT

    再查看下:

    sudo 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    ACCEPT     all  --  0.0.0.0/0            0.0.0.0/0

    第三条规则的target已改为ACCEPT。

    参考:

    http://blog.csdn.net/l241002209/article/details/43987933(以上内容转自此篇文章)

  • 相关阅读:
    OWIN katana注册中间件的几种写法
    ASP.NET Identity(处理身份数据存储) 与 OWIN主机(实现katana验证授权)原理概括
    entity framework 查询
    Sencha CMD 4- 安装与首次使用
    比较const ,readonly, stitac readonly
    (二)给IE6-IE9中的input添加HTML5新属性-placeholder
    (一)IE8以下background不起作用
    大虾翻译(一):jQuery.extend()
    JavaScript之三:jQuery插件开发(一)
    《JavaScript DOM编程艺术》
  • 原文地址:https://www.cnblogs.com/EasonJim/p/7594552.html
Copyright © 2011-2022 走看看