centos6.x版本系统使用iptables实现端口转发

作者: Mr.Xuan 分类: 技术交流 发布时间: 2016-04-18 13:49

 

转发目的

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

【步骤】

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

  1. vi /etc/sysctl.conf

2、 service iptables stop 关闭防火墙

  1. service iptables stop

3、 重新配置规则

  1. iptables t nat –A PREROUTING —dst 1.2.3.4 –p tcp dport 3389 –j DNAT todestination 5.6.7.8:3389
  2. iptables t nat A POSTROUTING dst 5.6.7.8 p tcp dport 3389 j SNAT tosource 1.2.3.4

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

  1. service iptables save

5、 启动iptables 服务, service iptables start

  1. service iptables start

可以写进脚本,设备启动自动运行;

  1. # vi /etc/rc.local
  2. #!/bin/sh
  3. #
  4. # This script will be executed *after* all the other init scripts.
  5. # You can put your own initialization stuff in here if you don’t
  6. # want to do the full Sys V style init stuff.
  7.  
  8. touch /var/lock/subsys/local
  9.  
  10. sh /root/myshipin.log
  11. ———————————————————————
  12. vi myshipin.log
  13. #!/bin/sh
  14. #
  15. # This script will be executed *after* all the other init scripts.
  16. # You can put your own initialization stuff in here if you don’t
  17. # want to do the full Sys V style init stuff.
  18.  
  19. iptables F t nat
  20. iptables t nat A PREROUTING dst 61.144.a.b p tcp dport 3389 j DNAT todestination 116.6.c.d:3389
  21. iptables t nat A POSTROUTING dst 116.6.a.b p tcp dport 3389 j SNAT tosource 61.144.c.d
  22. ~
  23. —————————————————————-
  24. TCP
  25.  
  26. iptables t nat A PREROUTING dst 61.144.a.b p tcp dport 9304 j DNAT todestination 10.94.a.b:9304
  27. iptables t nat A POSTROUTING dst 10.94.a.b p tcp dport 9304 j SNAT tosource 61.144.a.b
  28.  
  29. UDP
  30. iptables t nat A PREROUTING dst 61.144.a.b p udp dport 9305 j DNAT todestination 10.94.a.b:9305
  31. iptables t nat A POSTROUTING dst 10.94.a.b p udp dport 9305 j SNAT tosource 61.144.a.b

另:

iptables配置文件的位置:/etc/sysconfig/iptables 外网地址发变化在配置文件里修改就可以了。

如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

#9 #8 #73 #72 #71 #70 #7 #69 #68 #67 #66 #65 #64 #63 #62 #61 #60 #6 #59 #58 #57 #56 #55 #54 #53 #52 #51 #50 #5 #49 #48 #47 #46 #45 #44 #43 #42 #41 #40 #4 #39 #38 #37 #36 #35 #34 #33 #32 #31 #30 #3 #29 #28 #27 #26 #25 #24 #23 #22 #21 #20 #2 #19 #18 #17 #16 #15 #14 #13 #12 #11 #10 #1