This article has participated in the third phase of the “High Production more text” track of the Nuggets Creators Training Camp. Check out the details:The third phase of the Diggability Program | Creators’ camp is underway to “write” about your personal impact.

The background,

Install VirtualBox to create A Linux VM on the host. The Linux network and the gateway share the same IP address segment < bridge mode >. Therefore, the number of IP addresses on the company network may be insufficient. Therefore, you need to specify another IP address for the VM. In addition, the STATIC IP address must be configured for the VM because different office environments (home, Company, and public networks) are changed. If different gateways are used, the IP address will become invalid in other environments. For example, if the virtual environment is in segment 192.168.2, the IP network in segment 192.168.1 cannot be used properly. Therefore, the IP address of the virtual environment needs to be fixed.Copy the code

Two, VirtualBox four network modes

Virtualbox Default virtualBox host-only Indicates the host network only

  • A. Network Address Translation
    • NAT is the default nic environment that VirtualBox uses to create VMS
    • Also configured with port mapping: map the ports of the virtual machine to the host for external machines to access the virtual machine to provide services
  • B. Bridged Adapter Bridge mode
    • The network environment of the host is used as a bridge to directly connect to the network. It allows the virtual machine to be assigned to an independent IP address, all network environment is completely consistent with the host; That is, the vm network in bridge mode depends on the network environment of the host.
  • C. Internal Internal network mode
    • Each VM obtains its IP address using the built-in DHCP server of VirtualBox. Data packets are not transmitted through the network where the host resides, which ensures high security and prevents external packet capture
  • D. Host-only Adapter Host mode
    • The VirtualBox host-only Network interface card (NIC) is used to communicate with the VIRTUAL machine. The VIRTUAL machine uses the IP address as the gateway. Therefore, the LOCAL area Network (LAN) between the local machine and the virtual machine is simulated. This article will also attempt to use the host sharing this adapter as the gateway for the virtual machine.

3. Problems encountered with VirtualBox

1. Solve the problem that VMS do not share the host gateway to realize Internet functions

  • Set the gateway address of the NIC to be the same as the VIRTUalbox host-only IP address of the network adapter. < That is, the IP address of the virtual NIC on the host is the gateway address of the VM NIC. >
Virtualbox host-only network The default gateway of the adapter is 192.168.56.1. If you change the vm network mode to host-only, the gateway may change. In this case, the IP address of the VirtualBox virtual nic will be changed to 192.168.137.1, and the virtual NIC will be changed back to 192.168.56.1. The vm gateway must be the same as the virtualbox host-only NIC.Copy the code
  • Method 2: Add an outdated hardware < Network Adapter >, set a fixed IP address, host network adapter, and configure the Internet to share the added network adapter

Some may say that you need to set the host network adapter fixed IP to Internet sharing! Unnecessary!!!!!!

Step 1: My Computer - Manage, select network adapter, select - Add obsolete hardware; Step 2: Go all the way to The next step to select: Install the hardware I manually selected from the list step 3: Select the network adapter from the drop-down list, select Microsoft -- Microsoft loopback Adapter Step 5: Go back to method 2 and set the host adapter to share the new adapter networkCopy the code
  • Set the network mode with VirtualBox and select the network adapter that is shared by the host Internet to implement the Internet function
  • This leaves one question: Which network mode should be selected to ensure communication between virtual machines while solving the Internet function? Abandon the bridge!!

2. Resolve the problem that the host fails to set the Internet sharing network

  • Select the host network adapter – Properties – Share – Select the new or specified adapter
Note: An error occurred when Internet connection shared access was enabled. Solution: Enter CMD, enter services. MSC, and select Windows FirewalCopy the code

3. Solve the problem of accessing VMS from different machines on the LAN

VBoxManage modifyVM "WINXP" --natpf1 "rule_name, TCP,8081, 8080" The second is for the virtual machine. This means that the port of the VIRTUAL machine is mapped to the host machine so that other machines can access the services of the virtual machine by accessing the IP address and port of the host machine. Between the two commas are the IP addresses of the host and vm.Copy the code
  • Don’t try, I didn’t succeed anyway!! Keep reading, there will be a solution!!

4. Resolve the problem that the VM cannot access the Internet

  • First, check whether the network mode of the VM supports the Internet
  • Whether to manually set the network mode that does not support the Internet or select the network mode that supports the Internet
Virtualbox A VM has two default nics: NIC 1: The default NETWORK address translation (NAT) IP address is 10.0.2.15. If multiple VMS are started, the static IP address must be set. BOOTPROTO=STATIC NOBOOT=yes IPADDR=10.0.2.x NETMASK=255.255.255.0 GATEWAY=10.0.2.2 DNS #DNS1=8.8.8.8 #DNS2=114.114.114 The schema is determined by the current relationship between the virtual machine and the host machine, or the network environment that the virtual machine wants to use.Copy the code

5. Solve the problem that the HOST LAN machine can access the VM

VMS created on the LAN, even for the development environment, need to be accessible to other users.

  • Regarding the firewall, in the experiment, whether open or not should not affect other machines in the LAN to access the virtual machine service
  • You can configure the firewall only after configuring the network adapter of the host to share the VM and entering the service list
    • Services. MSC, select Windows Firewall startup
  • For static IP addresses, do not choose the bridge mode to avoid wasting LAN IP resources.
  • Set the default gateway of the first NIC to 10.0.2.2 instead of 10.0.2.1
  • The second nic can be in bridge or host-only mode to avoid WASTING IP resources on the LAN and support the Internet
    • You need to set the NETWORK interface card (NIC) of the Internet sharing VM on the host or add an outdated NIC to the HOST
    • The VM NICS are preferentially shared, eliminating the need to add outdated hardware nics to the OS
  • NAT and port mapping enable other LAN machines to access VM services
    • Note that the firewall access rules require open access ports

6. Solve the problem of accessing the VM from other machines on the LAN

Do not repeat the question: this means that the LAN can be accessed through IP, so other machines can directly access the VIRTUAL machine IP?

  • Although the network mode of the VIRTUAL machine is shared with the host computer over the Internet, other machines can access the services provided by the ports exposed by the virtual machine by accessing the host computer’s IP address.
  • However, how do you make it possible for others to access virtual machine services remotely, or without directly accessing the host IP?
Solution: In general, we use nginx! So we set the domain name to access the virtual machine, now the proxy server has configured the domain name; Back to the host, set the hosts file, and map the domain name to the VM IP address to access the VM using the domain name.Copy the code
  • An explanation is modified under /etc/hosts on Linux
Vm IP Nginx Configure the domain name and then access the domain name to the VM from other machines? No. You can only use this domain name to access the VM on the local PC! Don't believe it? Ping the configured domain name to check whether the resolved IP address is the IP address of the VM.Copy the code
  • In the other case, modify the hosts on the host that accesses the VM
Modify the hosts directory: C:\Windows\System32\drivers\etc format: VM IP address The domain name address test configured by VM Nginx: ping the domain name, the IP address resolved is the VM IP address, and then access the host. If the response is correct, the host is successfully accessed.Copy the code