如何在 Rocky Linux 9 / AlmaLinux 9 上安装 KVM
KVM
![](/data/attachment/album/202305/25/155857v1e939ckoj3eaxe3.jpg)



> 
> 在本指南中，我们演示了如何在 Rocky Linux 9 / Alma Linux 9 上安装 KVM。
> 
> 
> 


KVM 是 <ruby> 内核虚拟机 <rt>  Kernel Virtualization Machine </rt></ruby> 的简称，是一个为 Linux 内核设计的开源虚拟化平台。它是一种 1 类管理程序，或通常称为裸机管理程序。它允许用户创建和管理多台客户机，这些可以在 Linux 或 Windows 操作系统中创建。


与大多数虚拟化平台一样，它将硬件资源（如 CPU、内存、存储、网络、图形等）抽象化，并将它们分配给独立于宿主机运行的客户机。


### 先决条件


* 预装 Rocky Linux 9 / AlmaLinux 9
* 具有管理员权限的 sudo 用户
* 互联网连接


### 1、验证是否启用了硬件虚拟化


首先，你需要验证你的系统是否启用了虚拟化功能。在大多数现代系统上，此功能已在 BIOS 中启用。但可以肯定的是，你可以验证是否如图所示启用了虚拟化。


该命令探测是否存在 VMX（<ruby> 虚拟机扩展 <rt>  Virtual Machine Extension </rt></ruby>），它是英特尔硬件虚拟化的 CPU 标志，或 SVM，它是 AMD 硬件虚拟化的标志。



```
$ cat /proc/cpuinfo | egrep "vmx|svm"

```

从以下输出中，你可以看到我们的系统启用了英特尔硬件虚拟化：


![](/data/attachment/album/202305/25/160235mterwfxif5eezrrx.jpg)


### 2、在 Rocky Linux 9 / AlmaLinux 9 上安装 KVM


确保启用虚拟化后，下一步就是安装 KVM 和管理工具。为此，请运行以下 `dnf` 命令。



```
$ sudo dnf install qemu-kvm virt-manager libvirt virt-install virt-viewer virt-top bridge-utils  bridge-utils virt-top libguestfs-tools -y

```

![](/data/attachment/album/202305/25/160242b3dyjj3xc3d8s2h5.jpg)


安装完成后，运行以下命令检查是否已加载所需的 KVM 模块。



```
$ lsmod | grep kvm

```

你应该得到以下输出以确认已加载必要的模块：


![](/data/attachment/album/202305/25/160249fojj180t7asyyy6o.jpg)


### 3、启动并启用 libvirtd 守护进程


在下一步中，一定要启动 libvirtd 守护进程。这是一个服务器端守护程序组件，可在虚拟化的客户机上运行和管理任务。它用于管理虚拟化技术，例如 Xen、KVM 和 ESXi 等等。


要启动 libvirtd 守护进程，请运行以下命令：



```
$ sudo systemctl start libvirtd

```

请务必启用该服务以在引导时启动：



```
$ sudo systemctl enable --now libvirtd

```

验证 libvirtd 守护进程是否正在运行，如下所示：



```
$ sudo systemctl status libvirtd

```

![](/data/attachment/album/202305/25/160256t7vsaj7dlgppygop.jpg)


### 4、设置桥接接口


到目前为止，我们已经安装了 KVM 和所有管理工具，事实上，我们可以继续启动虚拟机。但是，如果我们可以从管理程序网络外部访问虚拟机，那就太好了。为此，我们需要创建一个桥接接口。


首先，确定系统上的网络接口。



```
$ sudo nmcli connection show

```

从输出来看，`ens160` 是活动的网络接口，请务必注意你的情况下的接口，因为你将一路使用它。


![](/data/attachment/album/202305/25/160304jkfynx9d9xys39y3.jpg)


要开始创建网桥，首先，使用以下语法用其 UUID 删除连接：



```
$ sudo nmcli connection delete UUID

```

在我们的例子中，命令将是：



```
$ sudo nmcli connection delete 19e98123-9a84-30a6-bc59-a7134446bb26

```

你将收到连接已成功删除的确认信息。


![](/data/attachment/album/202305/25/160315n1uf7r7si8fi67sm.jpg)


在继续进行之前，最好准备好以下详细信息：


* 网桥名称 – 新网桥的首选名称（例如 `br1`）
* 设备名称 – 这是你的网络接口的名称。它将作为网桥的从属设备（例如，`ens160`）
* IP 地址/子网 – 桥接网络的 IP 地址和子网（例如 `192.168.2.50/24`）。请注意，这应该与你的网络子网和 IP 地址相对应。
* 网关 – 你网络的默认网关地址（例如 `192.168.2.1`）
* DNS1 和 DNS2 – 首选 DNS 地址（例如 `8.8.8.8` 和 `8.8.4.4`）


继续，使用以下语法创建一个新的桥接接口。



```
$ sudo nmcli connection add type bridge autoconnect yes con-name BRIDGE NAME ifname BRIDGE NAME

```

在我们的例子中，`br1` 是首选的网桥接口名称。因此，命令将如图所示：



```
$ sudo nmcli connection add type bridge autoconnect yes con-name br1 ifname br1

```

![](/data/attachment/album/202305/25/160324tbvbin1ainv7ku1z.jpg)


在接下来的步骤中，你将通过指定 IP 子网、网关和 DNS 值来修改网桥。


首先使用以下语法指定 IP 子网：



```
$ sudo nmcli connection modify BRIDGE NAME ipv4.addresses IP ADDRESS/SUBNET ipv4.method manual

```

根据我们的设置，命令将是：



```
$ sudo nmcli connection modify br1 ipv4.addresses 192.168.2.150/24 ipv4.method manual

```

接下来，使用以下语法指定网关地址：



```
$ sudo nmcli connection modify BRIDGE NAME ipv4.gateway GATEWAY

```

根据我们的网络，该命令采用以下格式：



```
$ sudo nmcli connection modify br1 ipv4.gateway 192.168.2.1

```

DNS 地址的语法如下：



```
$ sudo nmcli connection modify BRIDGE NAME ipv4.dns DNS1 +ipv4.dns DNS2

```

该命令采用以下格式：



```
$ sudo nmcli connection modify br1 ipv4.dns 8.8.8.8 +ipv4.dns 8.8.4.4

```

![](/data/attachment/album/202305/25/160339zcc2vhcc2h61kzxx.jpg)


此后，使用以下命令添加网桥从属设备：



```
$ sudo nmcli connection add type bridge-slave autoconnect yes con-name DEVICE NAME ifname DEVICE NAME master BRIDGE NAME

```

使用我们的值，命令如图所示：



```
$ sudo nmcli connection add type bridge-slave autoconnect yes con-name ens160 ifname ens160 master br1

```

你将收到以下确认信息，表明已成功添加网桥从属设备。请记住，桥接从属设备是你的网络接口或适配器。


![](/data/attachment/album/202305/25/160349a4hzz16xzzqq1mx0.jpg)


要确认网桥已创建，请运行以下命令：



```
$ sudo nmcli connection show

```

从输出中，你可以看到列出了网桥接口。


![](/data/attachment/album/202305/25/160401o83frylp6rohplot.jpg)


激要活它，请运行以下命令：



```
$ sudo nmcli connection up br1

```

![](/data/attachment/album/202305/25/160410eqa68k8la88igrws.jpg)


此外，你可以使用 `ip addr` 命令验证：



```
$ ip addr | grep br1

```

![](/data/attachment/album/202305/25/160419b0r9rxd7v1ozydze.jpg)


最后，编辑网桥配置文件。



```
$ sudo vi /etc/qemu-kvm/bridge.conf

```

添加以下行：



```
allow all

```

然后重新启动虚拟化守护进程以应用更改



```
$ sudo systemctl restart libvirtd

```

### 5、创建虚拟机


安装 KVM 并配置桥接连接后，现在让我们创建一个虚拟机。在执行之前，为登录用户分配必要的所有权，以便在不切换到 root 的情况下运行命令。



```
$ sudo chown -R $USER:libvirt /var/lib/libvirt/

```

在命令行上，我们将使用以下语法使用 Ubuntu 20.04 ISO 镜像创建虚拟机。



```
$ virt-install \
    --name Ubuntu \
    --ram 2048 \
    --vcpus 2 \
    --disk path=/var/lib/libvirt/images/ubuntu-20.04.img,size=15 \
    --os-variant ubuntu20.04 \
    --network bridge=br1,model=virtio \
    --graphics vnc,listen=0.0.0.0 \
    --console pty,target_type=serial \
    --cdrom /home/linuxtechi/Downloads/ubuntu-20.04.4-desktop-amd64.iso

```

![](/data/attachment/album/202305/25/160430tslv6t61s2wlulxs.jpg)


执行该命令后，将启动图形屏幕会话，并开始安装客户操作系统。


![](/data/attachment/album/202305/25/160439bb6fdggaf60dbr88.jpg)


![](/data/attachment/album/202305/25/160452snhizitui7v0ivwp.jpg)


### 总结


我们关于如何在 Rocky Linux 9 / AlmaLinux 9 上安装 KVM 的文章到此结束，非常欢迎你提供反馈。


*（题图：MJ/a364d6e3-0c59-4be8-bf02-5df078359429）*




---


via: <https://www.linuxtechi.com/install-kvm-on-rocky-linux-almalinux/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed/) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
In this guide, we demonstrate how to install KVM on Rocky Linux 9 / Alma Linux 9.

[KVM](https://www.linux-kvm.org/page/Main_Page), short for Kernel Virtualization Machine, is an opensource virtualization platform designed for the Linux kernel. It’s a type 1 hypervisor, or commonly referred to as a bare metal hypervisor. It allows users to create and manage multiple guest machines which can be spun from either Linux or Windows operating systems.

Like most virtualization platforms, it abstracts hardware resources such as CPU, memory, storage, network, graphics etc. and allocates them to guest machines which run independently of the host.

#### Prerequisites

- Pre Installed Rocky Linux 9 / AlmaLinux 9
[Sudo User](https://www.linuxtechi.com/create-sudo-user-on-rhel-rocky-linux-almalinux/)with admin rights- Internet Connectivity

## 1) Verify if hardware Virtualization is Enabled

To start off, you need to verify if your system has the virtualization feature enabled. On most modern systems, this feature comes already enabled in the BIOS. But just to be sure, you can verify if virtualization is enabled as shown.

The command probes for the presence of vmx (Virtual Machine Extension) which is a CPU flag for Intel hardware virtualization or svm which is the flag for AMD hardware virtualization.

$ cat /proc/cpuinfo | egrep "vmx|svm"

From the following output, you can see that our system has Intel Hardware virtualization enabled.

## 2) Install KVM on Rocky Linux 9 / AlmaLinux 9

Once you have ensured that virtualization is enabled, the next step is to install KVM and management tools. To do so, run the following dnf command.

$ sudo dnf install qemu-kvm virt-manager libvirt virt-install virt-viewer virt-top bridge-utils bridge-utils virt-top libguestfs-tools -y

Once the installation is complete, run the following command to check if the required KVM modules have been loaded.

$ lsmod | grep kvm

You should get the following output to confirm that the necessary modules have been loaded.

## 3) Start and Enable the libvirtd daemon

In the next step, be sure to start the libvirtd daemon. This is a server-side daemon component that runs and manages tasks on virtualized guests. It is used to manage virtualization technologies such as Xen, KVM, and ESXi to mention a few.

To start the libvirtd daemon, run the command:

$ sudo systemctl start libvirtd

Be sure to enable the service to start on boot time.

$ sudo systemctl enable --now libvirtd

Verify that the libvirtd daemon is running as follows.

$ sudo systemctl status libvirtd

## 4) Setup the Bridge Interface

So far, we have installed KVM, and all the management tools and we can, in fact, proceed to spin up a virtual machine. However, it would be nice if we can access the VMs from outside the hypervisor network. To accomplish this, we need to create a bridge interface.

First, identify the network interfaces on your system.

$ sudo nmcli connection show

From the output, ens160 is the active network interface, be sure to take note of the interface in your case as you will use it along the way.

To begin creating the bridge, first, delete the connection using its UUID in the following syntax.

$ sudo nmcli connection delete UUID

In our case, the command will be:

$ sudo nmcli connection delete 19e98123-9a84-30a6-bc59-a7134446bb26

You will get a confirmation that the connection has successfully been deleted.

Before proceeding any further, it would be prudent to have the following details at hand:

- BRIDGE NAME – Preferred name of the new bridge (e.g. “br1”)
- DEVICE NAME – This is the name of your network interface. This will serve as the bridge slave (e.g., “ens160”)
- IP ADDRESS/SUBNET – The IP address and subnet for the bridge network (e.g., “192.168.2.50/24”). Note that this should correspond with your network subnet and IP addressing.
- GATEWAY – Default gateway address of your network (e.g. “192.168.2.1”)
- DNS1 and DNS2 – Preferred DNS addresses (e.g. “8.8.8.8” and “8.8.4.4”)

Moving on, create a new bridge interface using the following syntax.

$ sudo nmcli connection add type bridge autoconnect yes con-name BRIDGE NAME ifname BRIDGE NAME

In our case, br1 is the preferred bridge interface name. Therefore, the command will be as shown.

$ sudo nmcli connection add type bridge autoconnect yes con-name br1 ifname br1

In the next steps, you will modify the bridge by specifying the IP subnet, Gateway, and DNS values.

Start off by specifying the IP subnet using the following syntax.

$ sudo nmcli connection modify BRIDGE NAME ipv4.addresses IP ADDRESS/SUBNET ipv4.method manual

According to our setup, the command will be.

$ sudo nmcli connection modify br1 ipv4.addresses 192.168.2.150/24 ipv4.method manual

Next, specify the gateway address using the following syntax

$ sudo nmcli connection modify BRIDGE NAME ipv4.gateway GATEWAY

According to our network, the command takes the following format.

$ sudo nmcli connection modify br1 ipv4.gateway 192.168.2.1

The syntax for the DNS address is as follows.

$ sudo nmcli connection modify BRIDGE NAME ipv4.dns DNS1 +ipv4.dns DNS2

And the command takes the following format.

$ sudo nmcli connection modify br1 ipv4.dns 8.8.8.8 +ipv4.dns 8.8.4.4

Thereafter, use the following command to add the bridge slave.

$ sudo nmcli connection add type bridge-slave autoconnect yes con-name DEVICE NAME ifname DEVICE NAME master BRIDGE NAME

Using our values, the command is as shown.

$ sudo nmcli connection add type bridge-slave autoconnect yes con-name ens160 ifname ens160 master br1

You will get the following confirmation that the bridge slave was successfully added. Keep in mind that the bridge slave is your network interface or adapter.

To confirm that the bridge was created, run the following command:

$ sudo nmcli connection show

From the output, you can see that the bridge interface is listed.

To activate it, run the command:

$ sudo nmcli connection up br1

In addition, you can verify this using the [ip addr ](https://www.linuxtechi.com/ip-command-examples-for-linux-users/)command.

$ ip addr | grep br1

Lastly, edit the bridge configuration file.

$ sudo vi /etc/qemu-kvm/bridge.conf

Add the following line.

allow all

Then restart the virtualization daemon to apply the change

$ sudo systemctl restart libvirtd

## 5) Create a Virtual Machine

With KVM installed and the bridge connection configured, let’s now create a virtual machine. Before doing, assign the necessary ownership right to the logged-in user in order to run commands without switching to root.

$ sudo chown -R $USER:libvirt /var/lib/libvirt/

On the command line, we will create a virtual machine using Ubuntu 20.04 ISO image using the following syntax.

$ virt-install \ --name Ubuntu \ --ram 2048 \ --vcpus 2 \ --disk path=/var/lib/libvirt/images/ubuntu-20.04.img,size=15 \ --os-variant ubuntu20.04 \ --network bridge=br1,model=virtio \ --graphics vnc,listen=0.0.0.0 \ --console pty,target_type=serial \ --cdrom /home/linuxtechi/Downloads/ubuntu-20.04.4-desktop-amd64.iso

Once the command is executed, the graphical screen session will be launched, and the installation of the guest operating system will commence.

#### Conclusion

This concludes our article on how to install KVM on Rocky Linux 9 / AlmaLinux 9 Your feedback s highly welcome.

Also Read: [How to Manage KVM Virtual Machines with Cockpit Web Console](https://www.linuxtechi.com/manage-virtual-machines-cockpit-console/)