博威---云架构决胜云计算

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
查看: 1928|回复: 6

virtualbox配置NAT方式的端口转发

[复制链接]
发表于 2010-3-8 16:20:14 | 显示全部楼层 |阅读模式
近日安装Virtualbox,在其中安装win2k3操作系统,默认情况用的是NAT方式网络连接,可不需配置即可上网。但主机与虚拟机间不能PING通
虚拟机IP 10.0.2.15 网关 10.0.2.2
查看virtualbox用户手册,找到以下内容
解决问题

6.3.1 Configuring port forwarding with NAT
As the virtual machine is connected to a private network internal to VirtualBox and
invisible to the host, network services on the guest are not accessible to the host ma-
chine or to other computers on the same network. However, VirtualBox can make
selected services available outside of the guest by using port forwarding. This means
that VirtualBox listens to certain ports on the host and resends all packets which arrive
on them to the guest on the ports used by the services being forwarded.
   To an application on the host or other physical (or virtual) machines on the network,
it looks as though the service being proxied is actually running on the host (note that
this also means that you cannot run the same service on the same ports on the host).
However, you still gain the advantages of running the service in a virtual machine
– for example, services on the host machine or on other virtual machines cannot be
compromised or crashed by a vulnerability or a bug in the service, and the service can
run in a different operating system than the host system.
   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
use 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 an ssh server on the guest requires the following three commands:
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
   The above example assumes a PCNet virtual network card; if you have configured
the guest to use the Intel PRO/1000, replace “pcnet” with “e1000” in the above com-
mands and use “virtio-net” if you configured the guest to use the Paravirtualized Net-
work device. Similarly, if you want to configure a different interface instance replace
the /0/ with the appropriate index. pcnet, e1000 and virtio-net are counted sepa-
rately in this respect, and counting starts at 0 for these types.
   The name guestssh is an arbitrary one chosen for this particular forwarding con-
figuration. With that configuration in place, all TCP connections to port 2222 on the
host will be forwarded to port 22 on the guest. Protocol can be either of TCP or UDP
(these are case insensitive). To remove a mapping again, use the same commands, but
leaving out the values (in this case TCP, 22 and 2222).
   It is not possible to configure incoming NAT connections while the VM is running.
However you can change the settings for a VM which is currently saved (or powered
off at a snapshot).
 楼主| 发表于 2010-3-8 16:45:36 | 显示全部楼层
本帖最后由 bbl164 于 2010-3-8 17:19 编辑

由于虚拟主机被连接到了Virtualbox的一个私有内部网络,并县对于宿主机是不可见的,因此宿主机或其他同一网络中的计算机不能访问虚拟主机(客户机)上的网络服务。然而,利用端口转发,Virtualbox可以使外部主机访问特定的服务。即外部主机只能访问虚拟主机经过转发的服务。
以SSH服务为例,使用以下三条命令即可实现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
其中 pcnet 根据相应情况更改,为你所使用的网卡
Screenshot-5.png

Screenshot-freebsd - 设置.png

Linux Guest为客户虚拟机名称
如果你配置的客户机使用的网卡是Intel PRO/1000,将pcnet替换为e1000
如果你配置的客户机使用的是Paravirtualized,将pcnet替换为virtio-net

命令执行完成后,一定要重启虚拟机

用法 宿主机中执行 ssh localhost -p 2222
 楼主| 发表于 2010-3-8 16:46:11 | 显示全部楼层
virtualbox与vmware的不同
发表于 2010-3-8 19:20:32 | 显示全部楼层
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|boway Inc. ( 冀ICP备10011147号 )

GMT+8, 2024-5-4 12:53 , Processed in 0.101811 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表