
路由


實驗環(huán)境
機器1:本機172.16.1.52 把網(wǎng)關(guān)指向172.16.1.51
機器2:有公網(wǎng),能上網(wǎng)的機器,開啟內(nèi)核轉(zhuǎn)發(fā)
eth3:172.16.1.51 沒有網(wǎng)關(guān)
eth2:10.0.0.51 網(wǎng)關(guān)gw 10.0.0.1
在機器2上,做出去包的源地址轉(zhuǎn)換
方法1:做出去包的源地址轉(zhuǎn)換
a有固定外網(wǎng)Ip,
b或單一固定外網(wǎng)IP
iptables -t nat -D POSTROUTING -s 172.16.1.0/24 -o eth2 -j SNAT --to-source 10.0.0.51
方法2:做出去包的源地址偽裝
a沒有固定IP,
b或多個外網(wǎng)IP,不知道想用哪個ip,自動選擇ip
iptables -t nat -D POSTROUTING -s 172.16.1.0/24 -o eth2 -j MASQUERADE
目的端口轉(zhuǎn)換
筆記本要訪問10.0.0.51的9000端口
10.0.0.5進行iptables配置,將目的ip和端口都轉(zhuǎn)換、映射成172.16.1.52:80
就相當于,筆記本訪問172.16.1.52:80
iptables -t nat -A PREROUTING -d 10.0.0.51 -p tcp --dport 9000 -j DNAT --to-destination 172.16.1.52:80

內(nèi)部局域網(wǎng)生產(chǎn)映射案例:實現(xiàn)外部IP 124.42.34.112一對一映射到內(nèi)部server10.0.0.8
網(wǎng)關(guān):IP:eth0:124.42.60.109 eth1:10.0.0.254
首先在路由網(wǎng)關(guān)上綁定VIP124.42.34.112,可以是別名或輔助IP的方式。
-A POSTROUTING -s 10.0.0.0/255.255.240.0 -d 124.42.34.112 -j SNAT --to-source 10.0.0.254
-A PREROUTING -d 124.42.34.112 -j DNAT --to-destination 10.0.0.8
-A POSTROUTING -s 10.0.0.8 -o eth0 -j SNAT --to-source 124.42.34.112
內(nèi)核優(yōu)化


