After installing a KVM virtual machine using virt-install, it often comes with only a single network interface. To add additional interfaces, the virsh attach-interface command can be used.
Initially, the VM may appear as follows:
$ virsh domiflist ubuntu1604-test-vm1
Interface Type Source Model MAC
-------------------------------------------------------
vnet0 bridge virbr0 virtio 52:54:00:34:06:26
$ 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
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
There are two approaches to add a new NIC:
1. Temporary attachment (lost after reboot):
$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0
Interface attached successfully
$ 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:9c:c4:dd
$ ip a
# ... ens7 appears but is DOWN
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. Permanent attachment (persists across reboots):
Use the --config flag to update the domain’s persistent configuration:
$ virsh attach-interface ubuntu1604-test-vm1 --type bridge --source virbr0 --config
Interface attached successfully
Although virsh domiflist won’t immediately show the new itnerface for a running VM, inspecting the XML definition confirms its presence:
$ virsh edit ubuntu1604-test-vm1
The updated configuration includes:
<interface type='bridge'>
<mac address='52:54:00:34:06:26'/>
<source bridge='virbr0'/>
<model type='virtio'/>
</interface>
<interface type='bridge'>
<mac address='52:54:00:ab:2d:39'/>
<source bridge='virbr0'/>
<model type='rtl8139'/>
</interface>
After restarting the VM, the new interface becomes active:
$ virsh shutdown ubuntu1604-test-vm1
$ virsh start ubuntu1604-test-vm1
$ 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
$ ip a
# ens7 now appears in guest OS
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
To permanently remove a interface, use virsh detach-interface with the --config option and specify the MAC adress:
$ virsh detach-interface ubuntu1604-test-vm1 --type bridge --mac 52:54:00:ab:2d:39 --config
Interface detached successfully