iptables设置端口转发

转自:https://blog.csdn.net/sigangjun/article/details/17412821

[plain]  view plain  copy
 
  1. 一 从一台机到另一台机端口转发  
  2. 启用网卡转发功能  
  3. #echo 1 > /proc/sys/net/ipv4/ip_forward  
  4. 举例:从192.168.0.132:21521(新端口)访问192.168.0.211:1521端口  
  5. a.同一端口转发(192.168.0.132上开通1521端口访问 iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 1521 -j ACCEPT)  
  6. iptables -t nat -I PREROUTING -p tcp --dport 1521 -j DNAT --to 192.168.0.211  
  7. iptables -t nat -I POSTROUTING -p tcp --dport 1521 -j MASQUERADE  
  8. b.不同端口转发(192.168.0.132上开通21521端口访问 iptables -A RH-Firewall-1-INPUT -m state --state NEW -m tcp -p tcp --dport 21521 -j ACCEPT)  
  9. iptables -t nat -A PREROUTING -p tcp -m tcp --dport 21521 -j DNAT --to-destination 192.168.0.211:1521  
  10. iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -d 192.168.0.211 -p tcp -m tcp --dport 1521 -j SNAT --to-source 192.168.0.132  
  11.   
  12. 以上两条等价配置(更简单[指定网卡]):  
  13. iptables -t nat -A PREROUTING -p tcp -i eth0 --dport 31521 -j DNAT --to 192.168.0.211:1521  
  14. iptables -t nat -A POSTROUTING -j MASQUERADE  
  15. 保存iptables  
  16. #service iptables save  
  17. #service iptables restart  
  18. 二 用iptables做本机端口转发  
  19. 代码如下:  
  20.   iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-ports 8080  
  21. 估计适当增加其它的参数也可以做不同IP的端口转发。  
  22. 如果需要本机也可以访问,则需要配置OUTPUT链(********特别注意:本机访问外网的端口会转发到本地,导致访不到外网,如访问yown.com,实际上是访问到本地,建议不做80端口的转发或者指定目的 -d localhost):  
  23.   iptables -t nat -A OUTPUT -d localhost -p tcp --dport 80 -j REDIRECT --to-ports 8080  
  24. 原因:  
  25. 外网访问需要经过PREROUTING链,但是localhost不经过该链,因此需要用OUTPUT。  



echo 1 > /proc/sys/net/ipv4/ip_forward
 
 
 
 
 
 

将本地接口IP 61.144.14.72 的3389端口 转发到 116.6.73.229的3389      
(主要访问到61.144.14.72的3389端口,就会跳转到116.6.73.229的3389)

【步骤】

1、 首先应该做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默认是0    这样允许iptalbes FORWARD。

2、 service iptables stop  关闭防火墙

3、 重新配置规则

iptables -t nat -A PREROUTING --dst 61.144.14.72 -p tcp --dport 3389 -j DNAT --to-destination 116.6.73.229:3389
iptables -t nat -A POSTROUTING --dst 116.6.73.229 -p tcp --dport 3389 -j SNAT --to-source 61.144.14.72

service iptables save   
将当前规则保存到 /etc/sysconfig/iptables
若你对这个文件很熟悉直接修改这里的内容也等于命令行方式输入规则。

5、 启动iptables 服务, service iptables start
可以写进脚本,设备启动自动运行;
# vi /etc/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
sh /root/myshipin.log
--------------------------------------------------------------------- 
vi myshipin.log

#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don't
# want to do the full Sys V style init stuff.
iptables -F -t nat iptables -t nat -A PREROUTING --dst 61.144.14.72 -p tcp --dport 3389 -j DNAT --to-destination 116.6.73.229:3389
iptables -t nat -A POSTROUTING --dst 116.6.73.229 -p tcp --dport 3389 -j SNAT --to-source 61.144.14.72
~

----------------------------------------------------------------
TCP
iptables -t nat -A PREROUTING --dst 61.144.14.87 -p tcp --dport 9304 -j DNAT --to-destination 10.94.143.204:9304
iptables -t nat -A POSTROUTING --dst 10.94.143.204 -p tcp --dport 9304 -j SNAT --to-source 61.144.14.87
UDP
iptables -t nat -A PREROUTING --dst 61.144.14.87 -p udp --dport 9305 -j DNAT --to-destination 10.94.143.204:9305
iptables -t nat -A POSTROUTING --dst 10.94.143.204 -p udp --dport 9305 -j SNAT --to-source 61.144.14.87 

猜你喜欢

转载自www.cnblogs.com/x_wukong/p/9070212.html