iptables应用主要包括主机防火墙和网络防火墙
主机防火墙详解(服务范围当前主机):
iptables其规则主要作用在“匹配条件”上,具体为各种模块
icmp模块
--icmp-type
8:echo-request
0:echo-reply
例子:两个主机 in和out,允许in ping out 不允许out ping in
做法:首先在in上拒绝所有ping请求,先生成一个白名单再在in上写规则
[root@in ~]# iptables -A INPUT -p icmp -j REJECT
[root@in ~]# iptables -A OUTPUT -p icmp -j REJECT
[root@in ~]# iptables -I OUTPUT 1 -p icmp --icmp-type 8 -j ACCEPT
[root@in ~]# iptables -I INPUT 1 -p icmp --icmp-type 0 -j ACCEPT
multiport模块
iptables的多端口匹配
Linux系统上配置iptables放开相应的80、21、22、23、53的端口访问:
[root@ ~]# iptables -R INPUT 1 -p tcp -m multiport --dport 21:23,80,53 -j ACCEPT
iprange模块
匹配连读的地址访问:
允许192.168.0.1-192.168.0.50的地址访问到192.168.0.81的80端口:
[root@ ~]# iptables -I INPUT -d 192.168.0.81 -p tcp --dport 80 -m iprange --src-range 192.168.0.1-192.168.0.50 -j ACCEPT
ipset模块
允许非连续的地址访问
[root@ ~]# ipset create httplist hash:net maxelem 10000
[root@ ~]# ipset add httplist 192.168.153.101
[root@ ~]# ipset add httplist 192.168.153.102
[root@ ~]# iptables -I INPUT 1 -p tcp --dport 80 -m set --match-set httplist src -j ACCEPT
[root@ ~]# iptables -I OUTPUT 1 -p tcp --sport 80 -m set --match-set ht