---
title: "Add KVM Network With Virsh"
date: 2021-10-16
draft: false
tags: ["linux", "kvm", "libvirt", "virsh"]
authors: ["trent"]
post: 25
---
date: 2021-10-16
## Introduction
This is a short and sweet walk-through for how to create
a new network for `libvirt` for `kvm`, from the command line,
using `virsh`.
## Name Resolution
Let's start with name resolution.
* Install `libnss-libvirt`:
```shell
apt install libnss-libvirt
```
* In `/etc/nsswitch.conf`, add `libvirt` to hosts key.
```cfg
# /etc/nsswitch.conf
# change this
...
hosts: files dns mymachines
...
# to this
...
hosts: files libvirt dns mymachines
...
```
## Starter XML
You could dumpxml on the existing _default_ network:
```shell
virsh net-dumpxml default > foonet.xml
```
Then, edit foonet.xml:
* remove the network uuid
* change the network name to taste
* remove the bridge mac
* change the bridge name to taste
* change the bridge ip address and dhcp range to taste
```xml
foonet
```
## Define The Network
With the above xml file: `virsh net-define foonet.xml`
The network definition can now be found in `/etc/libvirt/qemu/networks/foonet.xml`
```xml
foonet
e6e40bfc-d449-4043-924c-ca0f0edf4210
```
You could also start the network without defining it
using `virsh net-create foonet.xml`.
## Start/Stop
* Start the network
* `virsh net-start foonet`
* Stop the network
* `virsh net-destroy foonet`
* Undefine the network
* `virsh net-undefine foonet`
* Autostart the network
* `virsh net-autostart foonet`
* Disable autostart for the network
* `virsh net-autostart foonet --disable`
Tab completion is you friend!