*filter
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
-A INPUT -s 0.0.0.0/32 -p tcp -m multiport --dports 22,80 -j ACCEPT
-A INPUT -s 0.0.0.0/32 -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT
-A INPUT -p tcp -m state --state NEW -m tcp --dport 80:101 -j ACCEPT
-A INPUT -m mac --mac-source 00:00:00:00:00:00 -j DROP
-A INPUT -s 192.168.0.0/24 -p tcp -m tcp --dport 3306 -j ACCEPT
-A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT
-A INPUT -p icmp -j ACCEPT
-A INPUT -i lo -j ACCEPT
-A INPUT -j REJECT --reject-with icmp-host-prohibited
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
COMMIT
例子:
单端口转发
iptables -t nat -A PREROUTING -p tcp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A PREROUTING -p udp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A POSTROUTING -p tcp -d [要转发的服务器IP] --dport [要转发的端口号] -j SNAT --to-source [本机IP]
iptables -t nat -A POSTROUTING -p udp -d [要转发的服务器IP] --dport [要转发的端口号] -j SNAT --to-source [本机IP]
端口段转发:转发10000到20000这个端口段,则填10000:20000
iptables -t nat -A PREROUTING -p tcp --dport [要转发的端口段] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A PREROUTING -p udp --dport [要转发的端口段] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A POSTROUTING -p tcp -d [要转发的服务器IP] --dport [要转发的端口段] -j SNAT --to-source [本机IP]
iptables -t nat -A POSTROUTING -p udp -d [要转发的服务器IP] --dport [要转发的端口段] -j SNAT --to-source [本机IP]
最后保存规则和重启iptables服务
service iptables save
service iptables restart
删除端口转发,用 -D 选项
iptables -t nat -D PREROUTING -p tcp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -D PREROUTING -p udp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -D POSTROUTING -p tcp -d [要转发的服务器IP] --dport [要转发的端口号] -j SNAT --to-source [本机IP]
iptables -t nat -D POSTROUTING -p udp -d [要转发的服务器IP] --dport [要转发的端口号] -j SNAT --to-source [本机IP]
Delete transfer
iptables -t nat -D PREROUTING -d [ ip ] -p udp --dport [ port ] -j DNAT --to [ ip ]:[ port ]
service iptables save
service iptables restart
iptables -t nat -L
Append transfer
iptables -t nat -A PREROUTING -p tcp --dport [要转发的端口号] -j DNAT --to-destination [要转发的服务器IP]
iptables -t nat -A PREROUTING -d [ ip ] -p tcp --dport [ port ] -j DNAT --to [ ip ]:[ port ]
iptables -t nat -A PREROUTING -d [ ip ] -p tcp --dport [ port ] -j DNAT --to [ ip ]:[ port ]
service iptables save
service iptables restart
iptables -t nat -L
原文链接:https://blog.csdn.net/qq_43064495/article/details/111496872