一, iptables 命令
iptables 是一个规则管理工具. 具有添加, 修改, 删除和显示等功能.
规则和链都有计数器:
pkts: 由规则或链所匹配到的报文的个数
bytes: 由规则或链匹配到的所有报文大小之和
1.1 用法
用法:
- $ iptables [-t table] SUBCOMMAND CHAIN CERTERIA -j TARGET
- -t table:
- filter
- nat
- mangle
- raw
- SUBCOMMAND:
链管理:
-F: flush, 清空规则链, 省略链, 表示清空指定表上的所有链.
-N: new, 创建新的自定义规则链.
-X: drop, 删除用户自定义的规则链 (空链).
-Z: zero, 清零规则计数器.
-P: Policy, 为指定链设置默认策略, 对 filter 表中的链而言, 默认策略通常有 ACCEPT, DROP, REJECT.
-E: rEname, 重命名自定义链, 引用计数不为 0 的自定义链, 无法改名, 也无法删除.
规则管理:
-A: append, 将新规则追加于指定链的尾部.
-I: insert, 将新规则插入指定链的指定位置.
-D: delete, 删除指定链上的指定规则; 有两种指定方式, 指定匹配条件和指定规则编号.
-R: replace, 替换指定链上的指定规则.
-C: check, 检查指定链上的指定规则.
查看:
-L: list, 列出指定链上的所有规则.
-n: numberic, 以数字格式显示地址和端口号.
-v: verbose, 显示详细信息; -vv, -vvv.
--line-numbers: 显示规则编号.
-x: exactly, 显示计数器计数结果的精确值.
目标:
-j TARGET: jump 至指定的 TARGET.
ACCEPT: 接受
DROP: 丢弃
REJECT: 拒绝
RETURN: 返回调用链
REDIRECT: 端口重定向
LOG: 记录日志
MARK: 做防火墙标记
DNAT: 目标地址转换
SNAT: 源地址转换
MASQUERADE: 地址伪装
...: 更多请查看 man 文档.
匹配条件:
基本匹配:
[!] -s, --src, --source IP|NetAddr: 检查报文中源 IP 地址是否符合此处指定的范围, 前面加 "!" 为非.
[!] -d, --dst, --destination IP|NetAddr: 检查报文中源 IP 地址是否符合此处指定的地址范围.
[!] -p, --protocol {tcp|udp|icmp}: 检查报文中的协议, 即 ip 首部中的 protocol 所标识的协议.
[!] -i, --in-interface IFACE: 数据报文的流入接口, 仅能用于 PREROUTING, INPUT 以及 FORWARD 链上.
[!] -o, --out-interface IFACE: 数据报文的流出接口, 仅能用于 FORWARD, OUTPUT 以及 FORWARD 链上.
扩展匹配: -m macth_name --spec_options; 例如 - m tcp --dport 22
隐式扩展: 对 - p protocol 指明的协议进行扩展, 可省略 - m 选项
-p tcp:
--dport PORT[-PORT]: 目标端口, 可以是单个端口或多个连续的端口.
--sport PORT[-PORT]: 源端口.
--tcp-flags LIST1 LIST2: 检查 LIST1 所指明的所有标志位, 且这其中, LIST2 所表示出的所有标记为必须为 1, 而余下的必须为 0, 没有在 LIST1 中指明的不做检查. 标志位, SYN, ACK, FIN, RST, PSH, URG; 示例, --tcp-flags SYN,ACK,FIN,RST SYN, 匹配 tcp 三次握手的第一次.
--syn: 检查是否为新建 tcp 连接请求的第一次请求.
- -p udp:
- --dport
- --sport
- -p icmp:
--icmp-type: 可用数字表示其类型, 0(echo-reply) 和 8(echo-request)
显示扩展: 必须使用 - m 选项指定使用的扩展, 请查看 iptables 显示扩展
二, 示例
2.1 在 filter 表中创建一个新的链, 名字叫做 IN_putlic
- $ iptables -t filter -N IN_public
- $ iptables -t filter -L -n
- ...
- Chain IN_public (0 references)
- target prot opt source destination
2.2 修改 IN_putlic 链为 OUT_putlic
- $ iptables -t filter -E IN_putlic OUT_pulic
- $ iptables -t filter -L -n
- ...
- Chain OUT_pulic (0 references)
- target prot opt source destination
2.3 修改 filter 表上的 FORWARD 链的规则为 DROP
- $ iptables -t filter -P FORWARD DROP
- $ iptables -t filter -L -n
- ...
- Chain FORWARD (policy DROP)
- target prot opt source destination
- ...
2.4 打开 firewalld 服务, 删除 FORWARD 链上的第九条规则
- $ iptables -t filter -L -n --line-numbers
- Chain FORWARD (policy ACCEPT)
- num target prot opt source destination
- 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
- 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
- 3 FORWARD_direct all -- 0.0.0.0/0 0.0.0.0/0
- 4 FORWARD_IN_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
- 5 FORWARD_IN_ZONES all -- 0.0.0.0/0 0.0.0.0/0
- 6 FORWARD_OUT_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
- 7 FORWARD_OUT_ZONES all -- 0.0.0.0/0 0.0.0.0/0
- 8 DROP all -- 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
- 9 REJECT all -- 0.0.0.0/0 0.0.0.0/0 reject-with icmp-host-prohibited
- $ iptables -t filter -D FORWARD 9
- $ iptables -t filter -L -n --line-numbers
- ...
- Chain FORWARD (policy ACCEPT)
- num target prot opt source destination
- 1 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
- 2 ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
- 3 FORWARD_direct all -- 0.0.0.0/0 0.0.0.0/0
- 4 FORWARD_IN_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
- 5 FORWARD_IN_ZONES all -- 0.0.0.0/0 0.0.0.0/0
- 6 FORWARD_OUT_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
- 7 FORWARD_OUT_ZONES all -- 0.0.0.0/0 0.0.0.0/0
- 8 DROP all -- 0.0.0.0/0 0.0.0.0/0 ctstate INVALID
2.5 对访问本机 TCP 协议的报文通通放行
- $ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p tcp -j ACCEPT
- $ iptables -t filter -L -n
- Chain INPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.123.101
2.6 允许本机的所有 TCP 报文通通发出去
- iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p tcp -j ACCEPT
- $ iptables -t filter -L -n
- ...
- Chain OUTPUT (policy ACCEPT)
- target prot opt source destination
- ACCEPT tcp -- 192.168.123.101 0.0.0.0/0
2.7 将 filter 表上的所有链改为 DROP
- $ iptables -t filter -P INPUT DROP
- $ iptables -t filter -P FORWARD DROP
- $ iptables -t filter -P OUTPUT DROP
2.8 允许其他主机 ping 192.168.123.101 这台主机
- $ iptables -t filter -A INPUT -d 192.168.123.101 -p icmp -j ACCEPT
- $ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p icmp -j ACCEPT
- $ iptables -L -n -v
- Chain INPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 779 83096 ACCEPT tcp -- * * 0.0.0.0/0 192.168.123.101
- 0 0 ACCEPT icmp -- * * 0.0.0.0/0 192.168.123.101
- ...
- Chain OUTPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 458 64864 ACCEPT tcp -- * * 192.168.123.101 0.0.0.0/0
- 0 0 ACCEPT icmp -- * * 192.168.123.101 0.0.0.0/0
2.9 删除 filter 表上的 INPUT 和 OUTPUT 链上的第二条规则
- $ iptables -t filter -L -n --line-numbers
- Chain INPUT (policy DROP)
- num target prot opt source destination
- 1 ACCEPT tcp -- 0.0.0.0/0 192.168.123.101
- 2 ACCEPT icmp -- 0.0.0.0/0 192.168.123.101
- ...
- Chain OUTPUT (policy DROP)
- num target prot opt source destination
- 1 ACCEPT tcp -- 192.168.123.101 0.0.0.0/0
- 2 ACCEPT icmp -- 192.168.123.101 0.0.0.0/0
- $ iptables -t filter -D INPUT 2
- $ iptables -t filter -D OUTPUT 2
- $ iptables -t filter -L -n
- Chain INPUT (policy DROP)
- target prot opt source destination
- ACCEPT tcp -- 0.0.0.0/0 192.168.123.101
- ...
- Chain OUTPUT (policy DROP)
- target prot opt source destination
- ACCEPT tcp -- 192.168.123.101 0.0.0.0/0
2.10 允许报文从 ens33 接口中流入和流出
- $ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -i ens33 -j ACCEPT
- $ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -o ens33 -j ACCEPT
- $ iptables -L -n -v
- Chain INPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 1256 133K ACCEPT tcp -- * * 0.0.0.0/0 192.168.123.101
- 0 0 ACCEPT all -- ens33 * 0.0.0.0/0 192.168.123.101
- ...
- Chain OUTPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- 791 113K ACCEPT tcp -- * * 192.168.123.101 0.0.0.0/0
- 0 0 ACCEPT all -- * ens33 192.168.123.101 0.0.0.0/0
2.11 允许其他主机通过 22 号端口访问 192.168.123.101 这台主机
- $ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p tcp --dport 22 -j ACCEPT
- $ iptables -t filter -A OUTPUT -s 192.168.123.100 -d 0.0.0.0/0 -p tcp --sport 22 -j ACCEPT
- $ iptables -t filter -L -n -v
- Chain INPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- ...
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.123.101 tcp dpt:22
- ...
- Chain OUTPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- ...
- 0 0 ACCEPT tcp -- * * 192.168.123.100 0.0.0.0/0 tcp spt:22
2.12 删除 filter 表上 INPUT 和链上的第二条规则
- $ iptables -t filter -D INPUT 2
- $ iptables -t filter -D OUTPUT 2
2.13 允许其他主机通过 80 端口访问 192.168.123.101 这台主机的 httpd 服务
- $ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p tcp --dport 80 -j ACCEPT
- $ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p tcp --sport 80 -j ACCEPT
- $ iptables -L -n -v
- Chain INPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- ...
- 0 0 ACCEPT tcp -- * * 0.0.0.0/0 192.168.123.101 tcp dpt:80
- ...
- Chain OUTPUT (policy DROP 2 packets, 152 bytes)
- pkts bytes target prot opt in out source destination
- ...
- 0 0 ACCEPT tcp -- * * 192.168.123.101 0.0.0.0/0 tcp spt:80
2.13 允许 192.168.123.101 这台主机 ping 其他主机, 不允许其他主机 ping 192.168.123.101.
- $ iptables -t filter -A OUTPUT -s 192.168.123.101 -d 0.0.0.0/0 -p icmp --icmp-type 9 -j ACCEPT
- $ iptables -t filter -A INPUT -s 0.0.0.0/0 -d 192.168.123.101 -p icmp --icmp-type 0 -j ACCEPT
- $ iptables -t filter -L -n -v
- Chain INPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- ...
- 0 0 ACCEPT icmp -- * * 0.0.0.0/0 192.168.123.101 icmptype 0
- ...
- Chain OUTPUT (policy DROP 0 packets, 0 bytes)
- pkts bytes target prot opt in out source destination
- ...
- 0 0 ACCEPT icmp -- * * 192.168.123.101 0.0.0.0/0 icmptype 9
来源: http://www.bubuko.com/infodetail-2687274.html