Technical note
iptables学习笔记
参考文档
https://www.zsythink.net/archives/1199
流程图

-
PREROUTING 的规则可以存在于:raw表,mangle表,nat表。
-
INPUT 的规则可以存在于:mangle表,filter表,(centos7中还有nat表,centos6中没有)。
-
FORWARD 的规则可以存在于:mangle表,filter表。
-
OUTPUT 的规则可以存在于:raw表mangle表,nat表,filter表。
-
POSTROUTING 的规则可以存在于:mangle表,nat表。
-
raw 表中的规则可以被哪些链使用:PREROUTING,OUTPUT
-
mangle 表中的规则可以被哪些链使用:PREROUTING,INPUT,FORWARD,OUTPUT,POSTROUTING
-
nat 表中的规则可以被哪些链使用:PREROUTING,OUTPUT,POSTROUTING(centos7中还有INPUT,centos6中没有)
-
filter 表中的规则可以被哪些链使用:INPUT,FORWARD,OUTPUT
常用操作
# 查看规则
iptables --line -nvL
# 查看指定链上的规则
iptables --line -nvL INPUT
# 清空INPUT链中的规则
iptables -F INPUT
# 清空filter表在INUT链中的规则
iptables -t filter -F INPUT
# 插入规则
iptables -t filter -I INPUT -s 10.211.55.101 -j DROP
# 追加规则
iptables -t filter -A INPUT -s 10.211.55.101 -j ACCEPT
# 在第N行前插入规则
iptables -t filter -I INPUT 2 -s 10.211.55.101 -j ACCEPT
# 删除指定行
iptables -t filter -D INPUT 3
# 修改默认策略
iptables -t filter -P FORWARD DROP
# centos7安装iptables-services
yum install iptables-services
systemctl stop firewalld && systemctl disable firewalld #停止firewalld
systemctl start iptables && systemctl enable iptables #启动iptables
# 保存规则
service iptables save
# 保存规则
iptables-save > /etc/sysconfig/iptables
# 还原规则
iptables-restore < /etc/sysconfig/iptables
# 查看规则
cat /etc/sysconfig/iptables
# 条件匹配
-s 10.211.55.0/24 #源地址
-d 10.211.55.10 #目标地址
-p tcp #协议 tcp, udp, udplite, icmp, esp, ah, sctp
-i eth0 #流入网卡 只能用于PREROUTING链、INPUT链、FORWARD链
-o eth1 #流出网卡 只能用于FORWARD链、OUTPUT链、POSTROUTING链
--dport 22 #目标端口22
! --dport 22 #目标端口,非22的
–-dport 22:25 # 目标端口,22到25的
-m multiport --dport 22,36,80 #目标端口,离散的,需要用到扩展模块multiport
# 创建自定义链
iptables -t filter -N IN_WEB
iptables -t filter -I IN_WEB -s 10.211.55.101 -j REJECT
iptables -I IN_WEB -s 10.211.55.102 -j REJECT
# 引用自定义链
iptables -I INPUT -p tcp --dport 80 -j IN_WEB
# 删除自定义链
iptables -D INPUT 1
iptables -t filter -F IN_WEB
iptables -t filter -X IN_WEB