KVM network management learning - NAT

KVM network management learning - NAT

The default networking method of KVM is NAT. After KVM is installed, a network bridge virbr0 will be automatically generated on the host to connect to the virtual machine.
There are two ways to configure NAT, graphic mode and configuration file mode. The configuration method here is relatively simple. What I need to sort out is how the operation of the graphical interface and the operation of the configuration file correspond to each other during the NAT configuration process.
The following describes the operations of the host machine and the virtual machine respectively.
Host:
As a network provider, you can use the default network or create a new network yourself.
The graphical interface is configured under the virtual machine manager virt-manager.
insert image description here
insert image description here
When adding a network, follow the wizard step by step. When adding a network segment here, the default gateway IP added in the graphical interface is xxx1, and the DHCP service is started, and you are required to provide the address pool (start and end range).
insert image description here
insert image description here
If you use the configuration file to operate the steps on the graphical interface, the process is as follows:
first find the configuration file named default in the /usr/share/libvirt/network directory, you can copy and modify this configuration file.
insert image description here
Define this network through the command virsh net-define, and open this network through virsh net-start.

virsh net-define mynat.xml  定义网络
virsh net-start mynat  开启网络
virsh net-autostart mynat 自动启动
virsh net-list  查看网络

insert image description here
After opening the network by definition, you can find the corresponding network configuration file in /etc/libvirt/qemu/networks/.
Use virsh dumpxml to open the virtual machine configuration file, find the following paragraph, and ensure that the network configuration is correct.

virsh dumpxml vm01 查看虚拟机vm01的配置文件

insert image description here

View the IP information of the host machine.
insert image description here
After
the virtual network generation operation is completed, the virtual machine needs to use the new network. You can open the virtual machine's details page, select the network card, and select the newly created network source on the virtual network interface page. (The virtual machine needs to be shut down or restarted to take effect)
insert image description here

In the virtual machine, you only need to execute the command dhclent first to obtain the DHCP address.

dhclient

insert image description here
At this time, the host can be pinged, and the external network ip can communicate.
insert image description here

To view the current virtual network on the host, you can use the command virsh net-list

[root@localhost ~]# virsh net-list

 Name                 State      Autostart     Persistent

----------------------------------------------------------

 default              active     yes           yes

 mynat                active     yes           yes

To view specific information in a network, you can use the virsh net-info command

[root@localhost ~]# virsh net-info default

Name:           default

UUID:           afd4648e-4f13-4f28-a2f5-db99da866253

Active:         yes

Persistent:     yes

Autostart:      yes

Bridge:         virbr0

To view the bridge status in a specific network, you can use the command brctl show

[root@localhost ~]# brctl show virbr0

bridge name	bridge id		STP enabled	interfaces

virbr0		8000.52540019d233	yes		virbr0-nic

Check routing

[root@localhost ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

0.0.0.0         192.168.217.2   0.0.0.0         UG    100    0        0 ens33

192.168.100.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr1

192.168.122.0   0.0.0.0         255.255.255.0   U     0      0        0 virbr0

192.168.217.0   0.0.0.0         255.255.255.0   U     100    0        0 ens33

To view the network information of a virtual machine, you can use the virsh domiflist command

[root@localhost networks]# virsh domiflist vm01
Interface  Type       Source     Model       MAC
-------------------------------------------------------
vnet0      network    mynat      virtio      52:54:00:dd:70:e5

Guess you like

Origin blog.csdn.net/qq_26350199/article/details/112758609