1. 程式人生 > >VirtualBox NAT設定和埠轉發

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 更靈活,更強大。