After completing the installation of the virtual machine with virt install, it is found that there is only one network card in it. What should we do sometimes when we need two or more network cards? Can be achieved by virsh attach-iterface
For example, the original ubuntu virtual machine is like this, with only one network card:
test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1 Interface Type Source Model MAC ------------------------------------------------------- vnet0 bridge virbr0 virtio 52:54:00:34:06:26 test@ubuntu1604-test-vm1:~$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe34:626/64 scope link valid_lft forever preferred_lft forever
There are two ways to add a network card:
1 Dynamically increase the network card, which took effect at that time, but disappeared after the virtual machine restarted
test@test-Lenovo-Product:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0 Interface attached successfully test@test-Lenovo-Product:~$ virsh domiflist Interface Type Source Model MAC ------------------------------------------------------- vnet0 bridge virbr0 virtio 52:54:00:34:06:26 vnet1 bridge virbr0 rtl8139 52:54:00:9c:c4:dd test@ubuntu1604-test-vm1:~$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe34:626/64 scope link valid_lft forever preferred_lft forever 3: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link/ether 52:54:00:9c:c4:dd brd ff:ff:ff:ff:ff:ff
2 Method 2, permanently increase the network card by modifying the configuration file
test@test-Lenovo-Product:~$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0 --config Interface attached successfully test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1 Interface Type Source Model MAC ------------------------------------------------------- vnet0 bridge virbr0 virtio 52:54:00:34:06:26
Although the newly added interface is not listed in virsh domiflist at this time, and the ip is not seen when logging into the virtual machine, but virsh edit ubuntu1604-test-vm1 can see an additional network card
<interface type='bridge'> <mac address='52:54:00:34:06:26'/> <source bridge='virbr0'/> <model type='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x03' function='0x0'/> </interface> <interface type='bridge'> <mac address='52:54:00:ab:2d:39'/> <source bridge='virbr0'/> <model type='rtl8139'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x07' function='0x0'/> </interface>
After restarting, you can see that the network card has been permanently added:
virsh shutdown ubuntu1604-test-vm1 virsh start domiflist ubuntu1604-test-vm1 test@test-Lenovo-Product:~$ virsh domiflist ubuntu1604-test-vm1 Interface Type Source Model MAC ------------------------------------------------------- vnet0 bridge virbr0 virtio 52:54:00:34:06:26 vnet1 bridge virbr0 rtl8139 52:54:00:ab:2d:39 test@ubuntu1604-test-vm1:~$ ip a 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1 link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00 inet 127.0.0.1/8 scope host lo valid_lft forever preferred_lft forever inet6 ::1/128 scope host valid_lft forever preferred_lft forever 2: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000 link/ether 52:54:00:34:06:26 brd ff:ff:ff:ff:ff:ff inet 192.168.122.2/24 brd 192.168.122.255 scope global ens3 valid_lft forever preferred_lft forever inet6 fe80::5054:ff:fe34:626/64 scope link valid_lft forever preferred_lft forever 3: ens7: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN group default qlen 1000 link / ether 52:54:00: ab: 2d: 39 brd ff: ff: ff: ff: ff: ff
3 If the network card is no longer used and want to delete it, use the following command, it will take effect after restarting
virsh detach-interface ubuntu1604-test-vm1 --type bridge --mac 52:54:00:ab:2d:39 --config Interface detached successfully