VirtualBox NAT设置和端口转发

本文详细介绍了VirtualBox中网络地址转换(NAT)的工作原理及配置方法。包括虚拟机如何通过NAT连接互联网、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 更灵活,更强大。

<think>好的,用户的问题是关于在Windows系统中通过NAT高级设置给Ubuntu虚拟机添加端口转发规则,以便连接虚拟机。用户之前已经提供了引用内容,其中涉及VirtualBoxVMware的NAT配置,以及一些实际案例。需要结合这些引用之前的回答来给出解决方案。 首先,用户的需求是在NAT模式下配置端口转发,可能使用的是VirtualBox或者VMware。根据引用[1][4],用户提到在VirtualBox中通过NAT端口转发访问Ubuntu,而引用[3]提到了VirtualBoxVMware的SSH登录配置。所以需要分别针对这两种虚拟机软件给出步骤。 之前的回答中,用户已经提到过如何在VMwareVirtualBox中配置端口转发,比如在VMware的NAT高级设置中添加规则,或者在VirtualBox中使用命令行或图形界面设置。现在用户再次询问具体的配置步骤,可能需要更详细的指导。 需要确保步骤清晰,分点列出,针对不同的虚拟机软件分别说明。同时,用户可能遇到防火墙问题或者配置后无法连接的情况,所以需要包含验证步骤常见问题解决。 另外,用户引用的内容中有提到使用Xshell连接虚拟机的问题,以及NAT模式下的网络限制,比如开发板无法访问Ubuntu,这可能影响用户的配置,但当前问题主要是端口转发,所以需要专注于如何正确设置规则,并确保端口正确映射。 还要注意用户可能混淆了VirtualBoxVMware的配置方法,需要明确区分两者,并提供对应的截图或命令行示例。例如,引用[4]中提到了VirtualBox端口转发规则设置,包括SSH数据端口转发,这可能是一个示例,需要指导用户如何根据自己的需求添加类似的规则。 最后,需要验证步骤,比如在Windows中使用Test-NetConnection或telnet来测试端口是否开放,以及检查Ubuntu的SSH服务是否运行,防火墙是否允许连接等。这些步骤能帮助用户确认配置是否正确。 总结下来,回答需要分为VirtualBoxVMware两大部分,每部分详细说明配置步骤,包括图形界面命令行方法,验证方法,以及常见问题的解决方案。同时,要引用提供的引用内容中的相关信息,确保回答准确可靠。</think>根据您的需求及引用内容,以下是针对 **VirtualBox** **VMware** 在 **NAT模式** 下配置端口转发规则的详细步骤: --- ### **一、VirtualBox 配置端口转发(引用[1][4])** #### **步骤1:打开虚拟机网络设置** 1. 关闭Ubuntu虚拟机(修改网络配置需关机) 2. 进入VirtualBox主界面 → 选中Ubuntu虚拟机 → 点击 **设置** → **网络** 3. 确保 **网卡1** 连接方式为 **NAT**(默认模式) #### **步骤2:添加端口转发规则** 1. 在 **网络** 设置界面 → 点击 **高级** → **端口转发** 2. 在弹出的窗口中添加规则(以SSH为例): ```plaintext 规则名称:SSH(自定义) 协议:TCP 主机IP:127.0.0.1(默认本地回环地址) 主机端口:10022(Windows端访问端口) 子系统IP:留空(自动映射虚拟机IP) 子系统端口:22(Ubuntu的SSH端口) ``` ![VirtualBox端口转发示例](https://example.com/vbox-port-forward.png) *(注:规则格式参考引用[4]中的SFTP转发示例)* #### **步骤3:验证规则生效** 1. 启动Ubuntu虚拟机,确保SSH服务已开启: ```bash sudo systemctl status ssh # 确认Active: active (running) ``` 2. 在Windows中通过Xshell或PowerShell连接: ```powershell ssh ubuntu_user@127.0.0.1 -p 10022 # 输入虚拟机用户名密码 ``` --- ### **二、VMware 配置端口转发(引用[1][2])** #### **步骤1:编辑虚拟机NAT设置** 1. 关闭Ubuntu虚拟机 → 进入VMware主界面 → **编辑** → **虚拟网络编辑器** 2. 选择 **NAT模式** 对应的网络(如VMnet8)→ 点击 **NAT设置** #### **步骤2:添加端口映射** 1. 在NAT设置界面 → **添加** → 填写以下参数(以SSH为例): ```plaintext 主机端口:10022 类型:TCP 虚拟机IP地址:Ubuntu的IP(如192.168.1.100) 虚拟机端口:22 ``` ![VMware端口转发示例](https://example.com/vmware-nat.png) *(引用[2]提到NAT模式需手动映射IP端口)* #### **步骤3:测试连接** 1. 启动Ubuntu,检查IP是否匹配规则: ```bash ip addr show eth0 # 查看实际IP ``` 2. 在Windows中执行: ```powershell Test-NetConnection -ComputerName 127.0.0.1 -Port 10022 # 应返回成功 ``` --- ### **三、关键问题排查(引用[1][3])** | 问题现象 | 解决方案 | |--------------------------|--------------------------------------------------------------------------| | **连接超时** | 1. 检查Ubuntu防火墙:`sudo ufw allow 22`<br>2. 确认虚拟机SSH服务已启动 | | **主机端口被占用** | 更换主机端口(如10022 → 10023) | | **NAT模式无法获取IP** | 重启虚拟机网络:`sudo netplan apply`(Ubuntu 18.04+) | | **Xshell提示拒绝连接** | 检查用户名/IP是否正确,并确认端口转发规则中的协议(TCP/UDP)是否匹配 | --- ### **四、高级场景扩展** #### **1. 多服务映射(如Web服务)** - **VirtualBox规则示例**: ```plaintext 名称:HTTP 协议:TCP 主机端口:8080 → 虚拟机端口:80 ``` - **VMware规则示例**: 添加新规则,映射主机8080到虚拟机80端口。 #### **2. 动态端口转发(SSH隧道)** 通过SSH直接建立隧道(无需配置NAT规则): ```powershell ssh -L 10022:localhost:22 ubuntu_user@虚拟机IP # 引用[3]类似场景 ``` --- ### **五、两种方案的对比** | 特性 | VirtualBox | VMware | |---------------------|-----------------------------------------|-----------------------------------------| | **配置入口** | 图形界面+命令行(`VBoxManage`) | 图形界面(需管理员权限) | | **IP动态适配** | 需手动指定或依赖DHCP | 支持自动分配(VMnet8子网) | | **跨平台兼容性** | 支持Windows/macOS/Linux | 仅限Windows/Linux(Workstation版) | | **企业级扩展** | 较弱 | 支持vSphere集成(如ESXi) | --- 通过上述步骤,您可以在NAT模式下实现Windows对Ubuntu虚拟机的稳定访问。实际选择VirtualBox或VMware时,可根据开发场景复杂度决定(轻量级测试推荐VirtualBox,企业级环境推荐VMware)[^1][^4]。 --- **相关问题** 1. 如何在NAT模式下实现虚拟机与宿主机的双向文件传输? 2. VMware的NAT模式与桥接模式在网络配置上有何本质区别? 3. VirtualBox端口转发规则能否通过命令行批量配置?
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值