VirtualBox通過設置端口映射實現Host和Client通信
VirtualBox 是 Sun公司不錯的一款開源虛擬機軟件,免費小巧好用,可以設置OEM,可以設置通過端口映射來實現Host和Client之間的通信,美中不足的是性能比商業(yè)軟件VMWare稍差些。如果你看過VirtualBox的用戶手冊,在6.4.1 Configuring port forwarding with NAT會有詳細端口映射的介紹,下面我就舉個例子說明:
我建立的虛擬機名稱為"CentOS-5.2",網絡采用默認的PCnet-FAST III(NAT),要設置端口映射,必須關閉所有虛擬主機和主程序,然后在DOS運行(如果是Vista請用管理員權限進入DOS):
C:
CD C:\Program Files\Sun\xVM VirtualBox
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCP
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 22
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/Protocol" TCP
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/GuestPort" 80
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/HostPort" 80
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesttomcat/Protocol" TCP
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesttomcat/GuestPort" 8080
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesttomcat/HostPort" 8080
第一行是切換到C盤,然后進入VirtualBox安裝目錄,使用里面的VBoxManage管理程序。注意"CentOS-5.2"改成你的虛擬機名稱,"pcnet"是默認網卡的名稱,如果你選用的是Intel網卡要改成"e1000",緊接著后面是端口順序,從0開始,Protocl可以是TCP也可以是UDP。CD C:\Program Files\Sun\xVM VirtualBox
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/Protocol" TCP
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/GuestPort" 22
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guestssh/HostPort" 22
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/Protocol" TCP
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/GuestPort" 80
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesthttp/HostPort" 80
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesttomcat/Protocol" TCP
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesttomcat/GuestPort" 8080
VBoxManage setextradata "CentOS-5.2" "VBoxInternal/Devices/pcnet/0/LUN#0/Config/guesttomcat/HostPort" 8080
有人覺得比VMWare來說貌似很麻煩,其實不然。首先,VirtualBox的設置都是通過VBoxMange命令或者修改xml來實現,讓用戶可以方便自己定義,比較安全;另外,像我這種經常需要Linux來測試的開發(fā)者來說,如果在沒有網絡的環(huán)境時沒法方便的連接遠程網絡,但是連接虛擬主機很方便的,直接用localhost或127.0.0.1訪問,對于VMWare來說你本機和虛擬主機都要設置固定IP,一旦你從家里跑到公司,可能還得手動修改IP,所以還是VirtualBox更方便些。