Open
Close

Installing and configuring kvm ubuntu 18.04. Rubber hypervisor. We use logical groups to virtualize QEMU-KVM in Linux. Connecting to a virtual machine

In this introductory article, I will briefly introduce all the software tools used in the service development process. They will be discussed in more detail in the following articles.

Why ? This operating system is close and understandable to me, so there was no torment, torment or tossing when choosing a distribution. It does not have any particular advantages over Red Hat Enterprise Linux, but the decision was made to work with a familiar system.

If you are planning to independently deploy an infrastructure using similar technologies, I would advise you to take RHEL: thanks to good documentation and well-written application programs, it will be, if not an order of magnitude, then certainly twice as simpler, and thanks to the developed certification system, you can easily will find a number of specialists who are familiar with this OS at the proper level.

We, again, decided to use Debian Squeeze with a set of packages from Sid/Experimental and some packages backported and compiled with our patches.
There are plans to publish a repository with packages.

When choosing virtualization technology, two options were considered - Xen and KVM.

Also, the fact that there was a huge number of developers, hosters, and commercial solutions based on Xen was taken into account - the more interesting it was to implement a solution based on KVM.

The main reason why we decided to use KVM is the need to run virtual machines with FreeBSD and, in the future, MS Windows.

To manage virtual machines, it turned out to be extremely convenient to use products that use its API: virsh, virt-manager, virt-install, etc.

This is a system that stores the settings of virtual machines, manages them, keeps statistics on them, makes sure that the interface of the virtual machine is raised when starting, connects devices to the machine - in general, it does a lot of useful work and a little more than that.

Of course, the solution is not perfect. The disadvantages include:

  • Absolutely insane error messages.
  • Inability to change part of the virtual machine configuration on the fly, although QMP (QEMU Monitor Protocol) allows this.
  • Sometimes, for some unknown reason, it is impossible to connect to libvirtd - it stops responding to external events.

The main problem in implementing the service at the very beginning was the limitation of resources for virtual machines. In Xen, this problem was solved with the help of an internal scheduler that distributes resources between virtual machines - and what’s best is that the ability to limit disk operations was also implemented.

There was nothing like this in KVM until the advent of the kernel resource allocation mechanism. As usual in Linux, access to these functions was implemented through a special file system cgroup, in which, using the normal write() system calls, one could add a process to a group, assign it its parrot weight, specify the core on which it will run, specify the disk bandwidth that the process can use, or, again, assign a weight to it.

The benefit is that all this is implemented inside the kernel, and it can be used not only for the server, but also for the desktop (which was used in the famous “The ~200 Line Linux Kernel Patch That Does Wonders”). And in my opinion, this is one of the most significant changes in the 2.6 branch, not counting my favorite #12309, and not the filing of another file system. Well, except perhaps for POHMELFS (but purely because of the name).

My attitude towards this utility library is very ambiguous.

On the one hand it looks something like this:

And this thing is also damn difficult to assemble from source, much less into a package: sometimes it seems to me that Linux From Scratch is a little easier to build from scratch.

On the other hand, it is a very powerful thing that allows you to create images for virtual machines, modify them, compress them, install grub, modify the partition table, manage configuration files, transfer hardware machines to a virtual environment, transfer virtual machines from one image to another, transfer virtual machines from the image to hardware and, to be honest, here my imagination lets me down a little. Oh, yes: you can also run a daemon inside a Linux virtual machine and access the virtual machine data live, and do all this in shell, python, perl, java, ocaml. This is a short and by no means exhaustive list of what you can do with .

Interestingly, most of the code is generated at the time of assembly, as well as the documentation for the project. Ocaml and perl are widely used. The code itself is written in C, which is then wrapped in OCaml, and the repeated pieces of code are generated themselves. Working with images is carried out by launching a special service image (supermin appliance), to which commands are sent through a channel into it. This rescue image contains a certain set of utilities, such as parted, mkfs and others useful for a system administrator.

Recently I even started using it at home, when I extracted the data I needed from the nandroid image. But this requires a yaffs-enabled kernel.

Other

Below are some more interesting links to a description of the software used - read and study it yourself if you are interested. For example,

KVM or Kernel Virtual Module is a virtualization module for the Linux kernel that allows you to turn your computer into a hypervisor for managing virtual machines. This module operates at the kernel level and supports hardware acceleration technologies such as Intel VT and AMD SVM.

KVM software itself does not virtualize anything in user space. Instead, it uses the /dev/kvm file to configure virtual address spaces for the guest in the kernel. Each guest machine will have its own video card, network and sound card, hard drive and other equipment.

Also, the guest system will not have access to components of the real operating system. The virtual machine runs in a completely isolated space. You can use kvm both on a GUI system and on servers. In this article we will look at how to install kvm Ubuntu 16.04

Before proceeding with the KVM installation itself, you need to check whether your processor supports hardware virtualization acceleration from Intel-VT or AMD-V. To do this, run the following command:

egrep -c "(vmx|svm)" /proc/cpuinfo

If the result returns 0, then your processor does not support hardware virtualization, if 1 or more, then you can use KVM on your machine.

Now we can proceed to installing KVM, a set of programs can be obtained directly from the official repositories:

sudo apt install qemu-kvm libvirt-bin bridge-utils virt-manager cpu-checker

We installed not only the kvm utility, but also the libvirt library, as well as the virtual machine manager. Once the installation is complete, you need to add your user to the libvirtd group, because only root and users in this group can use KVM virtual machines:

sudo gpasswd -a USER libvirtd

After running this command, log out and log in again. Next, let's check if everything was installed correctly. To do this, use the kvm-ok command:

INFO: /dev/kvm exists
KVM acceleration can be used

If everything was done correctly, you will see the same message.

Using KVM on Ubuntu 16.04

You have completed the task of installing kvm in Ubuntu, but you cannot yet use this virtualization environment and it still needs to be configured. Next, we will look at how kvm Ubuntu is configured. First you need to set up your network. We need to create a bridge with which the virtual machine will connect to the computer's network.

Configuring a bridge in NetworkManager

This can be done in several ways, for example, you can use the NetworkManager network configuration program.

Click the NetworkManager icon in the panel, then select change connections, then click the button Add:

Then select the connection type Bridge and press Create:

In the window that opens, click the button Add, to link our bridge to the internet connection:

From the list, select Ethernet and press Create:

In the next window, select in the field device, network interface to which our bridge should be associated:

Now you will see your bridge in the list of network connections. All that remains is to reboot the network to fully apply the changes, to do this, run:

Manual bridge setup

First you need to install the bridge-utils set of utilities if you have not already done so:

sudo apt install bridge-utils

Then, using the brctl program, we can create the bridge we need. To do this, use the following commands:

sudo brctl addbr bridge0
$ sudo ip addr show
$ sudo addif bridge0 eth0

The first command adds the bridge device br0, with the second you need to determine which network interface is the main connection to the external network, in my case it is eth0. And with the last command we connect bridge br0 to eth0.

Now you need to add a few lines to the network settings so that everything starts up automatically after the system starts. To do this, open the /etc/network/interfaces file and add the following lines there:

sudo gedit /etc/network/interfaces

loopback
auto lo bridge0
iface lo inet loopback
iface bridge0 inet dhcp
bridge_ports eth0

When the settings are added, reboot the network:

sudo systemctl restart networking

Now the installation and configuration of KVM is completely completed and you can create your first virtual machine. After this, you can view the available bridges using the command:

Creating KVM virtual machines

The Ubuntu KVM setup is complete and we can now move on to using it. First, let's look at the list of existing virtual machines:

virsh -c qemu:///system list

It's empty. You can create a virtual machine through the terminal or in the graphical interface. To create via the terminal, use the virt-install command. First let's go to the libvirt folder:

cd /var/lib/libvirt/boot/

To install CentOS the command will look like this:

sudo virt-install\
--virt-type=kvm \
--name centos7\
--ram 2048\
--vcpus=2\
--os-variant=rhel7 \
--hvm\
--cdrom=/var/lib/libvirt/boot/CentOS-7-x86_64-DVD-1511.iso \
--network=bridge=br0,model=virtio \
--graphics vnc\
--disk path=/var/lib/libvirt/images/centos7.qcow2,size=40,bus=virtio,format=qcow2

Let's take a closer look at what the parameters of this command mean:

  • virt-type- type of virtualization, in our case kvm;
  • name- name of the new car;
  • ram- amount of memory in megabytes;
  • vcpus- number of processor cores;
  • os-variant- type of operating system;
  • cdrom- installation image of the system;
  • network-bridge- the network bridge that we configured earlier;
  • graphics- a way to gain access to the graphical interface;
  • diskpath- address of the new hard drive for this virtual machine;

After the installation of the virtual machine is complete, you can find out the VNC connection parameters using the command:

sudo virsh vncdisplay centos7

Now you can enter the received data in your VNC client and connect to the virtual machine even remotely. For Debian the command will be slightly different, but everything looks similar:

Go to the folder for images:

cd /var/lib/libvirt/boot/

You can download the installation image from the Internet if necessary:

sudo wget https://mirrors.kernel.org/debian-cd/current/amd64/iso-dvd/debian-8.5.0-amd64-DVD-1.iso

Then let's create a virtual machine:

sudo virt-install\
--virt-type=kvm \
--name=debina8 \
--ram=2048\
--vcpus=2\
--os-variant=debian8 \
--hvm\
--cdrom=/var/lib/libvirt/boot/debian-8.5.0-amd64-DVD-1.iso \
--network=bridge=bridge0,model=virtio \
--graphics vnc\
--disk path=/var/lib/libvirt/images/debian8.qcow2,size=40,bus=virtio,format=qcow2

Now let's look at the list of available machines again:

virsh -c qemu:///system list

To start the virtual machine you can use the command:

sudo virsh start machinename

To stop:

sudo virsh shutdown machinename

To switch to sleep mode:

sudo virsh suspend machinename

To reboot:

sudo virsh reboot machinename

sudo virsh reset machinename

To completely remove a virtual machine:

sudo virsh destroy machinename

Creating virtual machines in GUI\

If you have access to a graphical interface, there is no need to use a terminal; you can use the full graphical interface of the Virtual Manager virtual machine manager. The program can be launched from the main menu:

To create a new machine, click on the icon with the monitor icon. Next, you will need to select the ISO image of your system. You can also use a real CD/DVD drive:

On the next screen, select the amount of memory that will be available to the virtual machine, as well as the number of processor cores:

On this screen, you need to select the size of the hard drive that will be available in your machine:

At the last step of the wizard, you have to check that the machine settings are correct and also enter its name. You also need to specify the network bridge through which the machine will connect to the network:

After this, the machine will be ready for use and will appear in the list. You can launch it using the green triangle on the manager toolbar.

conclusions

In this article, we looked at how to install KVM Ubuntu 16.04, we looked at how to fully prepare this environment for work, as well as how to create virtual machines and use them. If you have any questions, ask in the comments!

To conclude, a lecture from Yandex about what virtualization is in Linux:

I previously wrote about installation Qemu-KVM V Debian. But, in my opinion, the information turned out to be incomplete. Plus I didn't take into account some nuances. Therefore, I bring to your attention an updated article on installing the Qemu-KVM virtual machine. Naturally, I will delete the old article.

I think I should explain what it is virtual machine, not worth it. You probably know this (since you are reading this article). If not - . We will focus directly on the subject. Qemu-KVM is a project to combine two of the most dreamy (in my opinion) technologies of full virtualization. Qemu is a kind of “computer emulator” that supports a great variety of hardware architectures. It can run almost any OS for any device (for example, I ran old versions Mac OS X, which is for PowerPC). The disadvantage of Qemu is its slowness due to the lack of hardware acceleration. And then another project comes to the rescue - KVM. Or Kernel Virtual Machine. KVM is a Linux kernel technology that allows for hardware acceleration with full virtualization. The disadvantage of KVM is that it only supports x86 architecture

Why Qemu-KVM? For Linux, this is the most recommended virtualization project. It works faster than VirtualBox And VMware Player(according to my tests), KVM is a native technology for Linux. Plus, if you have a good gaming computer with two video cards, you can install in Qemu-KVM Windows, throw one of the video cards into it, and forget about rebooting to another OS. If you want to play, you launch a virtual machine with Windows and play. The performance will be 95% of the performance of Windows installed on the hardware. But it's just gorgeous, in my opinion. I will write a separate article about this. It will be interesting:)

Now I will describe our plan of action. Firstly, I will carry out the installation using an example Debian 8.2 GNOME 64 bit, although there will not be any significant differences in other graphical environments. Secondly, I will describe working with KVM only in graphical mode (after all, we will not be installing it on a server). Therefore, no terminals, scripts, and so on, as is usually done in the case of server virtualization. Thirdly, I advise you to additionally read the documentation for Qemu and KVM (I will provide links at the end of the article). This will be very useful to you if you want to make the most of the full potential of this bundle. Well, our plan of action is clear. Now the action steps:

  • install qemu-kvm;
  • installation of a graphic manager and additional utilities;
  • setting up a network bridge;
  • creating storage for virtual machines;
  • installing a guest system.
First, let's check whether your computer supports hardware virtualization. To do this, run the command in the terminal:

egrep "(vmx|svm)" /proc/cpuinfo

The command output must contain either vmx, or svm. If they are not there, check whether virtualization is enabled in the BIOS (look for items Intel VT-i or similar for AMD). If there is nothing, then you are out of luck.

Install the necessary components:

sudo apt install qemu-kvm bridge-utils libvirt-bin virt-manager

Add yourself to the libvirt group:

sudo adduser $USER libvirt

Now let's set up the network. In order for all virtual machines to be able to access the network and communicate with each other, you need to create a network bridge and virtual network cards for each virtual machine ( tap devices). Since we will install virtual machines from the graphical interface, there is no need to create taps manually. Virt Manager will do this for us every time we start. We only need to configure the bridge. First, let's enable routing in the kernel:

sudo nano /etc/sysctl.conf

We look for the line net.ipv4_forward=0 and change its value to 1 . We also save:

sudo sysctl -p

Next, I will assume the following: 1) on your computer there is one network card that receives an IP address from the router. 2) you access the Internet via a 3G modem, and your network card is free. This option involves more manual work, but it has been tested several times (I have done this myself on one of the machines). So, open the interfaces file:

sudo nano /etc/network/interfaces

Its default contents are:



auto lo
iface lo inet loopback

We change its contents. For the first option:

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
address 192.168.0.2
gateway 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
bridge_ports eth0
bridge_stp off
bridge_maxwait 0
bridge_fd 0

For the second option:

source /etc/network/interfaces.d/*

# The loopback network interface
auto lo
iface lo inet loopback

auto ppp0
iface ppp0 inet wvdial

auto eth0
iface eth0 inet manual

auto br0
iface br0 inet static
address 192.168.0.2
gateway 192.168.0.1
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
bridge_ports eth0
bridge_stp off
bridge_maxwait 0
bridge_fd 0
up route del default br0

Note: if you do not need an automatic Internet connection via a modem after the system starts, remove the lines auto ppp0 and
iface ppp0 inet wvdial . Otherwise, make sure that when the system starts, the modem is inserted into the USB port.

Save. Now for the option with a modem, you need to install the dialer program wvdial:

sudo apt install wvdial

Editing the config (note: a 3G modem is used as an example Beeline. You can easily find examples of configs for other modems on the Internet):

sudo nano /etc/wvdial.conf


Init1 = ATZ
Init2 = ATQ0 V1 E1 S0=0 &C1 &D2 +FCLASS=0
Init3 = AT+CGDCONT=1,"IP","home.beeline.ru"
Stupid Mode = 1
ISDN = 0
Modem Type = USB Modem
New PPPD = yes
Phone = *99#
Modem = /dev/ttyACM0
Username = beeline
Password = beeline
Baud = 9600
Country = Russia
Auto Reconnect=on
Auto DNS = off
Idle Seconds = 0

Save. Now the modem will turn on immediately after the system boots. The line up route del default br0 deletes the default route through the bridge. If this is not done, you will not be able to connect to the Internet, since the traffic will go over the bridge and not through the 3G modem.

The last step we need to say firewall, so that it passes traffic from our virtual machines to the network and back. To do this, you can go in two ways: write a script with several rules for iptables, which will run along with the system, or enter these rules manually and save them. I'll use the first option. For the second one you will need to install the package iptables-persistent and simply enter the rules one by one (using sudo). So. create a script (in any text editor). Paste the following content there:

#!/bin/sh

# Define the output interface for which address replacement (NAT) will be applied
iptables -v -t nat -A POSTROUTING -o ppp0 -j MASQUERADE

# We forward all packets received by the modem from the global network (0.0.0.0/0) to the local network (192.168.0.0/24)
iptables -v -A FORWARD -i ppp0 -o br0 -s 0.0.0.0/0 -d 192.168.0.0/24 -j ACCEPT

# Forward all packets coming from the local network (192.168.0.0/24) to the global one (0.0.0.0/0)
iptables -v -A FORWARD -i br0 -o ppp0 -s 192.168.0.0/24 -d 0.0.0.0/0 -j ACCEPT

Save it as gateway.sh and give execution rights (either in the file properties or in the terminal with the command chmod +x gateway.sh). Now you can either launch it manually after the system has booted, or add it to startup. To do this, move the script to ~/.config/autostart(in the file manager, turn on show hidden files and you will see the directory .config in your home directory).

Now everything is ready to install the virtual machine. From the application menu, launch Virt Manager (virtual machine manager):

Right click on the line localhost and choose Details. Go to the tab Storage. We need to specify the directory (or disk partition/disk) to store the virtual machines.

In the lower left corner, click on the plus sign ( Add a pool), indicate the type of storage and the path to it.

On the tab Network interfaces, you can check if everything works.

Now we press File - New virtual machine. Specify the path to the disk image and the type of virtual machine. Next, we indicate the amount of RAM for it and the number of processor cores. Next, indicate our storage and click New volume. Specify the name, leave the type qcow2, and size. This will be a virtual hard disk. If you plan to install a system with a graphical shell and a bunch of programs, give more space (50 gigabytes). On the last tab, check the box Change settings before launch, check that our bridge is selected as a network device, write any name for the virtual machine and click Complete. The parameters window for this virtual machine will open in front of you.





Go to the tab CPU, and put a tick on Copy host processor settings.

Next to the tab Net(next), and also indicate vitio. On the tab Display please indicate Spice, and on the tab Video - QXL. Usually this combination provides maximum graphics rendering performance, but if you want, you can experiment. Please note that for Windows guest systems, a separate installation of the QXL driver is required (in Windows itself).


Now that everything is ready, click in the upper left corner Start installation. And install the system as usual, with one exception: as soon as the installer begins to automatically configure the network, click Cancel, and select Configure the network manually. Specify the desired IP address for the virtual machine (in our case 192.168.0.3 ), subnet mask ( 255.255.255.0 ), gateway (the gateway will be the host address, that is 192.168.0.2 ) and DNS server (here just specify Google 8.8.8.8 ). That's all. There is no need to do anything further. Install the system and configure it. In general, that's all. The steps described are a way to replace, say, VirtualBox with a better alternative. After reading the documentation, you will understand how wide the capabilities of Qemu-KVM are. I deliberately did not describe here additional console parameters and methods for launching virtual machines through the terminal, since this is not always necessary on a home machine. I will write a separate article about this, on setting up a home multifunctional server (which can also act as a virtual machine server). For those who for some reason did not understand what was written, or there are still unclear moments, I suggest you watch the video in which I will not describe, but show you how to install and configure all this stuff. If you have suggestions or additions to the article, write in the comments.

The release of WordPress 5.3 improves and expands the block editor introduced in WordPress 5.0 with a new block, more intuitive interaction, and improved accessibility. New features in the editor […]

After nine months of development, the FFmpeg 4.2 multimedia package is available, which includes a set of applications and a collection of libraries for operations on various multimedia formats (recording, converting and […]

  • New features in Linux Mint 19.2 Cinnamon

    Linux Mint 19.2 is a long-term support release that will be supported until 2023. It comes with updated software and contains improvements and many new […]

  • Linux Mint 19.2 distribution released

    Presented is the release of the Linux Mint 19.2 distribution, the second update to the Linux Mint 19.x branch, formed on the Ubuntu 18.04 LTS package base and supported until 2023. The distribution is fully compatible [...]

  • New BIND service releases are available that contain bug fixes and feature improvements. New releases can be downloaded from the downloads page on the developer's website: […]

    Exim is a message transfer agent (MTA) developed at the University of Cambridge for use on Unix systems connected to the Internet. It is freely available in accordance with [...]

    After almost two years of development, the release of ZFS on Linux 0.8.0 is presented, an implementation of the ZFS file system, designed as a module for the Linux kernel. The module has been tested with Linux kernels from 2.6.32 to […]

  • WordPress 5.1.1 fixes a vulnerability that allows you to take control of your site
  • The IETF (Internet Engineering Task Force), which develops Internet protocols and architecture, has completed an RFC for the ACME (Automatic Certificate Management Environment) protocol […]

    The non-profit certification authority Let’s Encrypt, which is controlled by the community and provides certificates free of charge to everyone, summed up the results of the past year and talked about plans for 2019. […]

  • A new version of Libreoffice has been released – Libreoffice 6.2
  • Checking hypervisor support

    We check that the server supports virtualization technologies:

    cat /proc/cpuinfo | egrep "(vmx|svm)"

    You should get something like this in response:

    flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfm perf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 cx16 xtpr pdcm pcid dca sse4_1 sse4_2 popcnt aes lahf_lm epb tpr_shadow vnmi flexpriority ept vpid dtherm ida arat

    Otherwise, go to the BIOS, find the option to enable virtualization technology (has different names, for example, Intel Virtualization Technology or Virtualization) and enable it - set the value Enable.

    You can also check compatibility with the command:

    * if the command returns an error "kvm-ok command not found", install the appropriate package: apt-get install cpu-checker.

    If we see:

    INFO: /dev/kvm exists
    KVM acceleration can be used

    This means there is hardware support.

    Preparing the server

    For our convenience, we will create a directory in which we will store data for KVM:

    mkdir -p /kvm/(vhdd,iso)

    * two directories will be created: /kvm/vhdd(for virtual hard disks) and /kvm/iso(for iso images).

    Let's set the time:

    \cp /usr/share/zoneinfo/Europe/Moscow /etc/localtime

    * this command sets the zone in accordance with Moscow time.

    ntpdate ru.pool.ntp.org

    * We synchronize with the time server.

    Installation and launch

    We install KVM and the necessary management utilities.

    a) Ubuntu up to version 18.10

    apt-get install qemu-kvm libvirt-bin virtinst libosinfo-bin

    b) Ubuntu after 18.10:

    apt-get install qemu-kvm libvirt-daemon-system libvirt-bin virtinst libosinfo-bin

    * Where qemu-kvm- hypervisor; libvirt-bin— hypervisor control library; virtinst— virtual machine management utility; libosinfo-bin— a utility for viewing a list of operating system options that can be used as guest operating systems.

    Let's configure the service to start automatically:

    systemctl enable libvirtd

    Let's launch libvirtd:

    systemctl start libvirtd

    Network configuration

    Virtual machines can work behind NAT (which is a KVM server) or receive IP addresses from the local network - for this you need to configure a network bridge. We'll configure the latter.

    When using a remote connection, check your settings carefully. If an error occurs, the connection will be terminated.

    Install bridge-utils:

    apt-get install bridge-utils

    a) network setup in older versions of Ubuntu (/etc/network/interfaces).

    Open the configuration file to configure network interfaces:

    vi /etc/network/interfaces

    And let's put it in this form:

    #iface eth0 inet static
    # address 192.168.1.24
    # netmask 255.255.255.0
    #gateway 192.168.1.1
    # dns-nameservers 192.168.1.1 192.168.1.2

    Auto br0
    iface br0 inet static
    address 192.168.1.24
    netmask 255.255.255.0
    gateway 192.168.1.1
    dns-nameservers 192.168.1.1 192.168.1.2
    bridge_ports eth0
    bridge_fd 9
    bridge_hello 2
    bridge_maxage 12
    bridge_stp off

    * where everything that is commented out is the old settings of my network; br0— name of the interface of the created bridge; eth0— an existing network interface through which the bridge will operate.

    Restart the network service:

    systemctl restart networking

    b) setting up a network in new versions of Ubuntu (netplan).

    vi /etc/netplan/01-netcfg.yaml

    * depending on the system version, configuration file yaml may have a different name.

    Let's bring it to form:

    network:
    version: 2
    renderer: networkd
    ethernets:
    eth0:
    dhcp4: false
    dhcp6: false
    wakeonlan: true

    Bridges:
    br0:
    macaddress: 2c:6d:45:c3:55:a7
    interfaces:
    -eth0
    addresses:
    - 192.168.1.24/24
    gateway4: 192.168.1.1
    mtu: 1500
    nameservers:
    addresses:
    - 192.168.1.1
    - 192.168.1.2
    parameters:
    stp: true
    forward-delay: 4
    dhcp4: false
    dhcp6: false

    * in this example we are creating a virtual bridge interface br0; we use as a physical interface eth0.

    Apply network settings:

    We insist on redirecting network traffic (so that virtual machines with a NAT network interface can access the Internet):

    vi /etc/sysctl.d/99-sysctl.conf

    Add the line:

    net.ipv4.ip_forward=1

    Apply the settings:

    sysctl -p /etc/sysctl.d/99-sysctl.conf

    Creating a virtual machine

    To create the first virtual machine, enter the following command:

    virt-install -n VM1\
    --autostart\
    --noautoconsole\
    --network=bridge:br0 \
    --ram 2048 --arch=x86_64 \
    --vcpus=2 --cpu host --check-cpu \
    --disk path=/kvm/vhdd/VM1-disk1.img,size=16 \
    --cdrom /kvm/iso/ubuntu-18.04.3-server-amd64.iso \
    --graphics vnc,listen=0.0.0.0,password=vnc_password \
    --os-type linux --os-variant=ubuntu18.04 --boot cdrom,hd,menu=on

    • VM1 - the name of the machine being created;
    • autostart — allow the virtual machine to automatically start along with the KVM server;
    • noautoconsole — does not connect to the virtual machine console;
    • network - network type. In this example, we are creating a virtual machine with a “network bridge” interface. To create an internal interface with NAT type, enter --network=default,model=virtio;
    • ram - amount of RAM;
    • vcpus — number of virtual processors;
    • disk - virtual disk: path — path to disk; size — its volume;
    • cdrom - virtual drive with a system image;
    • graphics parameters for connecting to a virtual machine using a graphical console (in this example we use vnc); listen - at what address vnc requests are received (in our example, all); password - password for connecting using vnc;
    • os-variant — guest operating system (we received the entire list with the command osinfo-query os, in this example we install Ubuntu 18.04).

    Connecting to a virtual machine

    On the computer from which we plan to work with virtual machines, download a VNC client, for example, TightVNC and install it.

    On the server we enter:

    virsh vncdisplay VM1

    the command will show which port VNC is running on for machine VM1. I have had:

    * :1 means that you need to add 1 to 5900 - 5900 + 1 = 5901.

    Launch TightVNC Viewer, which we installed and enter the connection data:

    Click on Connect. When prompted for a password, enter the one you specified when creating the VM ( vnc_password). We will connect to the virtual machine using a remote console.

    If we don’t remember the password, we open the virtual machine setup with the command:

    And we find the line:



    * in this example, a password is used to access the virtual machine 12345678 .

    Managing a virtual machine from the command line

    Examples of commands that may be useful when working with virtual machines.

    1. Get a list of created machines:

    virsh list --all

    2. Enable the virtual machine:

    virsh start VMname

    * Where VMname— the name of the created machine.

    3. Shut down the virtual machine:

    ubuntu-vm-builder is a package developed by Canonical to make it easier to create new virtual machines.

    To install it, enter:

    apt-get install ubuntu-vm-builder