This is a guide on installing a virtualization server on Debian GNU/Linux.
Why? for testing. You can emulate hardware, test operating systems, isolate services and more, the limit is your imagination and your hardware.
When virtualizating you probably want to have network access on your virtualized systems so we will configure this too.
Requierements
- Computer with GNU/Linux installed, the guide will be in Debian but other Linux based systems will work.
- Internet access via ethernet
Installing
For virtualization in linux KVM/Qemu is prefered, for ease of use libvirt front end will be used. To install it:
apt install qemu qemu-kvm qemu-system qemu-utils libvirt-clients libvirt-daemon-system virtinst virt-manager bridge-utils
And to be sure the daemon started:
systemctl enable --now libvirtd
Network bridge
For network access we need to get the ip of the server, you may get it from the ip a
command.
Once with the network information we need to write our desired configuration to /etc/network/interfaces
. My example:
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
source /etc/network/interfaces.d/*
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
allow-hotplug enp1s0
iface enp1s0 inet static
auto br0
iface br0 inet static
address 192.168.122.220
netmask 255.255.255.0
gateway 192.168.122.1
bridge_ports enp1s0
up /usr/sbin/brctl stp br0 on
We need to reboot to load the network configuration, we can check the network is correctly configured. For example:
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
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: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast master br0 state UP group default qlen 1000
link/ether 52:54:00:da:fd:b4 brd ff:ff:ff:ff:ff:ff
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
link/ether da:64:e7:16:20:c8 brd ff:ff:ff:ff:ff:ff
inet 192.168.122.220/24 brd 192.168.122.255 scope global br0
valid_lft forever preferred_lft forever
inet6 fe80::d864:e7ff:fe16:20c8/64 scope link
valid_lft forever preferred_lft forever
As we can see the network is now working from the bridge interface.
Libvirt configuration
We need a directory to store virtual machine disks, you can keep it in the default /var/lib/libvirt/images
.
For configuring libvirt access as a root-less user we need to create some configuration files:
mkdir -p ~/.config/libvirt/
echo 'uri_default = "qemu:///system"' | tee -a ~/.config/libvirt/libvirt.conf
sudo usermod -a -G libvirt $(whoami)
Virtual machine install
For virtual disk creation we have various options, the main ones are raw disk and qemu disk, the second having snapshots and other capanilities, to create this disk we use:
qemu-img create -f qcow2 ./debian.qcow2 8G
For starting the virtual machine as headless we use:
virt-install \
--name debian \
--memory 2048 \
--disk path=./debian.qcow2,size=8,format=qcow2,bus=virtio \
--virt-type kvm \
--cpu host \
--vcpus 1 \
--os-type linux \
--os-variant debian12 \
--network bridge=br0,model=virtio \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.debian.org/debian/dists/stable/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'
Thats all
For more flexibility you may read the man man virsh
. Hope this is a useful guide :).