centos6.5 iptables實現端口轉發


將本地接口IP 61.144.a.b 的3389端口 轉發到 116.6.c.d的3389      (主要訪問到61.144.a.b的3389端口,就會跳轉到116.6.c.d的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.a.b -p tcp --dport 3389 -j DNAT --to-destination 116.
6.c.d:3389

iptables -t nat -A POSTROUTING --dst 116.6.c.d -p tcp --dport 3389 -j SNAT --to-source 61.144.a.b

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.a.b -p tcp --dport 3389 -j DNAT --to-destination 116.6.c.d:3389
iptables -t nat -A POSTROUTING --dst 116.6.a.b -p tcp --dport 3389 -j SNAT --to-source 61.144.c.d
~
----------------------------------------------------------------
TCP

iptables -t nat -A PREROUTING --dst 61.144.a.b -p tcp --dport 9304 -j DNAT --to-destination 10.94.a.b:9304
iptables -t nat -A POSTROUTING --dst 10.94.a.b -p tcp --dport 9304 -j SNAT --to-source 61.144.a.b

UDP
iptables -t nat -A PREROUTING --dst 61.144.a.b -p udp --dport 9305 -j DNAT --to-destination 10.94.a.b:9305
iptables -t nat -A POSTROUTING --dst 10.94.a.b -p udp --dport 9305 -j SNAT --to-source 61.144.a.b

另:

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM