機器1:有一個內網IP,一個外網IPtcp
內網:eth0:192.168.10.10
ide
外網:eth1:10.10.10.10
ip
機器2:與機器1內網同一網段,無外網IP路由
內網:eth0:192.168.10.11
it
機器1作路由器,映射機器2,使機器2能夠上網,從外網鏈接機器2io
步驟一:table
配置機器2的網關爲機器1
class
步驟二:配置
機器1:添加轉發功能
iptables
vi /etc/sysctl.conf
net.ipv4.ip_forward=1
sysctl -p
步驟三:
使用機器2能夠訪問外網,在機器1上配iptables
iptables -t nat -A POSTROUTING -s 192.168.10.11/24 -o em1 -j MASQUERADE
外網能夠鏈接到機器2.在機器1上配NAT映射
iptables -t nat -A PREROUTING -d 10.10.10.10 -p tcp --dport 11122 -j DNAT --to-destination 192.168.10.11:22
iptables -t nat -A PREROUTING -d 10.10.10.10 -p tcp --dport 18080 -j DNAT --to-destination 192.168.10.11:8080
iptables -t nat -A PREROUTING -d 10.10.10.10 -p tcp --dport 180 -j DNAT --to-destination 192.168.10.11:80