Centos 8 virtual machine network configuration and cloning under VMware Pro

Network configuration is very troublesome for novices, so we record the virtual machine network configuration under VMware Pro, and also record the process of cloning the virtual machine. This is very useful for the deployment of test clusters. Note that it must be VMware Pro, because the free version of Player does not have network configuration functions.

Environment installation

VMware Pro installation

Install Centos

Whether it is creating a virtual machine or centos installation wizard, they are similar to those in VMware Player, with some differences, so I won’t record too much here;

Reference: Installation and configuration of CentOS under VMware 17 Player

Network Configuration

VMware’s three network connection modes

When it comes to VMware's network connection mode, there are usually three common modes: Bridge Mode (Bridge Mode), NAT Mode (Network Address Translation Mode) and Host Mode (Host-Only Mode).

  1. Bridge Mode : In bridge mode, the virtual machine acts like an independent device connected to the real network. It gets a separate IP address from the real network and can interact directly with other devices on the real network. This means that the virtual machine can use network resources and access other devices just like a real device.

    For example, your computer is connected to the router via Wi-Fi, and you are running a virtual machine in VMware. In bridge mode, the virtual machine gets a separate IP address from the router and is able to communicate with other devices on the LAN. This means your virtual machine can browse the internet, share files with other devices, and more just like a real device.

    Advantages: The virtual machine is fully integrated with the real network, making it easy to communicate and share resources with other devices.

    Disadvantages: The virtual machine is exposed to the external network and has slightly lower security.

  2. NAT mode (Network Address Translation Mode) : In NAT mode, the virtual machine uses the host's IP address and maps it to the host's network interface. The virtual machine can access the Internet and other devices on the LAN through the shared IP address, but the external network cannot directly access the virtual machine.

    For example, your computer is connected to the Internet through a router and is running a virtual machine in VMware. In NAT mode, the virtual machine shares the same IP address with the host and communicates with external networks through Network Address Translation (NAT) technology. The virtual machine can access the Internet and other devices on the LAN, but the external network can only see the IP address of the host and cannot directly access the virtual machine.

    Advantages: Provides a certain level of security because the external network cannot directly access the virtual machine.

    Disadvantages: Limited communication between virtual machines and between virtual machines and external networks.

  3. Host-Only Mode : In host mode, the virtual machine and the host form an independent network, and the external network cannot directly access the virtual machine. Communication between virtual machines and between virtual machines and the host machine is possible, but they cannot connect to external networks.

    For example, you run two virtual machines simultaneously in VMware and configure them in host mode. This means that the two virtual machines can communicate with each other as if they were connected to the same switch. However, the external network cannot directly access these virtual machines, and they form a relatively independent network.

    Advantages: Provides isolation and security, external networks cannot directly access the virtual machine.

    Disadvantages: Virtual machines cannot access resources and services on the external network.

Network card introduction

After installing VMware, you can see the two virtual network cards in our control panel;

As shown in the figure below, the three network cards are enabled, and the WLAN behind them is the actual enabled network card;

Insert image description here

  1. VMware Network Adapter VMnet1:
    • VMnet1 is a default network adapter for VMware Workstation virtual networks.
    • It is usually used in bridge mode, which allows the virtual machine to connect to the physical network to which the host machine is connected.
    • Using VMnet1, a virtual machine can obtain an IP address on the network to which the host is connected and communicate with other devices.
  2. VMware Network Adapter VMnet8:
    • VMnet8 is another default network adapter for VMware Workstation virtual networks.
    • It is typically used in NAT (Network Address Translation) mode, where the virtual machines share the host's IP address but have a separate LAN subnet.
    • Virtual machines in VMnet8 can route their network traffic between the host and other network devices through NAT.
  3. WLAN network card:
    • The WLAN network card is the wireless network adapter of the host computer and is used to connect to the wireless network.

Check the address allocation mode of the network card:

Right-click the virtual network card->View properties->Select Internet Protocol version 4 (TCP/IPv4)->View properties

After checking, you can see that both virtual network cards are automatically assigned IP addresses ;

Insert image description here

Virtual machine configuration fixed IP

After reading the introduction of the three network connection modes above, it can be clearly seen that the best connection mode to set a fixed IP is the NAT mode ;

In bridge mode, the specified static IP address of the virtual machine needs to be valid and unoccupied in the connected real network . If there is an IP address conflict with another device, it may cause network connectivity problems. There is no way to completely set this to a fixed IP, and the IP is different in different network environments;

The host mode cannot access the external network, let alone;

Let’s start the configuration!

Modify vmware configuration

  • First open Vmware’s network editor

Insert image description here

  • Grant permission to change settings

Insert image description here

  • Select NAT mode network and cancel using local DHCP to automatically assign IP addresses.

Insert image description here

  • Set the subnet IP. After setting, click NAT settings.

    Generally 192.168.0.0 to 192.168.254.254.

    Do not fill in 0 or 255 in the fourth digit of the IP address. Fill in 0 to connect remotely but not be able to access the Internet. The IP address should not be the same as the gateway IP.

    In addition, you need to ensure that the set subnet IP does not conflict with the IP of other devices in the physical network to avoid IP address conflicts.

    It needs to be added here that the first two digits here do not necessarily have to be 192.168, nor do they have to be consistent with the first two digits of the host IP, as long as they meet the basic requirements (testing in VMware 17 pro and Centos 8 environments is no problem, if the conclusion is wrong , please leave a message for feedback!)

Insert image description here

  • Enter the NAT settings page to adjust the gateway IP. After setting, click OK.

Insert image description here

  • Click Apply or OK and wait for configuration;

Insert image description here

  • After the configuration is completed, without proceeding to the next step, simply take a look at the IPv4 attributes of VMnet 8 in the host network card:

    You can find that the IP here has been adjusted to the IP we set. Basically, you don’t need to make other settings here, including dns;

Insert image description here

Modify centos network configuration file

  1. Open a terminal or command prompt in the virtual machine and perform the following steps using the root user or an account with administrator privileges.

    Edit the network configuration file: Use a text editor to open the network configuration file, which on most Linux distributions is located in /etc/sysconfig/network-scripts/the directory and named ifcfg-eth0or something similar.

    vim /etc/sysconfig/network-scripts/ifcfg-eth0
    

    Different centos versions in this area will have different configurations in the file. The following are some common configurations:

    In CentOS 8, the network configuration files are still located /etc/sysconfig/network-scripts/in the directory, but compared with CentOS 7, CentOS 8 introduces the default management of NetworkManager in terms of network configuration.

    1. TYPE: Specifies the interface type. For Ethernet interfaces, the type is Ethernet.
    2. NAME: Specifies the name of the interface. For example, NAME=eth0indicates that the configuration file is for a network interface named eth0.
    3. BOOTPROTO: Specifies the startup protocol of the interface and determines how to obtain the IP address. Common options include:
      • none: Set static IP address manually.
      • dhcp: Obtain an IP address automatically through DHCP.
      • bootp: Automatically obtain an IP address through BOOTP.
      • static: Use a static IP address.
    4. DEVICE: Specify the name of the device, which is the same as the NAME configuration.
    5. ONBOOT: Specifies whether the interface is automatically enabled when the system starts.
      • yes: Automatically enable this interface when the system starts.
      • no: Do ​​not automatically enable this interface when the system starts.
    6. IPADDR: used to configure static IP address. The format is IP address, for example IPADDR=192.168.1.100.
    7. PREFIX: Specifies the subnet prefix length. For example, for a subnet mask of 255.255.255.0, the corresponding prefix length is 24. PREFIX or NETMASK can be used.
    8. GATEWAY: Specify the IP address of the default gateway.
    9. DNS1/DNS2: Specify the IP addresses of the primary and secondary DNS servers.
    10. IPV6INIT: Whether to enable IPv6 support.
      • yes: Enable IPv6 support.
      • no: Disable IPv6.
    11. IPV6_AUTOCONF: Whether to obtain the IPv6 address through automatic configuration.
      • yes: Obtain the IPv6 address through automatic configuration.
      • no: Disable automatic configuration and require manual configuration of the IPv6 address.
    12. IPV6_PEERDNS: Whether to obtain DNS server information from IPv6 neighbors.
    13. IPV6_PEERROUTES: Whether to obtain routing information from IPv6 neighbors.
    14. DEFROUTE: Specifies whether the interface is the default routing interface.
      • yes: It is the default routing interface.
      • no: Not the default routing interface.

    Check out the configuration of our virtual machine here:
    Insert image description here

  2. Find and edit the following lines in the file:

    BOOTPROTO="dhcp"
    

    Change to:

    BOOTPROTO="static"
    

    Add the following lines at the end of the file to set the IPv4 address, subnet mask, gateway, and DNS server address:

    IPADDR=<ipv4_address>
    NETMASK=<subnet_mask>
    GATEWAY=<gateway_address>
    DNS1=<dns1_address>
    

    Replace <ipv4_address>with the IPv4 address you want to set, <subnet_mask>replace with the subnet mask, <gateway_address>replace with the gateway address, and <dns1_address>replace with the network's preferred DNS server IP address. For example:

    IPADDR=192.168.1.100
    NETMASK=255.255.255.0
    GATEWAY=192.168.1.2
    DNS1=192.168.1.2
    

    DNS1 here, which is the preferred DNS server, is very important:

    In NAT (Network Address Translation) mode, the network communication of the virtual machine is forwarded through the host's network connection. When a virtual machine requires domain name resolution, DNS works as follows:

    1. The virtual machine sends a DNS query request to the default gateway in the NAT network.
    2. When the default gateway (usually a host or router) receives a DNS query request, it forwards it to a configured DNS server.
    3. After receiving the DNS query request, the DNS server starts to resolve the domain name and returns the corresponding IP address.
    4. After receiving the IP address returned by the DNS server, the default gateway sends it back to the corresponding virtual machine.
    5. The virtual machine establishes a connection with the target host through the obtained IP address to implement network communication.

    In this case, the virtual machine in NAT network mode will use the DNS server configured on the host or router for domain name resolution. Therefore, a virtual machine's DNS settings usually point to the address of the default gateway, which is the IP address of the host or router .

    It should be noted that in NAT mode, the virtual machine cannot directly access the public DNS server. It relies on the default gateway to forward DNS query requests to public DNS servers and return resolution results. Therefore, in a NAT network environment, it is very important to ensure that the default gateway can normally forward DNS query requests to available DNS servers.

    Additionally, some virtualization software allows you to set up a virtual machine to use the host's DNS settings. In this case, the virtual machine will directly use the DNS server configured on the host for domain name resolution without forwarding through the default gateway.

    Here our edited content is as follows:

    Insert image description here

  3. Save the file and close the editor.

  4. Restart the network service: Execute the following command to make the changes take effect.

    systemctl restart NetworkManager
    

    CentOS 8 differs from previous versions in the way it restarts network services. CentOS 8 introduced a new network management tool, NetworkManager, and gradually replaced the previously used network service management tools (such as ifup and ifdown).

    For CentOS 8, restart the network service as follows:

    1. Restart the NetworkManager service using the following command:

      systemctl restart NetworkManager
      
    2. If you need to restart a network interface (such as eth0), you can use the following commands to disable and enable the interface:

      nmcli device disconnect eth0
      nmcli device connect eth0
      

    For previous CentOS versions (such as CentOS 7 or earlier), you can use the following command to restart the network service:

    1. Use the following command to restart the network service:

      sudo service network restart
      
    2. Or use the following command to restart a specific network interface (such as eth0):

      sudo ifdown eth0
      sudo ifup eth0
      

    It should be noted that CentOS 8 recommends using NetworkManager to manage network services, but it still supports traditional network service management methods. For previous versions, the traditional network service management method was still the main network configuration method.

  5. Verify network connection:

    pingVerify that your virtual machine can access the internet using the command:

    ping www.jiumoz.com
    

    Insert image description here

    Verify that the virtual machine accesses the host machine:

    First use cmd to check the IP of the host machine.

    Insert image description here

    pingEnter the IP address of the host in the virtual machine :

    ping 192.168.101.4
    

    Insert image description here

    Verify that the host has access to the virtual machine:

    pingDirectly click the virtual machine IP in the command window of the host machine

    ping 192.168.120.100
    

    Insert image description here

Basically, the network configuration is completed here, and it can be used happily;

To sum up, it is not troublesome to operate, but there are still a lot of knowledge points involved!

Clone a virtual machine

Virtual machine cloning is important in many situations:

  1. Increased efficiency: By cloning a virtual machine, you can quickly create multiple similarly configured virtual machines without having to install and configure them from scratch. This can greatly save time and workload and improve work efficiency.
  2. Rapid deployment: Virtual machine cloning can quickly deploy new virtual machine instances to meet various needs, such as testing, development, emergency preparedness, etc. You can create as many cloned virtual machines as needed and run them simultaneously without having to set up each virtual machine from scratch.
  3. Flexibility and portability: The cloned virtual machine has the same configuration and software environment as the original virtual machine and can be easily moved between different physical servers or virtualization platforms. This is very useful in data center migration, load balancing and disaster recovery.
  4. Testing and development: Virtual machine cloning is one of the common ways to conduct software testing and development. You can clone an already configured virtual machine and conduct experiments, test new software versions, or perform application development on it without affecting the original virtual machine.
  5. Simplified maintenance and management: After cloning a virtual machine, when it is necessary to update, install patches, or perform other maintenance tasks on the virtual machine, testing and verification can be performed on the cloned virtual machine first to reduce potential impact on the production environment.
  • Right-click the created virtual machine, click Management Options, and click Clone

Insert image description here

  • Click next page

Insert image description here

  • Click next page

Insert image description here

  • Create a complete clone

Insert image description here

  • Set the virtual machine name and installation location

Insert image description here

  • Wait for completion and close

Insert image description here

  • A cloned machine has appeared in VMware

Insert image description here

Here you can click on the network adapter of the virtual machine to check whether the mac address is also inherited. If it is inherited, it needs to be generated, which is relatively simple;

  • Log in with the same username and password

Insert image description here

  • Modify hostname
vim /etc/hostname

// 也可以用下面的命令

hostnamectl 
hostnamectl set-hostname <主机名>  //实际命令中没有< >
hostnamectl 

After modification, you need to restart and modify the result:

Insert image description here

  • Modify IP address

    vim /etc/sysconfig/network-scripts/ifcfg-eth0
    

    Just change the IP here:

    Insert image description here

  • Modify uuid

    Generally, after cloning is completed, the MAC address of the virtual machine's network card will automatically change, but after the virtual machine is cloned, the UUID will inherit the UUID of the original virtual machine by default;

    UUID plays an important role in the configuration of virtual machines and is critical for identifying and managing virtual machines;

    UUID is a system-generated globally unique identifier used to ensure that there are no duplicate interface identities in the system. Its role in the network configuration file is as follows:

    • Uniqueness: Each network interface is assigned a different UUID to ensure that no two interfaces have the same identifier.

    • Persistence: By using UUIDs, the system can still accurately identify and match the correct configuration file even if the name or other properties of the interface change.

    • Stability: If you copy a network interface from one system to another or change system hardware, UUIDs will help the system correctly identify and apply the appropriate network configuration.

    There are two solutions here. One is to directly modify the uuid manually, as follows, directly change 32 to 21:

    Insert image description here

    Or use uuidgenthe command to generate a uuid and then replace it

    Insert image description here

  • After the modification is completed, restart the network card (network interface)

    Before restarting, you need to reload the network configuration file to ensure that the latest configuration has taken effect.

    At the same time, we should pay attention tonmcli 命令主要适用于使用 NetworkManager 管理网络连接的系统。

    nmcli con reload
    
    nmcli c reload
    

    nmcli con reloadThe two commands have nmcli c reloadthe same effect, they are both reloading

    Close network connection:

    nmcli connection down <connection-name>
    

    is the name of the connection you want to close, that is, hereens160

    Start network connection:

    nmcli connection up <connection-name>
    同样,<connection-name> 是要启动的连接名称。
    

    Verify that the connection has been restarted:

    nmcli connection show
    

    As follows, the configuration of the network card is completed.

    Insert image description here

  • Verify network environment

    Host pings clone machine

    Insert image description here

    Clone machine pings internet

    Insert image description here

    Clone machine pings host machine

    Insert image description here

    The main machine pings the clone machine

    Insert image description here

    Clone machine pings main machine

    Insert image description here

At this point, we have completed cloning the virtual machine and configuring the network;
we encountered the problem of no network on the clone machine, but it did not recur later (very strange), so there is no record here;
if you encounter the problem of no network when cloning , Questions, please leave a message and discuss together~


Original link: https://blog.jiumoz.com/archives/vmwarepro-xia-centos8-xu-ni-ji-wang-luo-pei-zhi-yu-ke-long

Welcome to follow the blogger’s personal mini program!

Guess you like

Origin blog.csdn.net/qq_45730223/article/details/132469321