eth0: 192.168.100.22/24 gateway:192.168.100.1
宿主机
eth0:192.168.100.223/24 gateway:192.168.100.1
eth1: 172.16.0.1/24 gateway:none
虚机
eth0: 172.16.0.2/24 gateway:172.16.0.1
要求:
把虚机的22端口转发给宿主的2020端口,让clinet可以访问
# vim iptables.sh #!/bin/bash # 转发内网地址172.16.0.2:22到外网192.168.100.223:2020 # 内网gateway 172.16.0.1 Lan_IP=172.16.0.2 Wan_IP=192.168.100.223 iptables -F # 开启转发 echo "1" > /proc/sys/net/ipv4/ip_forward # 允许虚拟机ip iptables -A INPUT -d $Lan_IP -j ACCEPT iptables -A OUTPUT -s $Lan_IP -j ACCEPT # 对虚拟机主机ip端口转发 iptables -t nat -A PREROUTING -p tcp -d $Wan_IP --dport 2020 -j DNAT --to-destination $Lan_IP:22 iptables -t nat -A POSTROUTING -p tcp -d $Lan_IP --dport 22 -j SNAT --to-source 172.16.0.1
# 保存重启
# service iptables save # /etc/init.d/iptables restart # sh iptables.sh
2017-08-08 08:25