iptables基本命令

查询规则
iptables -L
# 默认查询filter表中的所有链的规则

iptables --line -v -t 表名 -L 链名
# 查询指定表中的指定链的规则
# --line: 显示行号
# -v: 显示详细信息
# -n: 不对规则中的IP或者端口进行反解(提高命令执行速度)
[root@hdz-iptables1 ~]# iptables --line -v -t filter -L INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1     4095  306K ACCEPT     all  --  any    any     anywhere             anywhere             ctstate RELATED,ESTABLISHED
2        1    80 ACCEPT     all  --  lo     any     anywhere             anywhere
3    53965 2933K INPUT_direct  all  --  any    any     anywhere             anywhere
4    53965 2933K INPUT_ZONES_SOURCE  all  --  any    any     anywhere             anywhere
5    53965 2933K INPUT_ZONES  all  --  any    any     anywhere             anywhere
6        1    84 ACCEPT     icmp --  any    any     anywhere             anywhere
7    53962 2933K REJECT     all  --  any    any     anywhere             anywhere             reject-with icmp-host-prohibited
增加规则

规则由匹配条件和处理动作组成,并且为顺序执行,一但满足一条规则就会执行对应的动作,不再继续匹配后面的规则。(即前面规则会覆盖后面规则)

  • 常用匹配条件:原地址、目标地址、源端口,目标端口
  • 常用动作有:ACCEPT(接受)、DROP(丢弃),REJECT(拒绝)
iptables -t 表名 -I 链名 1 -s 10.10.60.5 -j DROP
# 插入一条规则
# 1:规则ID,可以不指定
# -s:匹配条件——源地址
# -j:处理动作,DROP(丢弃)

iptables -t 表名 -A 链名 -s 10.10.60.5 -j ACCEPT
# 追加一条规则
# -s:匹配条件——源地址
# -j:处理动作,ACCEPT(接受)
[root@hdz-iptables1 ~]# iptables -t filter -A INPUT -s 10.10.60.5 -j ACCEPT
[root@hdz-iptables1 ~]# iptables --line -vn -t filter -L INPUT
Chain INPUT (policy ACCEPT 9 packets, 636 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        6   504 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0


[root@hdz-iptables1 ~]# iptables -t filter -I INPUT 1 -s 10.10.60.5 -j DROP
[root@hdz-iptables1 ~]# iptables --line -vn -t filter -L INPUT
Chain INPUT (policy ACCEPT 7 packets, 488 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        2   168 DROP       all  --  *      *       10.10.60.5           0.0.0.0/0
2       17  1428 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0
删除规则

删除规则可以通过编号(ID)删除,也可以通过匹配条件和动作删除,更暴力的可以删除整个表或者整条链的规则

iptables -t 表名 -D 链名 1
# 根据ID删除一条规则
# 1:匹配的ID

iptables -t 表名 -D 链名  -s 10.10.60.5 -j ACCEPT
# 根据匹配条件和动作删除一条规则
# -s:匹配条件——源地址
# -j:处理动作,ACCEPT(接受)

iptables -t 表名 -F 链名
# 清空(flush)某个表中某个链的所有规则(慎用!)

iptables -t 表名 -F
# 清空(flush)某个表中所有链的规则(慎用!)
[root@hdz-iptables1 ~]# iptables --line -vn -t filter -L INPUT
Chain INPUT (policy ACCEPT 7 packets, 488 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        2   168 DROP       all  --  *      *       10.10.60.5           0.0.0.0/0
2       17  1428 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0

[root@hdz-iptables1 ~]# iptables -t filter -D INPUT 1

[root@hdz-iptables1 ~]# iptables --line -vn -t filter -L INPUT
Chain INPUT (policy ACCEPT 7 packets, 488 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1       19  1596 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0


[root@hdz-iptables1 ~]# iptables --line -vn -t filter -L INPUT
Chain INPUT (policy ACCEPT 29 packets, 2040 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        4   336 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0

[root@hdz-iptables1 ~]# iptables -t filter -D INPUT -s 10.10.60.5 -j ACCEPT

[root@hdz-iptables1 ~]# iptables --line -vn -t filter -L INPUT
Chain INPUT (policy ACCEPT 9 packets, 656 bytes)
num   pkts bytes target     prot opt in     out     source               destination
修改规则

可通过ID和匹配条件来修改规则表中的规则,但修改操作容易出现问题,其实可以通过先删除后添加来达到同样修改的效果
默认规则是在报文未匹配到某个表中任何条件的时候使用的默认处理规则,也可以通过命令修改某个表的默认规则

iptables -t 表名 -R 链名 1 -s 10.10.60.5 -j ACCEPT
# 根据ID修改一条规则,必须指定匹配条件,否则会把规则的条件也会修改成默认的条件,下面实例中可以看到例子
# 1:匹配的ID
# -s:匹配条件——源地址
# -j:处理动作,ACCEPT(接受)

iptables -t 表名 -P 链名 DROP
# 修改表的默认规则
# DROP:处理动作
[root@hdz-iptables1 ~]# iptables --line  -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 DROP       all  --  *      *       10.10.60.5           0.0.0.0/0

[root@hdz-iptables1 ~]# iptables -t filter -R INPUT 1 -j ACCEPT
[root@hdz-iptables1 ~]# iptables --line  -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1       10   896 ACCEPT     all  --  *      *       0.0.0.0/0            0.0.0.0/0

[root@hdz-iptables1 ~]# iptables -t filter -R INPUT 1 -s 10.10.60.5 -j ACCEPT
[root@hdz-iptables1 ~]# iptables --line  -nvL INPUT
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1        0     0 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0

[root@hdz-iptables1 ~]# iptables -t filter -P INPUT DROP
[root@hdz-iptables1 ~]# iptables --line -t filter -nvL INPUT
Chain INPUT (policy DROP 0 packets, 0 bytes)
num   pkts bytes target     prot opt in     out     source               destination
1      310 23371 ACCEPT     all  --  *      *       10.10.60.5           0.0.0.0/0
保存规则

通过命令修改的规则是临时的,在服务器重启后就会失效,因此需要保存并持久化规则。

iptables-save:导出当前iptables配置,打印的屏幕,可以通过重定向来得到规则配置文件
iptables-restore:应用导出的iptables配置文件

有几种方式可以持久化规则配置

  • 修改/etc/sysconfig/iptables
    在iptables重启的时候,此配置文件会被应用,作为默认配置,可以通过命令iptables-save输出当前iptables配置,重定向到/etc/sysconfig/iptables配置文件,这样当前配置就会变成iptables启动的默认配置

  • 通过service
    CentOS7以前使用init方式管理服务,因此centos6等可以使用(service iptables save)命令来持久化iptables规则,对于CentOS7的则需要安装iptables-service来使用此命令

[root@hdz-iptables1 ~]# iptables-save
# Generated by iptables-save v1.4.21 on Sun Jun 17 01:14:36 2018
*filter
:INPUT ACCEPT [86:6108]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [99:16008]
-A INPUT -s 10.10.60.5/32 -j ACCEPT
COMMIT
# Completed on Sun Jun 17 01:14:36 2018


[root@hdz-iptables1 ~]# cat /etc/sysconfig/iptables
# Generated by iptables-save v1.4.21 on Sun Jun 17 01:12:15 2018
*filter
:INPUT ACCEPT [73:5204]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [69:10468]
-A INPUT -s 10.10.60.5/32 -j ACCEPT
COMMIT
# Completed on Sun Jun 17 01:12:15 2018
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值