首页 iptables教程iptables 用法说明

iptables 规则说明

iptables 匹配条件

运维派隶属马哥教育旗下专业运维社区,是国内成立最早的IT运维技术社区,欢迎关注公众号:yunweipai
领取学习更多免费Linux云计算、Python、Docker、K8s教程关注公众号:马哥linux运维

iptables 用法说明

帮助:man 8 iptables

格式:

iptables [-t table] {-A|-C|-D} chain rule-specification
iptables [-t table] -I chain [rulenum] rule-specification
iptables [-t table] -R chain rulenum rule-specification
iptables [-t table] -D chain rulenum
iptables [-t table] -S [chain [rulenum]]
iptables [-t table] {-F|-L|-Z} [chain [rulenum]] [options...]
iptables [-t table] -N chain
iptables [-t table] -X  [chain]
iptables [-t table] -P chain target
iptables [-t table] -E old-chain-name new-chain-name

rule-specification = [matches...]  [target]
match = -m matchname [per-match-options]
target = -j targetname [per-target-options]

范例:Filter表中INPUT规则

iptables 用法说明插图

iptables命令格式详解:

iptables   [-t table]   SUBCOMMAND   chain   [-m matchname [per-match-options]]   -j targetname [per-target-options]

1、-t table:指定表

raw, mangle, nat, [filter]默认

2、SUBCOMMAND:子命令

链管理类:

-N:new, 自定义一条新的规则链
-E:重命名自定义链;引用计数不为0的自定义链不能够被重命名,也不能被删除
-X:delete,删除自定义的空的规则链
-P:Policy,设置默认策略;对filter表中的链而言,其默认策略有:ACCEPT:接受, DROP:丢弃

范例:

[root@centos8 ~]#iptables -N web_chain 
[root@centos8 ~]#iptables -N web_chain -t nat
[root@centos8 ~]#iptables -X web_chain -t nat
[root@centos8 ~]#iptables -E web_chain WEB_CHAIN
[root@centos8 ~]#iptables -A WEB_CHAIN -s 10.0.0.6 -p tcp -m multiport  --dports 80,443 -j REJECT
[root@centos8 ~]#iptables -R WEB_CHAIN 1  -s 10.0.0.6 -p tcp -m multiport  --dports 80,443,8080 -j REJECT
[root@centos8 ~]#iptables -vnL  WEB_CHAIN
Chain WEB_CHAIN (1 references)
 pkts bytes target     prot opt in     out     source               destination         
    1    60 REJECT     tcp  --  *      *       10.0.0.6             0.0.0.0/0            multiport dports 80,443,8080 reject-with icmp-port-unreachable

[root@centos8 ~]#iptables -AINPUT -j WEB_CHAIN 
[root@centos8 ~]#iptables -X WEB_CHAIN
iptables v1.8.2 (nf_tables):  CHAIN_USER_DEL failed (Device or resource busy): chain WEB_CHAIN
[root@centos8 ~]#iptables -F  WEB_CHAIN
[root@centos8 ~]#iptables -X WEB_CHAIN
iptables v1.8.2 (nf_tables):  CHAIN_USER_DEL failed (Device or resource busy): chain WEB_CHAIN
[root@centos8 ~]#iptables -D INPUT 1
[root@centos8 ~]#iptables -X WEB_CHAIN

查看类:

-L:list, 列出指定鏈上的所有规则,本选项须置后
-n:numberic,以数字格式显示地址和端口号
-v:verbose,详细信息
-vv  更详细
-x:exactly,显示计数器结果的精确值,而非单位转换后的易读值 
--line-numbers:显示规则的序号
-S  selected,以iptables-save 命令格式显示链上规则

常用组合:
-vnL
-vvnxL –line-numbers

规则管理类:

-A:append,追加
-I:insert, 插入,要指明插入至的规则编号,默认为第一条
-D:delete,删除
       (1) 指明规则序号
       (2) 指明规则本身
-R:replace,替换指定链上的指定规则编号
-F:flush,清空指定的规则链
-Z:zero,置零
       iptables的每条规则都有两个计数器
        (1) 匹配到的报文的个数
        (2) 匹配到的所有报文的大小之和

范例:

[root@centos8 ~]#iptables -F OUTPUT

3、chain:

PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING

4、匹配条件

  • 基本:通用的,PARAMETERS
  • 扩展:需加载模块,MATCH EXTENTIONS

5、处理动作:

-j targetname [per-target-options]

简单:ACCEPT,DROP

扩展:

REJECT:–reject-with:icmp-port-unreachable默认
RETURN:返回调用链
REDIRECT:端口重定向
LOG:记录日志,dmesg
MARK:做防火墙标记
DNAT:目标地址转换
SNAT:源地址转换
MASQUERADE:地址伪装
自定义链

本文链接:https://www.yunweipai.com/35053.html

iptables 规则说明

iptables 匹配条件

网友评论comments

发表回复

您的电子邮箱地址不会被公开。

暂无评论

Copyright © 2012-2022 YUNWEIPAI.COM - 运维派 京ICP备16064699号-6
扫二维码
扫二维码
返回顶部