input:匹配目标IP是本机的数据包。
forward:匹配流经本机的数据包。
output:出口数据包。
prerouting:进行路由选择前处理数据包。
postrouting:进行路由选择后处理数据包。
raw表 | mangle表 | nat表 | filter表 |
---|---|---|---|
prerouting | prerouting | prerouting | input |
output | postrouting | postrouting | forward |
input | output | output | |
output | |||
forward |
表间的优先顺序:raw>mangle>net>filter
链间的匹配顺序
入站数据:prerouting、input
出站数据:output、postrouting
转发数据:prerouting、forward、postrouting
链内的匹配顺序
自上向下匹配,找到匹配的规则就会停止。找不到则按该链的默认策略处理。
[root@server ~]# yum install iptables -y
[root@server ~]# ls /etc/sysconfig/iptables-config
/etc/sysconfig/iptables-config
不指定表名时,默认表示filter表。
不指定链名时,默认表示该表内所有链。
除非设置了规则链的缺省策略,否则需要指定匹配条件。
[root@server ~]# iptables --help #可以查看其使用规则
iptables v1.4.21
Usage: iptables -[ACD] chain rule-specification [options]
iptables -I chain [rulenum] rule-specification [options]
iptables -R chain rulenum rule-specification [options]
iptables -D chain rulenum [options]
iptables -[LS] [chain [rulenum]] [options]
iptables -[FZ] [chain] [options]
iptables -[NX] chain
iptables -E old-chain-name new-chain-name
iptables -P chain target [options]
iptables -h (print this help information)
Commands:
Either long or short options are allowed.
--append -A chain Append to chain
--check -C chain Check for the existence of a rule
--delete -D chain Delete matching rule from chain
--delete -D chain rulenum
Delete rule rulenum (1 = first) from chain
--insert -I chain [rulenum]
Insert in chain as rulenum (default 1=first)
--replace -R chain rulenum
Replace rule rulenum (1 = first) in chain
--list -L [chain [rulenum]]
List the rules in a chain or all chains
--list-rules -S [chain [rulenum]]
Print the rules in a chain or all chains
--flush -F [chain] Delete all rules in chain or all chains
--zero -Z [chain [rulenum]]
Zero counters in chain or all chains
--new -N chain Create a new user-defined chain
--delete-chain
-X [chain] Delete a user-defined chain
--policy -P chain target
Change policy on chain to target
--rename-chain
-E old-chain new-chain
Change chain name, (moving any references)
Options:
--ipv4 -4 Nothing (line is ignored by ip6tables-restore)
--ipv6 -6 Error (line is ignored by iptables-restore)
[!] --protocol -p proto protocol: by number or name, eg. `tcp'
[!] --source -s address[/mask][...]
source specification
[!] --destination -d address[/mask][...]
destination specification
[!] --in-interface -i input name[+]
network interface name ([+] for wildcard)
--jump -j target
target for rule (may load target extension)
--goto -g chain
jump to chain with no return
--match -m match
extended match (may load extension)
--numeric -n numeric output of addresses and ports
[!] --out-interface -o output name[+]
network interface name ([+] for wildcard)
--table -t table table to manipulate (default: `filter')
--verbose -v verbose mode
--wait -w [seconds] maximum wait to acquire xtables lock before give up
--wait-interval -W [usecs] wait time to try to acquire xtables lock
default is 1 second
--line-numbers print line numbers when listing
--exact -x expand numbers (display exact values)
[!] --fragment -f match second or further fragments only
--modprobe= try to insert modules using this command
--set-counters PKTS BYTES set the counter during insert/append
[!] --version -V print package version.
参数 | 作用 |
---|---|
-P | 设置默认策略 |
-F | 清空规则链 |
-L | 查看规则链 |
-A | 在规则链的末尾加入新规则 |
-I num | 在规则链的头部加入新规则 |
-D num | 删除某一条规则 |
-s | 匹配来源地址IP/MASK,加叹号“!”表示除这个IP之外 |
-d | 匹配目标地址 |
-i | 匹配从这个网卡流入的数据 |
-o | 匹配从这个网卡流出的数据 |
-p | 匹配协议,如TCP、UDP、ICMP |
–dport num | 匹配目标端口号 |
–sport num | 匹配来源端口号 |
例1、filter表input链追加一条规则,规则是丢弃访问本机IP的数据包(禁止所有人访问我)。
[root@server ~]# iptables -t filter -A INPUT -j DROP #注意加入后用的远程连接工具会断开。
例2、设置FORWARD链默认规则为DROP
[root@server ~]# iptables -L #查看规则链,可以看到默认是ACCEPT
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@server ~]# iptables -P FORWARD DROP
[root@server ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
Chain FORWARD (policy DROP)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@server ~]# iptables -t filter -I INPUT -p icmp -j DROP
[root@server ~]# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
DROP icmp -- anywhere anywhere
Chain FORWARD (policy ACCEPT)
target prot opt source destination
Chain OUTPUT (policy ACCEPT)
target prot opt source destination
[root@server ~]# iptables -D INPUT 1 #根据序号删除。
[root@server ~]# iptables -F INPUT #清空INPUT链所有规则。
[root@server ~]# iptables -F #清空filter表上所有链上规则。
[root@server ~]# iptables -t NAT #清空NAT表上所有链上规则。
[root@server ~]# iptables -t NAT -F PREROUTING #清空NAT表中 PREROUTING链上的规则。
-F 仅仅是清空链上规则,不会清空默认规则。
如果不写链名,默认清空某表里所有链里的规则。
[root@server ~]# iptables -t nat -A POSTROUTING -s 192.168.220.0/24 -j SNAT --to 2.2.2.2
将192.168.220.0/24段的ip地址转换为2.2.2.2
[root@server ~]# iptables -t nat -vnxL
Chain PREROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain INPUT (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
Chain OUTPUT (policy ACCEPT 5 packets, 354 bytes)
pkts bytes target prot opt in out source destination
Chain POSTROUTING (policy ACCEPT 0 packets, 0 bytes)
pkts bytes target prot opt in out source destination
5 354 SNAT all -- * * 192.168.220.0/24 0.0.0.0/0 to:2.2.2.2
[root@server ~]# iptables -t nat -A PREROUTING -i eno16777736 -p tcp --dport 80 -j DNAT --to 192.168.220.1
从eno16777736 进来要访问80端口的地址改为192.168.220.1
-m state --state
状态:NEW、RELATED、ESTABLISHED、INVALID
NEW:有别于tcp的syn
ESTABLISHED:连接态
RELATED:衍生态,与conntrack关联(FTP)
INVALID:不能被识别属于哪个连接或没有任何状态
[root@server ~]# iptables -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
[root@server ~]# iptables -A FORWARD -m mac --mac-source 00:0c:29:7c:18:13 -j DROP
不转发mac地址为xxx的数据包
-m limit --limit 匹配速率 [--burst 缓冲数量]
用一定速率去匹配
[root@server ~]# iptables -A FORWARD -d 192.168.220.100 -m limit --limit 50/s -j ACCEPT
[root@server ~]# iptables -A FORWARD -d 192.168.220.100 -j DROP
DROP掉平均速率高于50/s的包
-m multiport <--sports|--dports|--ports>
一次性匹配多个端口
[root@server ~]# iptables -A INPUT -p tcp -m multiport --dports 20,21,22,80 -j ACCEPT
允许所有客户端访问服务器20,21,22,80端口上的服务