https://www.cyberciti.biz/faq/install-kvm-server-debian-linux-9-headless-server/
Step 1: Install kvm
Type the following
apt-get command/
apt command:
$
sudo apt install qemu-kvm libvirt-clients libvirt-daemon-system
bridge-utils libguestfs-tools genisoimage virtinst libosinfo-bin
If you want normal/regular user can manage virtual machines.
Add user vivek to libvirt and libvirt-qemu using usermod command:
$ sudo adduser vivek libvirt
$ sudo adduser vivek libvirt-qemu
Reload group membership with the help of newgrp command:
$ newgrp libvirt
$ newgrp libvirt-qemu
Verify your group membership with
id command:
$ id
Please note that you need to use the following command to connect to KVM server:
$ virsh --connect qemu:///system
$ virsh --connect qemu:///system command
$ virsh --connect qemu:///system list --all
I am going to create bridge Interface br0 as the network connection in VM guests configuration for eth0 interface:
$ sudo vi /etc/network/interfaces.d/br0
Append the following:
## make sure all config related to eth0 deleted ##
auto br0
iface br0 inet static
address 192.168.2.23 ## set up/netmask/broadcast/gateway as per your setup
broadcast 192.168.2.255
netmask 255.255.255.0
gateway 192.168.2.254
bridge_ports eth0 # replace eth0 with your actual interface name
bridge_stp off # disable Spanning Tree Protocol
bridge_waitport 0 # no delay before a port becomes available
bridge_fd 0 # no forwarding delay
Restart the networking service on Linux: $ sudo systemctl restart network-manager
To see current networking setting for KVM, run:
$ sudo virsh net-list --all
You need to configure a KVM guest domain on a bridged network. So
create a file named bridge.xml as follows a text editor such as NA
command:
$ sudo vi /root/bridged.xml
Append the
following config:
<network>
<name>br0</name>
<forward mode="bridge"/>
<bridge name="br0"/>
</network>
|
Save and close the file in vi/vim.
$ sudo virsh net-define --file /root/bridged.xml
$ sudo virsh net-autostart br0
$ sudo virsh net-start br0
Create CentOS 7 VM
In this example, I’m creating CentOS 7.x VM with 2GB RAM, 2 CPU core, 1 nic and 40GB disk space, enter:
$ sudo virt-install \
--virt-type=kvm \
--name centos7 \
--ram 2048 \
--vcpus=2 \
--os-variant=rhel7 \
--virt-type=kvm \
--hvm \
--cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-DVD-1708.iso \
--network=bridge=br0,model=virtio \
--graphics vnc \
--disk path=/var/lib/libvirt/images/centos7.qcow2,size=40,bus=virtio,format=qcow2
To configure vnc login from another terminal over ssh and type:
$ sudo virsh dumpxml centos7 | grep vnc
<graphics type='vnc' port='5901' autoport='yes' listen='127.0.0.1'>
You can also use the following command:
$ sudo virsh vncdisplay centos7
Please note down the port value (i.e. 5901). You need to use an SSH
client to setup tunnel and a VNC client to access the remote vnc server.
Type the following SSH port forwarding command from your
client/desktop:
$ ssh vivek@server1.cyberciti.biz -L 5901:127.0.0.1:5901
Once you have ssh tunnel established, you can point your VNC client at
your own 127.0.0.1 (localhost) address and port 5901 as follows:
Step 5 – Use virt-builder to create VM
Above method (virt-install) works nicely but if you need quickly building new virtual machines, try virt-builder.
How to list the virtual machines available
$ virt-builder --list | more
You can use the
grep command to filter out only x86_64 arch based VMs:
$ virt-builder --list | grep x86_64
Create Debian 9.x VM
Create Debian 9 VM with 10GB disk space, 2GB ram, 2 vCPU and random password for root account, run:
$ sudo virt-builder debian-9 \
--size=10G \
--format qcow2 -o /var/lib/libvirt/images/debian9-vm1.qcow2 \
--hostname debain9-vm1 \
--network \
--timezone Asia/Kolkata
Finally import image with virt-install command:
$ sudo virt-install --import --name debian9-vm1 \
--ram 2048 \
--vcpu 2 \
--disk path=/var/lib/libvirt/images/debian9-vm1.qcow2,format=qcow2 \
--os-variant debian9 \
--network=bridge=br0,model=virtio \
--noautoconsole
You can login to your VM using x0E4iZ8sHjA6ekb6 password for root account:
$ sudo virsh list --all
$ virsh console debian9-vm1
--30--