濮阳杆衣贸易有限公司

主頁 > 知識庫 > 配置iptables實現(xiàn)本地端口轉(zhuǎn)發(fā)的方法詳解

配置iptables實現(xiàn)本地端口轉(zhuǎn)發(fā)的方法詳解

熱門標簽:高德地圖標注在電腦上 金華呼叫中心外呼系統(tǒng)廠家 杭州電銷機器人有效果嗎 襄陽地圖標注店 萊蕪移動外呼系統(tǒng) 柯城手機地圖如何做地圖標注 申請400電話流程好嗎 小語股票電銷機器人 軟件電話機器人


場景
假如你在用 resin 調(diào)試一個 Web 程序,需要頻繁地重啟 resin。這個 Web 程序需要開在 80 端口上,而 Linux 限制 1024 以下的端口必須有 root 權(quán)限才能開啟。但是你又不愿意在調(diào)程序的時候總是開著一個 root 終端。在這種情況下,你可以把 resin 開在默認的 8080 端口上,然后使用 iptables 來實現(xiàn)和真的把服務(wù)開在 80 端口上一樣的效果。
方法
將與 80 端口的 TCP 連接轉(zhuǎn)接到本地的 8080 端口上。使用 DNAT (Destination Network Address Translation) 技術(shù)可以滿足這一要求。因為 iptables 在處理本地連接和遠程連接的方法不同,所以需要分開處理。下面假設(shè)本機的 IP 是 192.168.4.177。
遠程連接
遠程連接指的是由另外一臺機器連接到這臺機器上。這種連接的數(shù)據(jù)包在 iptables 會首先經(jīng)過 PREROUTING 鏈,所以只需在 PREROUTING 鏈中作 DNAT。

復(fù)制代碼
代碼如下:

# iptables -t nat -A PREROUTING -p tcp -i eth0 -d 192.168.4.177 --dport 80 -j DNAT --to 192.168.4.177:8080

本地連接
本地連接指的是在本機上,用 127.0.0.1 或者本機 IP 來訪問本機的端口。本地連接的數(shù)據(jù)包不會通過網(wǎng)卡,而是由內(nèi)核處理后直接發(fā)給本地進程。這種數(shù)據(jù)包在 iptables 中只經(jīng)過 OUTPUT 鏈,而不會經(jīng)過 PREROUTING 鏈。所以需要在 OUTPUT 鏈中進行 DNAT。除了對 127.0.0.1 之外,對本機 IP (即 192.168.4.177) 的訪問也屬于本地連接。

復(fù)制代碼
代碼如下:

# iptables -t nat -A OUTPUT -p tcp -d 127.0.0.1 --dport 80 -j DNAT --to 127.0.0.1:8080
# iptables -t nat -A OUTPUT -p tcp -d 192.168.4.177 --dport 80 -j DNAT --to 127.0.0.1:8080

注意事項
你也許需要通過以下命令打開 IP 轉(zhuǎn)發(fā):

復(fù)制代碼
代碼如下:

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

在進行試驗時,如果要重新設(shè)置 iptables,需要首先清空 nat 表:

復(fù)制代碼
代碼如下:

# iptables -F -t nat

實例操作
這里將本地接口IP 61.144.a.b 的3389端口 轉(zhuǎn)發(fā)到 116.6.c.d的3389      (主要訪問到61.144.a.b的3389端口,就會跳轉(zhuǎn)到116.6.c.d的3389)
1、 首先應(yīng)該做的是/etc/sysctl.conf配置文件的 net.ipv4.ip_forward = 1 默認是0    這樣允許iptalbes FORWARD。
2、 service iptables stop  關(guān)閉防火墻
3、 重新配置規(guī)則

復(fù)制代碼
代碼如下:

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

        將當前規(guī)則保存到 /etc/sysconfig/iptables
        若你對這個文件很熟悉直接修改這里的內(nèi)容也等于命令行方式輸入規(guī)則。
5、 啟動iptables 服務(wù), service iptables start


可以寫進腳本,設(shè)備啟動自動運行;


復(fù)制代碼
代碼如下:

# 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./p> p>touch /var/lock/subsys/local/p> p>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./p> p>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/p> p>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/p> p>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 外網(wǎng)地址發(fā)變化在配置文件里修改就可以了。

標簽:鶴壁 景德鎮(zhèn) 河南 威海 天門 欽州 海北 黔南

巨人網(wǎng)絡(luò)通訊聲明:本文標題《配置iptables實現(xiàn)本地端口轉(zhuǎn)發(fā)的方法詳解》,本文關(guān)鍵詞  配置,iptables,實現(xiàn),本地,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《配置iptables實現(xiàn)本地端口轉(zhuǎn)發(fā)的方法詳解》相關(guān)的同類信息!
  • 本頁收集關(guān)于配置iptables實現(xiàn)本地端口轉(zhuǎn)發(fā)的方法詳解的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章
    红安县| 瑞安市| 蒙城县| 衡阳县| 广德县| 潞城市| 黄山市| 松原市| 习水县| 罗源县| 盐亭县| 宣化县| 岑溪市| 建德市| 黄平县| 赤城县| 浮梁县| 巴东县| 余庆县| 大埔区| 微山县| 寿光市| 库车县| 沙雅县| 阜新| 江华| 横山县| 海口市| 万盛区| 会同县| 余江县| 和林格尔县| 大冶市| 交口县| 镇坪县| 长沙县| 九寨沟县| 阳曲县| 樟树市| 梅州市| 贡觉县|