virtualbox关于网络设置的文档:https://www.virtualbox.org/manual/topics/networkingdetails.html#networkingdetails
DHCP
Dynamic Host Configuration Protocol:动态主机配置协议,是专门用来给网络中的节点分发IP地址,确保每个节点的IP地址都是唯一。从协议字面意思来理解"动态"意味着自动处理,当网络环境发生变化时,可以自动调整而不需要人为的干预。比如新节点的加入/离开,DHCP Server可以自动的回收/分发对应IP地址。
它分为两个部份:一个是服务器端,而另一个是客户端。所有的 IP 网络设定数据都由 DHCP 服务器集中管理,并负责处理客户端的 DHCP 要求;而客户端则会使用从服务器分配下来的IP环境数据。
DHCP是局域网的标配,会自动给每个电脑分配动态的IP,意味着你每次启动电脑得到的局域网IP是不同的,如果你希望自己得IP能够固定下来,则需要进行一些设置,步骤如下。
控制面板–>网络和共享中心–>更改适配器设置–>我这里是以太网3
右键以太网3,选择“状态”,查看相关信息,或者点击屏幕右下角的网络图标获取信息。
右键以太网3,选择“属性”,按如下步骤输入信息,选择“使用下面的IP地址”,即放弃DHCP协议。
IP地址是要输入的,其他都是根据上面查到的信息填写。
1、内部网络 Internal
多台虚拟机组成一个局域网,他们无法与主机相互访问,也无法与公网相互访问。
NAT 网络:它是 Internal 模式的变种,忽略。与下面的NAT网络地址转换是不同的东西。
2、Host-only Adapter〈宿主机模式〉
在Host-Only模式下,一台宿主机和多台虚拟机构成了一个局域网,他们之间可以相互访问,但是这些虚拟机都无法访问此局域网之外的网络,即无法访问互联网。
在此模式下需要选择一个“主机网络管理器”,在主界面中的管理-->主机网络管理器
,可以设置网络管理器。
3、NAT 网络地址转换
Network Address Translation,在没有任何设置的情况下,这是虚拟机默认的网络模式,一个局域网可能有几百台机器,他们要想访问公网,只需要一个公网IP即可,这就是NAT的工作,如果你不设置NAT,那内网IP在公网是无法识别的,也就无法上网。这种方式虚拟机可以上外网,如果宿主机想要访问虚拟机需要设置端口转发。
4、桥接网络 Bridged Adapter
在桥接模式下,虚拟机会选择一个宿主机上的网卡来进行网络数据通信,虚拟机ip地址需要与主机在同一个网段,如果需要联网,则网关与DNS需要与主机网卡一致。同一网段的IP都可以相互通信,包括虚拟机与主机。
在设置桥接网络的时候需要选择一个网卡。第一个是有线网络的,第二个带有 Wireless 字样的是无线网络,这个要跟实际相符。
5、对比
Introduction to Networking Modes
Each of the networking adapters can be separately configured to operate in one of the following modes:
-
Not attached. In this mode, Oracle VirtualBox reports to the guest that a network card is present, but that there is no connection. This is as if no Ethernet cable was plugged into the card. Using this mode, it is possible to pull the virtual Ethernet cable and disrupt the connection, which can be useful to inform a guest operating system that no network connection is available and enforce a reconfiguration.
-
Network Address Translation (NAT). If all you want is to browse the Web, download files, and view email inside the guest, then this default mode should be sufficient for you, and you can skip the rest of this section. Please note that there are certain limitations when using Windows file sharing. See NAT Limitations.
-
NAT Network. A NAT network is a type of internal network that allows outbound connections. See Network Address Translation Service.
-
Bridged networking. This is for more advanced networking needs, such as network simulations and running servers in a guest. When enabled, Oracle VirtualBox connects to one of your installed network cards and exchanges network packets directly, circumventing your host operating system’s network stack.
-
Internal networking. This can be used to create a different kind of software-based network which is visible to selected virtual machines, but not to applications running on the host or to the outside world.
-
Host-only networking. This can be used to create a network containing the host and a set of virtual machines, without the need for the host’s physical network interface. Instead, a virtual network interface, similar to a loopback interface, is created on the host, providing connectivity among virtual machines and the host.
-
Cloud networking. This can be used to connect a local VM to a subnet on a remote cloud service.
-
Generic networking. Rarely used modes which share the same generic network interface, by allowing the user to select a driver which can be included with Oracle VirtualBox or be distributed in an extension pack.
The following submodes are available:
- UDP Tunnel: Used to interconnect virtual machines running on different hosts directly, easily, and transparently, over an existing network infrastructure.
- VDE (Virtual Distributed Ethernet) networking: Used to connect to a Virtual Distributed Ethernet switch on a Linux or a FreeBSD host. At the moment this option requires compilation of Oracle VirtualBox from sources, as the Oracle packages do not include it.
Port forward
为端口转发,意思就是如果宿主机想要访问虚拟机需要设置端口转发
。虽然NAT模式下可以通过此种方式来实现宿主机对虚拟机的访问,但是,如果虚拟机上运行了多个API服务,那就需要设置多个端口转发,所以不太适用。
NAT模式下,VM可以访问Host,此时Host的IP为10.0.2.2
,比如VM运行WEB服务器,Host运行数据库服务。
NAT网络对应的标识符为ifcfg-enp0s3
。
桥接网络或Host-only对应的标识符为ifcfg-enp0s8
。
一般情况下,我们使用NAT + Host-only
组合,网卡1使用NAT访问外网,网卡2使用Host-only满足VM和Host之间的通讯,然后在Host-only可以设置使用DHCP动态分配IP还是使用固定的IP。
无论是桥接模式还是Host-only都可以设置固定IP,需要修改配置文件。
/etc/sysconfig/network-scripts/ifcfg-enp0s8
#VAGRANT-BEGIN
# The contents below are automatically generated by Vagrant. Do not modify.
NM_CONTROLLED=yes
BOOTPROTO=none
ONBOOT=yes
IPADDR=192.168.2.200
NETMASK=255.255.255.0
DEVICE=enp0s8
PEERDNS=no
#VAGRANT-END