临川十中 胡长生 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 更灵活,更强大。


VirtualBox是通过NAT(Network Address Translation 网络地址转换)来实现虚拟机对外部网络的访问,设置为通过 NAT 方式连接的一台虚拟机能像一台真正的计算机一样访问互联网,主机就是一只路由器。在这种方式下,通过 VirtualBox 网络引擎,虚拟机透明地映射到外部网络。 但是NAT 方式不方便的是,很像是在路由器之后的一个专用网络,从外部互联网看来,虚拟机是无形和不能到达的;一般情况下是不可能在虚拟机上运行一个服务器,因为外部网络无法访问通过 NAT 方式连接的内部机。


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


     为了让外部网络访问虚拟服务器,只有设定端口映射(转发)。在设置一个虚拟机的服务(比如SSH服务)后,通过使用命令行工具 VboxManage 代理。假如需要在虚拟机的服务端口是22,然后决定在主机上确定一个端口2222(通常但不总是想要使虚拟机和主机使用同一个端口,但必须保证能使用在主机上并且没有准备用来提供其他服务的端口)。接下来使用VBoxMangage执行端口映射的命令:


VBoxManage setextradata “Debian” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol” TCP


VBoxManage setextradata “Debian” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort” 22


VBoxManage setextradata “Debian” “VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort” 2222


说明:VboxManage 是一个命令行程序,请查询你的 VirtualBox 安装目录,”Debian” 是虚拟主机名。guestssh 是一个自定义、任意设置的名称,pcnet是虚拟机的网络连接接口,在虚拟机配置中,可以看到!通过上面的三个命令,把虚拟机的 22 端口转发到主机的 2222 端口。


Mandriva下配置VirtualBox的bridge上网方式 这种方式的优点是:外部主机可以直接访问virtualbox系统


步骤如下: (以下命令均用root用户执行)
1. 安装uml-utilities和bridge-utils和,这两个工具分别含有tunctl和brctl命令


   urpmi uml-utilities
   urpmi bridge-utils


2. 生成一个新的TAP接口


   tunctl -t tap1 -u <username>


这里<username>是你想用bridge联网方式启动VirtualBox的那个用户的用户名
比如我的是: tunctl -t tap1 -u wan


3. 生成一个叫做br0的bridge


   brctl addbr br0


4. 把你的真实网卡设成promiscuous模式,使得它能够接收发送到别的MAC地址的帧


   ifconfig eth0 0.0.0.0 promisc


5. 把你的真实网卡加到bridge br0的一端


   brctl addif br0 eth0


6. 为你的br0配置一个真实的IP地址,注意与你的真实网卡在一个网段


   ifconfig br0 192.168.1.60


  不建议使用DHCP


7. 把上面生成的TAP接口加到 bridge br0的另一端


   brctl addif br0 tap1


8. 激活TAP


   ifconfig tap1 up


9. 设置/dev/net/tun的读写权限


   chmod 0666 /dev/net/tun
  


经过以上几步之后,在你的virtualbox系统启动之前,将它的Network的Adapter0设成
HostInterface,然后Interface Name那一栏天上tap1
启动virtualbox系统,在该系统中,将你的ip地址设成跟Host系统的eth0在一个局域网就可以了

历史博文

标签:, , ,
八月 25, 2009 at 5:13 下午 by yippee 47 次
Category: Info
Tags: , , ,