(Proudly powered by QKQ)
在学习Kubernetes的时候,kubernetes的proxy的实现方式,其中一种就是iptable。因此需要学习一下iptable相关指令。
Q: iptables指令是用来干啥的?
A: 先来linux man的说明:
Iptables and ip6tables are used to set up, maintain, and inspect the tables of IPv4 and IPv6 packet filter rules in the Linux kernel.
简单来说,就是一个工具,用来建立、维护、查看Linux内核中的IPv4和IPv6的网络包的过滤规则(filter rules)。
再来看另一个定义[2]:
iptables is an application that allows users to configure specific rules that will be enforced by the kernel’s netfilter framework. It acts as a packet filter and firewall that examines and directs traffic based on port, protocol and other criteria.
即iptables是一个应用,该应用可以配置Linux内核中netfilter框架使用的规则(rules)。它可以通过检查端口、协议及其他条件来实现包过滤器(packet filter)和防火墙的功能。
Q: iptables怎么用?
A: 首先得知道几个概念:
Several different tables may be defined. Each table contains a number of built-in chains and may also contain user-defined chains.
Each chain is a list of rules which can match a set of packets. Each rule specifies what to do with a packet that matches. This is called a `target', which may be a jump to a user-defined chain in the same table.
这段描述中有几个概念:
- table,即表
- chain,链条
- rule,规则
- packet,即网络包
- target,目标
这几个概念之间的关系是:
- 系统中可以有若干张表(table)
- 每张表可以包含多个默认的chains以及用户自定义的chain
- 每个chain里面可以包含若干个规则(rules)
- 每个规则定义了对符合某个条件的网络包的操作,这些操作称为target,比如对符合条件的网络包,跳转到另一个用户定义的chain,这个操作就是一个target
来张图吧:
Q: table有哪些?
A: linux中当前有五个table:
- filter。默认的table。即不查询其他table的packet都会查询此table。
- nat。当一个网络包要创建一个新的链接时会查询此table。
- mangle。用来改变指定的网络包。
- raw。主要用来避免connection tracking。raw表具有最高的优先级。
- security。用于MAC(Mandatory Access Control)网络规则。MAC由Linux的安全模块如SELinux实现。security table会在filter table之后使用。
其中每个table里面的chain有:
其中的box应该就是指路由功能这样一个黑盒。
Q: 如何使用iptables命令?
A: 首先,需要指定table,使用iptables -L <table_name>
来指定哪个table。
之后的命令行参数,可以分为几类:
- COMMANDS。命令,指定了你想要完成的操作。
- -A, --append chain rule-specification。在chain的最后增加一个或者多个rules。
- -C, --check chain rule-specification。判断该规则是否存在于chain中。
- -D, --delete chain rule-specification | rulenum。删除rule
- -I, --insert chain [rulenum] rule-specification。插入一个或者多个rules.
- -R, --replace chain rulenum rule-specification。替换某个rule。
- -L,--list chain,查看所有该chain的rules,如果没有指定chain,则列出所有chain的所有rules。
- -S, --list-rules chain。列出该chain的所有rules,如果没有指定chain,则列出所有。
- -F, --flush chain。flush该chain的所有rules,不指定chain,就flush该table中的所有chain中的所有rules。flush同一条一条删除是一样的。
- ...
- PARAMETERS。参数,下列参数组成了规则的定义(rule specification)。
- -4, --ipv4
- -6, --ipv6
- -p, --protocol protocol,指定rule要去检查的协议,可能的值有:tcp, udp, udplite, icmp, all等
- -s, --source address[/mask]
- -d, --destination address[/mask]
- -m, --match
- -j, --jump target。即如果match了,应该调到哪儿去。可以跟DROP, ALLOW, REJECT。分别表示丢弃、允许、拒绝。
- -g, --goto chain。表示继续到另一个用户定义的chain中去处理
- -i, --in-interface name。指定进入哪个network interface
- -o, --out-interface name。指定出去的时候是哪个interface
当table中没有任何rules的时候,默认是不对包做过滤的。
Q: iptables中的-m是如何使用的?
A: 这部分的内容在man iptables中没有,需要使用man iptables-extensions来查看具体的module。
其使用格式为:
iptables [-m name [module_options...]] -j target_name [target-options...]]
举个例子:
# 插入一个rule,使用了comment模块,--comment为该模块的options,添加了一个注释
iptables -A INPUT -i eth1 -m comment --comment "my local LAN"
Q: 来点例子?
A:
比如:
iptables -I INPUT -s 198.51.100.0 -j DROP
往默认的filter表中的chain的头部插入一个rule。该rule表示所有源地址为198.51.100.0的包全都丢弃(DROP)
比如:
sudo iptables -L -nv --line-numbers
查看filter表中的所有rules,并显示行号。其中-n表示不做domain的查询。
最后,贴一张网上的图[3],有些帮助:
参考资料:
[1] linux man page
[2] https://www.linode.com/docs/security/firewalls/control-network-traffic-with-iptables/
[3] https://msazure.club/kubernetes-services-and-iptables/