端口转发 虚拟机
VirtualBox and VMware both create virtual machines with the NAT network type by default. If you want to run server software inside a virtual machine, you’ll need to change its network type or forward ports through the virtual NAT.
默认情况下,VirtualBox和VMware都使用NAT网络类型创建虚拟机。 如果要在虚拟机中运行服务器软件,则需要更改其网络类型或通过虚拟NAT转发端口。
Virtual machines don’t normally need to be reachable from outside the virtual machine, so the default is fine for most people. It actually provides some security, as it isolates the virtual machine from incoming connections.
通常不需要从虚拟机外部访问虚拟机,因此对于大多数人而言,默认设置是合适的。 它实际上提供了一定的安全性,因为它将虚拟机与传入的连接隔离开来。
选择桥接网络 (Selecting Bridged Networking)
With the NAT network type, your host operating system performs network address translation. The virtual machine shares your host computer’s IP address and won’t receive any incoming traffic. You can use bridged networking mode instead — in bridged mode, the virtual machine will appear as a separate device on your network and have its own IP address.
使用NAT网络类型,您的主机操作系统将执行网络地址转换。 虚拟机共享您的主机的IP地址,并且不会收到任何传入流量。 您可以改用桥接网络模式-在桥接模式下,虚拟机将在网络上显示为单独的设备,并具有自己的IP地址。
To change a virtual machine’s network type in VirtualBox, right-click a virtual machine and select Settings. If you can’t click Settings, you’ll need to power off the virtual machine before making these change.
要在VirtualBox中更改虚拟机的网络类型,请右键单击虚拟机,然后选择设置。 如果无法单击“设置”,则需要先关闭虚拟机的电源,然后再进行这些更改。
Select the Bridged adapter network mode in the Network settings section and click OK. For more information about each type of network mode, consult the Introduction to networking modes section in VirtualBox’s manual.
在“网络设置”部分中选择桥接适配器网络模式,然后单击“确定”。 有关每种类型的网络模式的更多信息,请参阅VirtualBox手册中的“ 网络模式简介”部分 。
The process is similar in VMware applications. First, right-click a powered off virtual machine and select Virtual Machine Settings.
在VMware应用程序中,此过程类似。 首先,右键单击已关闭电源的虚拟机,然后选择“虚拟机设置”。
Select the Network Adapter virtual hardware device, select the Bridged network connection type, and click OK.
选择网络适配器虚拟硬件设备,选择桥接网络连接类型,然后单击确定。
将端口转发到虚拟机 (Forwarding Ports to a Virtual Machine)
If you prefer to use NAT network mode for some reason, you can also forward ports through the virtual machine’s NAT. Note that only one of these steps is necessary – you don’t have to forward ports if you enabled bridged networking above.
如果出于某种原因更喜欢使用NAT网络模式,则还可以通过虚拟机的NAT转发端口。 请注意,仅这些步骤之一是必要的-如果您在上面启用了桥接网络,则不必转发端口。
At one point, you had to use the VBoxManage command to forward ports in VirtualBox, but VirtualBox now contains a simple graphical port-forwarding window. If you need to set up port forwarding with the VBoxManage command instead of using the graphical interface, you’ll find instructions for doing so in VirtualBox’s manual.
曾经,您必须使用VBoxManage命令在VirtualBox中转发端口,但是VirtualBox现在包含一个简单的图形端口转发窗口。 如果您需要使用VBoxManage命令而不是使用图形界面来设置端口转发,则可以在VirtualBox的手册中找到有关执行此操作的说明。
To forward ports in VirtualBox, first open a virtual machine’s settings window by selecting the Settings option in the menu.
要转发VirtualBox中的端口,请首先通过选择菜单中的“设置”选项来打开虚拟机的设置窗口。
Select the Network pane in the virtual machine’s configuration window, expand the Advanced section, and click the Port Forwarding button. Note that this button is only active if you’re using a NAT network type – you only need to forward ports if you’re using a NAT.
在虚拟机的配置窗口中选择“网络”窗格,展开“高级”部分,然后单击“端口转发”按钮。 请注意,此按钮仅在使用NAT网络类型时才处于活动状态–仅在使用NAT时才需要转发端口。
Use VirtualBox’s Port Forwarding Rules window to forward ports. You don’t have to specify any IP addresses – those two fields are optional.
使用VirtualBox的“端口转发规则”窗口来转发端口。 您无需指定任何IP地址-这两个字段是可选的。
Note: While you don’t have to enter any IP details, leaving the Host IP box blank will make VirtualBox listen on 0.0.0.0—in other words, it will accept all traffic from the local network and forward it to your virtual machine. Enter 127.0.0.1 in the Host IP box and VirtualBox will only accept traffic originating on your computer—in other words, on the host operating system.
注意 :虽然不必输入任何IP详细信息,但将Host IP(主机IP)框保留为空白将使VirtualBox侦听0.0.0.0,换句话说,它将接受来自本地网络的所有流量并将其转发到您的虚拟机。 在“主机IP”框中输入127.0.0.1 ,VirtualBox将仅接受源自您计算机的流量,即主机操作系统上的流量。
If you’re using VMware, you’ll need to use the Virtual Network Editor (vmnetcfg) application to do this. VMware Workstation users can select Edit –> Virtual Network Editor to open it.
如果使用的是VMware,则需要使用虚拟网络编辑器(vmnetcfg)应用程序来执行此操作。 VMware Workstation用户可以选择编辑–>虚拟网络编辑器将其打开。
Unfortunately, this utility is not included with VMware Player. There are methods for installing it yourself by extracting the vmnetcfg.exe file from the VMware Player installer, but I couldn’t locate the vmnetcfg.exe utility in the latest version of VMware Player, VMware Player 5.0 – it may no longer be included in the installer. If you’re using VMware Player, you can still use bridged networking mode to make your server accessible.
不幸的是,该实用程序不包含在VMware Player中。 有一些方法可以通过从VMware Player安装程序中提取vmnetcfg.exe文件来自己安装,但是我无法在最新版本的VMware Player VMware Player 5.0中找到vmnetcfg.exe实用程序-它可能不再包含在其中。安装程序。 如果使用的是VMware Player,您仍然可以使用桥接网络模式来访问服务器。
Remember that this is only part of the process of making the server software inside a virtual machine reachable. You’ll also need to ensure that:
请记住,这只是使虚拟机中的服务器软件可访问的过程的一部分。 您还需要确保:
- The firewall software running inside your virtual machine isn’t blocking the connections. (You may need to allow the server program in the guest operating system’s firewall.) 虚拟机内部运行的防火墙软件不会阻止连接。 (您可能需要允许服务器程序进入客户机操作系统的防火墙。)
- The firewall software on your host computer isn’t blocking the connections. (This only applies to NAT mode with port forwarding – the host computer’s firewall doesn’t interfere in bridged networking mode.) 主机上的防火墙软件不会阻止连接。 (这仅适用于具有端口转发功能的NAT模式–主机的防火墙不会干扰桥接网络模式。)
-
Your router is forwarding ports correctly – this is only necessary if you want to access the virtual machine from the Internet. (Consult our guide to forwarding ports on routers here.)
您的路由器正确转发了端口–仅在您要从Internet访问虚拟机时才需要这样做。 ( 在此处咨询我们的指南,以转发路由器上的端口 。)
翻译自: https://www.howtogeek.com/122641/how-to-forward-ports-to-a-virtual-machine-and-use-it-as-a-server/
端口转发 虚拟机