频道栏目
首页 > 资讯 > 虚拟机 > 正文

VirtualBox网络NAT模式的端口映射设置

11-10-24        来源:[db:作者]  
收藏   我要投稿

 

VirtualBox的提供了四种网络接入模式,它们分别是:

1、NAT 网络地址转换模式(NAT,Network Address Translation)

2、Bridged Adapter 桥接模式

3、Internal 内部网络模式

4、Host-only Adapter 主机模式

 

其中NAT模式是最简单的实现虚拟机上网的方式,可以认为Virtual Host访问网络的所有数据都是由主机提供,但是却并不真实存在于网络中,主机与网络中的任何机器都不能查看和访问到Virtual Host的存在。

 

 

Virtual Host与主机关系:

只能单向访问,虚拟机可以通过网络访问到主机,主机无法通过网络访问到虚拟机。

 

 

Virtual Host与网络中其他Virtual Host的关系:

只能单向访问,虚拟机可以访问到网络中其他主机,其他主机不能通过网络访问到虚拟机。

 

 

Virtual Host与Virtual Host之间的关系:

相互不能访问,虚拟机与虚拟机各自完全独立,相互间无法通过网络访问彼此。

 

 

NAT方案优缺点:

笔记本已插网线时: 虚拟机可以访问主机,虚拟机可以访问互联网

 

笔记本没插网线时: 主机的“本地连接”有红叉的,虚拟机可以访问主机,虚拟机不可以访问互联网

 

But, 做了端口映射后,两种方式主机均可以访问虚拟机上的服务(如SSH Web DB 等)

 

 

 

譬如我需要用Secure CRT通过NAT方式连接到Virtual Box 的ssh服务

 

1  查看NAT网卡的地址

 

连接到在我的电脑上,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

 

 

 

 

                            [--productid <XXXX>|""]

                            [--revision <IIFF>|""]

                            [--manufacturer <string>|""]

                            [--product <string>|""]

                            [--remote yes|no] (null, VM filters only)

                            [--serialnumber <string>|""]

                            [--maskedinterfaces <XXXXXXXX>]

 

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

 

摘自 AlphaJay的博客

相关TAG标签
上一篇:ubuntu上网慢的问题
下一篇:ubuntu shared folder to windows
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站