iptables -vnL --line-numbers
iptables -F INPUT
iptables -Z
#拒绝来自192.168.0.99的任何访问
[root@96 ~]#iptables -A INPUT -s 192.168.0.99 -j DROP
#拒绝来自192.168.0.0/24网段的任何访问
[root@96 ~]#iptables -A INPUT -s 192.168.0.0/24 -j REJECT
#删除INPUT链的第一条规则
[root@96 ~]#iptables -D INPUT 1
[root@96 ~]#iptables -S
-P INPUT ACCEPT
-P FORWARD ACCEPT
-P OUTPUT ACCEPT
-A INPUT -s 192.168.0.98/32 -j DROP
#在第二条插入一条规则
[root@96 ~]#iptables -I INPUT 2 -s 192.168.0.99 -j DROP
#所有外部主机都拒绝
[root@96 ~]#iptables -A INPUT -j REJECT
[root@96 ~]#iptables -I INPUT -s 127.0.0.1 -j ACCEPT
[root@96 ~]#iptables -R INPUT 2 -s 192.168.0.88 -j ACCEPT
[root@96 ~]#iptables -I INPUT 3 -i ens33 -j ACCEPT
[root@96 ~]#iptables -A OUTPUT -d 192.168.0.97/32 -j REJECT
#禁止ping,不给对方响应包。自己可以主动向外ping
[root@96 ~]#iptables -A OUTPUT -p icmp --icmp-type 0 -j REJECT
#允许127.0.0.1可以ping通
[root@96 ~]#iptables -I OUTPUT 1 -s 127.0.0.1 -p icmp --icmp-type 0 -j ACCEPT
#允许192.168.0.97访问本机的22端口
[root@96 ~]#iptables -I INPUT 3 -s 192.168.0.97 -p tcp --dport 22 -j ACCEPT
#允许192.168.0.97访问本机的22到80之间的端口
[root@96 ~]#iptables -I INPUT 3 -s 192.168.0.97 -p tcp --dport 22:80 -j ACCEPT
[root@96 ~]#iptables -R INPUT 3 -p tcp -m multiport --dports 30,40,50 -j ACCEPT
[root@96 ~]#iptables -R INPUT 3 -s 192.168.0.97 -p tcp -m multiport --dports 30,40,50 -j ACCEPT
#禁止192.168.0.98访问我,我可以访问他
[root@96 ~]#iptables -R INPUT 1 -s 192.168.0.98 -m state --state NEW -j DROP
##########################################################################################################
##########################################################################################################
#########################################扩展模块#########################################################
##########################################################################################################
##########################################################################################################
# -m multiport
[root@96 ~]#iptables -R INPUT 3 -s 192.168.0.97 -p tcp -m multiport --dports 30,40,50 -j ACCEP
# -m iprange
[root@96 ~]#iptables -R INPUT 3 -m iprange --src-range 192.168.0.90-192.168.0.98 -j ACCEPT
# -m iprange 和-m multiport合并使用
[root@96 ~]#iptables -R INPUT 3 -m iprange --src-range 192.168.0.90-192.168.0.98 -p tcp -m multiport --dports 30,40,50 -j ACCEPT
# -m addrtype
[root@96 ~]#iptables -I INPUT -m addrtype --src-type LOCAL -j ACCEPT
# -m state [INVALID|ESTABLISHED|NEW|RELATED|UNTRACKED] 禁止192.168.0.98访问我,我可以访问他
[root@96 ~]#iptables -R INPUT 1 -s 192.168.0.98 -m state --state NEW -j DROP
####网络防火墙使用的FORWARD链守护整个网络(禁止别人访问我,我可以访问别人)
[root@96 ~]#iptables -A FORWARD -d 192.168.0.0/24 -m state --state NEW -j REJECT
#####################SNAT和DNAT后端主机的网关需要配置正确。指向防火墙主机的内网端口
#SNAT(用于共享上网,需要注意filter的的forward的默认规则需要是允许,安装docker会导致默认规则更改为拒绝。ip_forward需要开启)
#方式一:修改默认FORWARD策略为接收
[root@97 ~]# iptables -P FORWARD ACCEPT
[root@97 ~]# iptables -t nat -R POSTROUTING 1 -s 192.168.101.0/24 -j SNAT --to-source 192.168.0.97
[root@97 ~]# iptables -t nat -R POSTROUTING 1 -s 192.168.101.0/24 -j MASQUERADE
#方式二:添加源地址和目标地址为自己网段的接收规则
[root@97 ~]# iptables -I FORWARD -s 192.168.101.0/24 -j ACCEPT
[root@97 ~]# iptables -I FORWARD -d 192.168.101.0/24 -j ACCEPT
##DNAT本地端口映射,映射到后端的另一个端口上,ip_forward需要开启
iptables -t nat -R PREROUTING 1 -d 192.168.0.96 -p tcp --dport 33067 -j DNAT --to-destination 192.168.101.97:3306
###本地端口映射,映射到本机的另一个端口上,ip_forward不需要开启
iptables -t nat -R PREROUTING 1 -d 192.168.0.96 -p tcp --dport 33067 -j REDIRECT --to-ports 22