VirtualBox NAT设置和端口转发

这是我对 VirtualBox 自带帮助关于 NAT 设置部分的翻译,翻的不当处,请批评指正,我不是英语专业。未完,待续。转载我的文章,请注明出处,非常感谢。

1、 Network Address Translation (NAT)

Network Address Translation (NAT) is the simplest way of accessing an external network from a virtual machine. Usually, it does not require any configuration on the host network and guest system. For this reason, it is the default networking mode in VirtualBox.

网络地址转换( NAT )是最简单的方法从一个虚拟机访问外部网。通常,它并不要求在主机网络和客户机上做任何配置。基于这个原因,它是默认的网络模式 。

A virtual machine with NAT enabled acts much like a real computer that connects to the Internet through a router. The “router”, in this case, is the VirtualBox networking engine, which maps traffic from and to the virtual machine transparently. The disadvantage of NAT mode is that, much like a private network behind a router, the virtual machine is invisible and unreachable from the outside internet; you cannot run a server this way unless you set up port forwarding (described below).

设置为通过 NAT 方式连接的一台虚拟机能像一台真正的计算机一样访问互联网,主机就是一只路由器。 在这种方式下,通过 VirtualBox 网络引擎,虚拟机透明地映射到外部网络。 NAT 方式不方便是,很像是在路由器之后的一个专用网络,从外部互联网看来,虚拟机是无形和不能到达的; 您不可能在虚拟机上运行一个服务器,因为外部网络无法访问通过 NAT 方式连接的内部机,除非您设定了端口转发(下述)。

The virtual machine receives its network address and configuration on the private network from a DHCP server that is integrated into VirtualBox. The address which the virtual machine receives is usually on a completely different network to the host.As more than one card of a virtual machine can be set up to use NAT, the first card is connected to the private network 10.0.2.0, the second card to the network 10.0.3.0 and so on.

虚拟机从一个 VirtualBox 整合的 DHCP 服务器得到私有的网址。 这个网址对主机来说是一个完全不同的网络。一台虚拟机的多个网卡可以被设定使用 NAT, 第一个网卡连接了到专用网 10.0.2.0,第二个网卡连接到专用网络 10.0.3.0,等等。默认得到的客户端ip(IP Address)是10.0.2.15,网关(Gateway)是10.0.2.2,域名服务器(DNS)是10.0.2.3,可以手动参考这个进行修改。

The network frames sent out by the guest operating system are received by VirtualBox’s NAT engine, which extracts the TCP/IP data, and resends it using the host operating system. To an application on the host, or to another computer on the same network as the host, it looks like the data was sent by the VirtualBox application on the host, using an IP address belonging to the host. VirtualBox listens for replies to the packages sent, and repacks and resends them to the guest machine on its private network.

客户机(即虚拟机)送出的网络帧被 VirtualBox 的 NAT 引擎收到,抽取 TCP/IP 数据,再通过主机的操作系统(即安装 VirtualBox 的操作系统)重新发送出去。送到在主机上的一个应用程序,或者到位于主机同一网络的另一台计算机上,它看起来好象是安装在主机上的程序 VirtualBox,通过一个属于主机的 IP 地址,把数据发送出去。VirtualBox 倾听到数据包裹的回复,通过客户机的私人网络重新包装和发送往客户机上。

You can set up a guest service which you wish to proxy using the command line tool VBoxManage. You will need to know which ports on the guest the service uses and to decide which ports to use on the host (often but not always you will want to se the same ports on the guest and on the host). You can use any ports on the host which are not already in use by a service. An example of how to set up incoming NAT connections to a ssh server on the guest requires the following three commands:

你可以设置一个虚拟机的服务(比如 WEB 服务),通过使用命令行工具 VboxManage 代理。你需要知道虚拟机的服务使用哪个端口,然后决定在主机上使用哪个端口(通常但不总是想要使虚拟机和主机使用同一个端口)。在主机上提供一个服务需要使用一个端口,你能使用在主机上没有准备用来提供服务的任何端口。一个怎样设置新的 NAT 例子,在虚拟机上连接到一个 ssh 服务器,需要下面的三个命令:

VBoxManage setextradata "Linux Guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCP

VBoxManage setextradata "Linux Guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22

VBoxManage setextradata "Linux Guest" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 2222

说明:VboxManage 是一个命令行程序,请查询你的 VirtualBox 安装目录,"Linux Guest" 是虚拟主机名。guestssh 是一个自定义的名称,你可以任意设置,通过上面的三个命令,把虚拟机的 22 端口 转发到主机的 2222 端口。

又比如,我在虚拟机 debian 上安装了 apache2 服务器,使用 80 端口,映射到主机的 80 端口。使用下面的命令。

"C:\Program Files\innotek VirtualBox\VBoxManage.exe" setextradata "debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/huzhangsheng/Protocol" TCP

"C:\Program Files\innotek VirtualBox\VBoxManage.exe" setextradata "debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/huzhangsheng/GuestPort" 80

"C:\Program Files\innotek VirtualBox\VBoxManage.exe" setextradata "debian" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/huzhangsheng/HostPort" 80

注意:要使设置生效,请关掉 VirtualBox 再运行虚拟机,我把 VirtualBox 安装在 winxp 上,在虚拟机中安装 debian 4.02r ,虚拟机名是 debian ,并安装了 apache2 php5 mysql-server ,在主机上用IE浏览 http://localhost,成功转发到虚拟机 debian 的 apache2 web 服务器上,通过这点我发现:可能 VirtualBox 的设置比 vmware 更灵活,更强大。

点赞