pc1 ip:192.168.23.252 linux
pc2 ip:192.168.23.253 web
目的:訪問pc1 的web服務跳轉到pc2的web服務上 bash
實現步驟:tcp
開啓linux 自帶的轉發功能ide
echo 1 > /proc/sys/net/ipv4/ip_forward
我這裏使用沒有任何規則的iptables測試
service iptables stop iptables -F iptables -X iptables -Z service iptables save
轉發命令blog
iptables -t nat -A PREROUTING -d 192.168.23.252 -p tcp --dport 80 -j DNAT --to-destination 192.168.23.253:80 #若是進來的route的訪問目的地址是192.168.23.252而且訪問的目的端口是80,就進行dnat轉換,把目的地址改成192.168.23.253 ,端口仍是80 iptables -t nat -A POSTROUTING -d 192.168.23.253 -p tcp --dport 80 -j SNAT --to 192.168.23.252 #當FORWARD 出來後,訪問的目的地址是192.168.23.253,端口是80的。進行snat地址轉換,把原地址改成192.168.23.252 iptables -A FORWARD -o eth0 -d 192.168.23.253 -p tcp --dport 80 -j ACCEPT #當從eth0出去的訪問目的地址是 192.168.23.253且目的端口是80的route,容許經過 iptables -A FORWARD -i eth0 -s 192.168.23.253 -p tcp --dport 80 -j ACCEPT #當從eth0進來的原地址是 192.168.23.253且目的端口是80的route,容許經過 保存規則啓動iptables service iptables save service iptables start
訪問測試:ip
沒轉發以前訪問pc1 以下圖:it
沒轉發以前訪問pc2 以下圖:
io
實現端口轉發後訪問pc1 以下圖: