VirtualBox的提供了四種網絡接入模式,它們分別是:
一、NAT 網絡地址轉換模式(NAT,Network Address Translation)
二、Bridged Adapter 橋接模式
三、Internal 內部網絡模式
四、Host-only Adapter 主機模式html
其中NAT模式是最簡單的實現虛擬機上網的方式,能夠認爲Virtual Host訪問網絡的全部數據都是由主機提供,可是卻並不真實存在於網絡中,主機與網絡中的任何機器都不能查看和訪問到Virtual Host的存在。
ios
Virtual Host與主機關係:
只能單向訪問,虛擬機能夠經過網絡訪問到主機,主機沒法經過網絡訪問到虛擬機。 web
Virtual Host與網絡中其餘Virtual Host的關係:
只能單向訪問,虛擬機能夠訪問到網絡中其餘主機,其餘主機不能經過網絡訪問到虛擬機。shell
Virtual Host與Virtual Host之間的關係:
相互不能訪問,虛擬機與虛擬機各自徹底獨立,相互間沒法經過網絡訪問彼此。
api
NAT方案優缺點:
筆記本已插網線時: 虛擬機能夠訪問主機,虛擬機能夠訪問互聯網網絡
筆記本沒插網線時: 主機的「本地鏈接」有紅叉的,虛擬機能夠訪問主機,虛擬機不能夠訪問互聯網less
But, 作了端口映射後,兩種方式主機都可以訪問虛擬機上的服務(如SSH Web DB 等)
dom
譬如我須要用Secure CRT經過NAT方式鏈接到Virtual Box 的ssh服務 ssh
1 查看NAT網卡的地址tcp
鏈接到在個人電腦上,NAT模式的配置是
IP:10.0.2.15
GW:10.0.2.2
DNS:10.0.2.3
2 查看設備名稱
打開VirtualBox -> Machine -> Show log 搜索NAT關鍵字能夠看到
00:00:02.043 [/Devices/e1000/0/LUN#0/] (level 4)
00:00:02.043 Driver <string> = "NAT" (cb=4)
3 配置端口映射
case 1 老版本的VirtualBox
新建bat文件,輸入如下內容:
VBoxManage.exe setextradata "Ubuntu" "VBoxInternal/Devices/e1000/0/LUN#0/Config/http/Protocol" TCP
VBoxManage.exe setextradata "Ubuntu" "VBoxInternal/Devices/e1000/0/LUN#0/Config/http/GuestPort" 8080
VBoxManage.exe setextradata "Ubuntu" "VBoxInternal/Devices/e1000/0/LUN#0/Config/http/HostPort" 8888
VBoxManage.exe setextradata "Ubuntu" "VBoxInternal/Devices/e1000/0/LUN#0/Config/shell/Protocol" TCP
VBoxManage.exe setextradata "Ubuntu" "VBoxInternal/Devices/e1000/0/LUN#0/Config/shell/GuestPort" 22
VBoxManage.exe setextradata "Ubuntu" "VBoxInternal/Devices/e1000/0/LUN#0/Config/shell/HostPort" 2222
其中http和shell爲服務映射的名稱, 能夠自定義, Virtual Host的8080和22 端口映射到Host的8888和2222端口
運行中鍵入cmd進入控制檯,切換到程序目錄
cd C:\Program Files\Oracle\VirtualBox
執行bat文件並重啓VirtualBox
另外也能夠經過修改虛擬機目錄下.xml的方式添加服務,參見
http://wenku.baidu.com/view/9f91900f7cd184254b35358d.html
case 2
新版的Oracle VirtualBox中VBoxManager的命令已經發生改變
能夠用modifyvm參數設置端口映射
VBoxManage modifyvm <uuid|name>
[--name <name>]
[--ostype <ostype>]
[--memory <memorysize in MB>]
[--pagefusion on|off]
[--vram <vramsize in MB>]
[--acpi on|off]
[--pciattach 03:04.0]
[--pciattach 03:04.0@02:01.0]
[--pcidetach 03:04.0]
[--ioapic on|off]
[--pae on|off]
[--hpet on|off]
[--hwvirtex on|off]
[--hwvirtexexcl on|off]
[--nestedpaging on|off]
[--largepages on|off]
[--vtxvpid on|off]
[--synthcpu on|off]
[--cpuidset <leaf> <eax> <ebx> <ecx> <edx>]
[--cpuidremove <leaf>]
[--cpuidremoveall]
[--hardwareuuid <uuid>]
[--cpus <number>]
[--cpuhotplug on|off]
[--plugcpu <id>]
[--unplugcpu <id>]
[--cpuexecutioncap <1-100>]
[--rtcuseutc on|off]
[--monitorcount <number>]
[--accelerate3d on|off]
[--accelerate2dvideo on|off]
[--firmware bios|efi|efi32|efi64]
[--chipset ich9|piix3]
[--bioslogofadein on|off]
[--bioslogofadeout on|off]
[--bioslogodisplaytime <msec>]
[--bioslogoimagepath <imagepath>]
[--biosbootmenu disabled|menuonly|messageandmenu]
[--biossystemtimeoffset <msec>]
[--biospxedebug on|off]
[--boot<1-4> none|floppy|dvd|disk|net>]
[--nic<1-N> none|null|nat|bridged|intnet|hostonly|
generic]
[--nictype<1-N> Am79C970A|Am79C973|
82540EM|82543GC|82545EM|
virtio]
[--cableconnected<1-N> on|off]
[--nictrace<1-N> on|off]
[--nictracefile<1-N> <filename>]
[--nicproperty<1-N> name=[value]]
[--nicspeed<1-N> <kbps>]
[--nicbootprio<1-N> <priority>]
[--nicpromisc<1-N> deny|allow-vms|allow-all]
[--nicbandwidthgroup<1-N> none|<name>]
[--bridgeadapter<1-N> none|<devicename>]
[--hostonlyadapter<1-N> none|<devicename>]
[--intnet<1-N> <network name>]
[--natnet<1-N> <network>|default]
[--nicgenericdrv<1-N> <driver>
[--natsettings<1-N> [<mtu>],[<socksnd>],
[<sockrcv>],[<tcpsnd>],
[<tcprcv>]]
[--natpf<1-N> [<rulename>],tcp|udp,[<hostip>],
<hostport>,[<guestip>],<guestport>]
[--natpf<1-N> delete <rulename>]
[--nattftpprefix<1-N> <prefix>]
[--nattftpfile<1-N> <file>]
[--nattftpserver<1-N> <ip>]
[--natbindip<1-N> <ip>
[--natdnspassdomain<1-N> on|off]
[--natdnsproxy<1-N> on|off]
[--natdnshostresolver<1-N> on|off]
[--nataliasmode<1-N> default|[log],[proxyonly],
[sameports]]
[--macaddress<1-N> auto|<mac>]
[--mouse ps2|usb|usbtablet
[--keyboard ps2|usb
[--uart<1-N> off|<I/O base> <IRQ>]
[--uartmode<1-N> disconnected|
server <pipe>|
client <pipe>|
file <file>|
<devicename>]
[--guestmemoryballoon <balloonsize in MB>]
[--gueststatisticsinterval <seconds>]
[--audio none|null|dsound]
[--audiocontroller ac97|hda|sb16]
[--clipboard disabled|hosttoguest|guesttohost|
bidirectional]
[--vrde on|off]
[--vrdeextpack default|<name>
[--vrdeproperty <name=[value]>]
[--vrdeport <hostport>]
[--vrdeaddress <hostip>]
[--vrdeauthtype null|external|guest]
[--vrdeauthlibrary default|<name>
[--vrdemulticon on|off]
[--vrdereusecon on|off]
[--vrdevideochannel on|off]
[--vrdevideochannelquality <percent>]
[--usb on|off]
[--usbehci on|off]
[--snapshotfolder default|<path>]
[--teleporter on|off]
[--teleporterport <port>]
[--teleporteraddress <address|empty>
[--teleporterpassword <password>]
VBoxManage modifyvm <uuid|name> [--natpf<1-N> [<rulename>],
tcp|udp, [<hostip>], <hostport>, [<guestip>],<guestport>]
因此設置命令爲 VBoxManage modifyvm Ubuntu --natpf1 「sshpf, tcp, , 2222, 10.0.2.15, 22」
這裏Ubuntu爲Virtual Host名, 1爲對應的網卡編號 sshpf爲自定義映射服務名稱,主機任意IP的2222端口映射到Virtual Host的10.0.2.15 IP的22端口
若要刪除規則則 VBoxManage modifyvm "Ubuntu" --natpf1 delete sshpf
4 Virtual Host安裝ssh服務
yueshen@yueshen-VirtualBox:~$ sudo ps -e | grep ssh
1806 ? 00:00:00 ssh-agent
yueshen@yueshen-VirtualBox:~$ sudo apt-get install openssh-server
yueshen@yueshen-VirtualBox:~$ sudo /etc/init.d/ssh restart
Rather than invoking init scripts through /etc/init.d, use the service(8)
utility, e.g. service ssh restart
Since the script you are attempting to invoke has been converted to an
Upstart job, you may also use the stop(8) and then start(8) utilities,
e.g. stop ssh ; start ssh. The restart(8) utility is also available.
ssh stop/waiting
ssh start/running, process 2860
VBoxManage usbfilter remove <index,0-N>
--target <uuid>|<name>|global
VBoxManage sharedfolder add <vmname>|<uuid>
--name <name> --hostpath <hostpath>
[--transient] [--readonly] [--automount]
VBoxManage sharedfolder remove <vmname>|<uuid>
--name <name> [--transient]
VBoxManage guestproperty get <vmname>|<uuid>
<property> [--verbose]
VBoxManage guestproperty set <vmname>|<uuid>
<property> [<value> [--flags <flags>]]
VBoxManage guestproperty enumerate <vmname>|<uuid>
[--patterns <patterns>]
VBoxManage guestproperty wait <vmname>|<uuid> <patterns>
[--timeout <msec>] [--fail-on-timeout]
VBoxManage guestcontrol <vmname>|<uuid>
exec[ute]
--image <path to program>
--username <name> --password <password>
[--dos2unix]
[--environment "<NAME>=<VALUE> [<NAME>=<VALUE>]"]
[--timeout <msec>] [--unix2dos] [--verbose]
[--wait-exit] [--wait-stdout] [--wait-stderr]
[-- [<argument1>] ... [<argumentN>]]
copyfrom
<source on guest> <destination on host>
--username <name> --password <password>
[--dryrun] [--follow] [--recursive] [--verbose]
copyto|cp
<source on host> <destination on guest>
--username <name> --password <password>
[--dryrun] [--follow] [--recursive] [--verbose]
createdir[ectory]|mkdir|md
<director[y|ies] to create on guest>
--username <name> --password <password>
[--parents] [--mode <mode>] [--verbose]
stat
<file element(s) to check on guest>
--username <name> --password <password>
[--verbose]
updateadditions
[--source <guest additions .ISO>] [--verbose]
VBoxManage debugvm <uuid>|<name>
dumpguestcore --filename <name> |
info <item> [args] |
injectnmi |
osdetect |
osinfo |
getregisters [--cpu <id>] <reg>|all ... |
setregisters [--cpu <id>] <reg>=<value> ... |
statistics [--reset] [--pattern <pattern>]
[--descriptions]
VBoxManage metrics list [*|host|<vmname> [<metric_list>]]
(comma-separated)
VBoxManage metrics setup
[--period <seconds>] (default: 1)
[--samples <count>] (default: 1)
[--list]
[*|host|<vmname> [<metric_list>]]
VBoxManage metrics query [*|host|<vmname> [<metric_list>]]
VBoxManage metrics enable
[--list]
[*|host|<vmname> [<metric_list>]]
VBoxManage metrics disable
[--list]
[*|host|<vmname> [<metric_list>]]
VBoxManage metrics collect
[--period <seconds>] (default: 1)
[--samples <count>] (default: 1)
[--list]
[--detach]
[*|host|<vmname> [<metric_list>]]
VBoxManage hostonlyif ipconfig <name>
[--dhcp |
--ip<ipv4> [--netmask<ipv4> (def: 255.255.255.0)] |
--ipv6<ipv6> [--netmasklengthv6<length> (def: 64)]]
create |
remove <name>
VBoxManage dhcpserver add|modify --netname <network_name> |
--ifname <hostonly_if_name>
[--ip <ip_address>
--netmask <network_mask>
--lowerip <lower_ip>
--upperip <upper_ip>]
[--enable | --disable]
VBoxManage dhcpserver remove --netname <network_name> |
--ifname <hostonly_if_name>
VBoxManage extpack install [--replace] <tarball> | uninstall [--force] <name> | cleanup