netfilter/iptables 指南

扶醉桌前 提交于 2020-01-09 14:06:28

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

netfilter/iptables

简述历史

  • kernel 2.0.x IPfwadm
  • kernel 2.2.x IPchains
  • kernel 2.4.x Netfilter
  • kernel 3.13.x NFtables

iptables基础

netfilter/iptables 分别是内核态模块和用户态工具,管理员通过iptables给netfilter变更规则

netfilter/iptables 预设的表和链

  • 内置了五个表,分别是 filter, nat, mangle, raw, security 分别对应的内核模块是 /lib/modules/x.x.x/kernel/net/ipv4/netfilter/ 目录下的

<pre> iptable_filter.ko iptable_nat.ko iptable_mangle.ko iptable_raw.ko iptable_security.ko </pre>

  • 预设了五个chain 分别是 POSTROUTING, INPUT, FORWARD, OUTPUT, POSTROUTING 可以用通过 iptable -t table_name -L 来查看,详见 man iptables

iptables 的启用与停用

以 默认的filter为例

<pre> 启用filter表:modprobe iptable_filter 启用filter表:modprobe -r iptable_filter </pre>

命令基本格式

iptables 语法分成三部分:

命令 动作 条件准则 处置方式
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
  • iptables的基本操作

<pre> (iptable 命令默认约定 如果 -t 参数不指定 默认是 -t filter ) iptables -t filter -L # 列出filter表所有chain的规则 iptables -t filter -F # 清除所有默认 chain的规则 iptables -t filter -N # 新增自定义的chain iptables -t filter -X # 清除自定义的chain的以及对应的规则 iptables -t filter -P # </pre>

  • 查看规则 iptables-save

<pre> # Generated by iptables-save v1.4.12 on Tue Apr 22 14:55:51 2014 *filter :INPUT ACCEPT [1689:106559] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [1608:154769] -A INPUT -s 192.168.1.0/32 -p tcp -m multiport --dports 80 -j ACCEPT -A INPUT -p tcp -m multiport --dports 80 -j DROP COMMIT # Completed on Tue Apr 22 14:55:51 2014 # Generated by iptables-save v1.4.12 on Tue Apr 22 14:55:51 2014 *raw :PREROUTING ACCEPT [2498:166654] :OUTPUT ACCEPT [2298:219551] COMMIT # Completed on Tue Apr 22 14:55:51 2014 </pre>

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!