1.COMMAND
1.1 列出所有链的规则
iptables -L
1.2 列出某条链的规则就是
iptables -L INPUT
iptables -vnL
1.3 清除所有链的规则
iptables -F
1.4 设置默认规则策略
iptables -P INPUT DROP
iptables -P OUTPUT DROP
iptables -P FORWARD DROP
1.5 添加规则,在INPUT链上添加规则,协议是tcp,目标端口号是21
iptables -A INPUT -p tcp --dport 21
1.6 插入规则,在INPUT链上插入规则,协议是tcp,目标端口号是23,规则号是1
iptables -I INPUT 1 -p tcp --dport 23
1.7 替换规则,在INPUT链上替换规则号1的iptables规则,将目标端口号更改为24
iptables -R INPUT 1 -p tcp –dport 24
1.8 删除规则,在INPUT 链上删除规则号是1的iptables规则
iptables -D INPUT 1
2.match 基本规则匹配器
2.1 指定协议
iptables -A INPUT -p tcp -j ACCEPT
2.2 指定ICMP类型
iptables -A INPUT -p icmp --icmp-type 8 -j DROP
iptables -A INPUT -p icmp --icmp-type 0 -j ACCEPT
2.3 指定ip地址
iptables -A INPUT -s 192.168.1.111 -j ACCEPT
2.4 指定接口
iptables -A FORWARD -o ens32 -j ACCEPT
2.5 指定端口号
iptables -A INPUT -p tcp --sport 80 -j ACCEPT
3.match:扩展规则匹配器
3.1 limit :限制速率
iptables -I INPUT -d 192.168.1.111 -p icmp --icmp-type 8 -m limit --limit 3/minute --limit-burst 5 -j ACCEPT
#3/minute 单位时间内可以匹配的数据包
#burst 5 匹配最大初始数据包数量,默认值是5
3.2 iprange :一整段连续的ip
iptables -A INPUT -d 192.168.1.111 -p tcp --dport 80 -m iprange --src-range 192.168.1.200- 192.168.1.200 -j DROP
3.3 time :指定某个时间范围内可以
iptables -A INPUT -s 192.168.1.0/24 -d 192.168.2.0/24 -p tcp --dport 80 -m time --timestart 14:30 --timestop 18:30 --weekdays Sat,Sun --kerneltz -j DROP
3.4 multiport :多个端口
iptables -A INPUT -s 192.168.1.0/24 -d 192.168.2.0/24 -p tcp -m multiport --dports 22,80 -j ACCEPT
3.5 string :对报文中的字符串做匹配检查,一些敏感词汇
iptables -A INPUT -s 192.168.1.0/24 -d 192.168.2.0/24 -p tcp --sport 80 -m string --algo bm --string 'gay' -j REJECT
#algo bm 字符串检验算法
3.6 state:根据”连接追踪机制“去检查连接的状态
iptables -A INPUT -d 172.16.100.67 -p tcp -m multiport --dports 22,80 -m state --state NEW,ESTABLISHED -j ACCEPT
iptables -A OUTPUT -s 172.16.100.67 -p tcp -m multiport --sports 22,80 -m state --state ESTABLISHED -j ACCEPT