2022-05-25 10:49:49 +08:00
[#]: subject: "How to Install KVM on Ubuntu 22.04 (Jammy Jellyfish)"
[#]: via: "https://www.linuxtechi.com/how-to-install-kvm-on-ubuntu-22-04/"
[#]: author: "James Kiarie https://www.linuxtechi.com/author/james/"
[#]: collector: "lkxed"
2022-05-26 10:49:24 +08:00
[#]: translator: "turbokernel"
2022-05-25 10:49:49 +08:00
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
2022-05-26 13:16:41 +08:00
Ubuntu 22.04 (Jammy Jellyfish) 之 KVM 安装手札
2022-05-25 10:49:49 +08:00
======
2022-05-30 14:06:05 +08:00
**KVM** 是 **Kernel-based Virtual Machine** 首字母缩写,一项嵌入内核的开源虚拟化技术。其属于标准的基于内核的裸设备虚拟化引擎。
2022-05-25 10:49:49 +08:00
2022-05-30 14:06:05 +08:00
在 **KVM** 之上可以运行 Windows 和 Liunx 虚拟机。每个虚拟机之间相互独立并独享 CPU、内存、网络设备、存储设备等相关资源。
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
本文将介绍在 Ubuntu 22.04 LTS (Jammy Jellyfish) 中如何安装 **KVM** 。在文末,我们也将演示在安装 **KVM** 完成之后如何创建虚拟机。
2022-05-25 10:49:49 +08:00
2022-05-30 14:06:05 +08:00
### 1) 更新 Ubuntu 22.04
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
在一切开始前, 打开终端并通过如下命令更新更新apt软件包索引:
2022-05-25 10:49:49 +08:00
```
$ sudo apt update
```
2022-05-31 11:41:43 +08:00
### 2) 检查虚拟化是否开启
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
首先需要检查您的CPU是否支持 KVM 虚拟化。确保您系统中安装了 Intel 处理器 VT-x( vmx ) 或 AMD AMD-V (svm) 处理器。
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
您可以通过运行如下命令, 根据返回的结果是否大于0判断虚拟化是否启用。如果返回值等于0, 则表示虚拟化已禁用, 您需要先启用虚拟化。
2022-05-25 10:49:49 +08:00
```
$ egrep -c '(vmx|svm)' /proc/cpuinfo
```
![SVM-VMX-Flags-Cpuinfo-linux][1]
2022-05-31 12:47:55 +08:00
根据上方命令输出, 您通过返回值是否大于0判断虚拟化环境启用与否。如果虚拟化未启用, 请在BIOS设置中将其开启。
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
另外,您可以通过如下命令判断 KVM 虚拟化是否已经在运行:
2022-05-25 10:49:49 +08:00
```
$ kvm-ok
```
2022-05-31 12:47:55 +08:00
请确保您已经安装了 cpu-checker 软件包,否则将提示未找到该命令的报错。
2022-05-25 10:49:49 +08:00
2022-05-31 12:47:55 +08:00
关于此类问题的解决方案以及 cp-checker 软件包的安装,您可以参考如下:
2022-05-25 10:49:49 +08:00
![KVM-OK-Command-Not-Found-Ubuntu][2]
2022-05-31 11:41:43 +08:00
随后,通过如下命令安装 cpu-checker 软件包:
2022-05-25 10:49:49 +08:00
```
$ sudo apt install -y cpu-checker
```
2022-05-31 11:41:43 +08:00
接着运行 kvm-ok 命令,如果 KVM 已经启动,您将看到如下输出:
2022-05-25 10:49:49 +08:00
```
$ kvm-ok
```
![KVM-OK-Command-Output][3]
2022-05-31 11:41:43 +08:00
### 3) 在 Ubuntu 22.04 上安装 KVM
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
随后,通过如下命令在 Ubuntu 22.04 中安装 KVM 以及其他相关虚拟化软件包:
2022-05-25 10:49:49 +08:00
```
$ sudo apt install -y qemu-kvm virt-manager libvirt-daemon-system virtinst libvirt-clients bridge-utils
```
2022-05-31 11:41:43 +08:00
为您解释下刚刚安装了哪些软件包:
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
* qemu-kvm – 一款开源模拟器,为虚拟化安装包提供硬件模拟的支持
* virt-manager – 一款基于 **QT** 和 **libvirt** 的图形的 libvirt 虚拟机管理工具
* libvirt-daemon-system – 一款为运行 libvirt 进程提供必要配置文件的工具
* virtinst – 一套为置备和修改虚拟机提供的命令行工具
2022-05-31 12:47:55 +08:00
* libvirt-clients – 一套提供管理和控制虚拟机以及虚拟化引擎的客户端侧库和API命令行工具
* bridge-utils – 一套用于创建和管理桥接设备的工具
2022-05-31 11:41:43 +08:00
### 4) 启用虚拟化守护进程 (libvirtd)
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
在所有软件包安装完毕之后,通过如下命令 启用并启动 Libvirt 守护进程:
2022-05-25 10:49:49 +08:00
```
$ sudo systemctl enable --now libvirtd
$ sudo systemctl start libvirtd
```
2022-05-31 12:47:55 +08:00
您可以通过如下命令验证虚拟化守护进程是否已经运行:
2022-05-25 10:49:49 +08:00
```
$ sudo systemctl status libvirtd
```
![Libvirtd-Status-Ubuntu-Linux][4]
2022-05-31 12:47:55 +08:00
另外,请将当前登录用户加入 kvm 和 libvirt 用户组,以便能够创建和管理虚拟机。
2022-05-25 10:49:49 +08:00
```
$ sudo usermod -aG kvm $USER
$ sudo usermod -aG libvirt $USER
```
2022-05-31 12:47:55 +08:00
$USER 环境变量引用的即为当前登录的用户名。您需要重新登录才能使得配置生效。
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
### 5) 创建网桥(br0)
2022-05-25 10:49:49 +08:00
If you are planning to access KVM virtual machines outside from your Ubuntu 22.04 system, then you must map VM’ s interface to a network bridge. Though a virtual bridge named virbr0, created automatically when KVM is installed but it is used for testing purposes.
To create a network bridge, create the file ‘ 01-netcfg.yaml’ with following content under the folder /etc/netplan.
```
$ sudo vi /etc/netplan/01-netcfg.yaml
network:
ethernets:
enp0s3:
dhcp4: false
dhcp6: false
# add configuration for bridge interface
bridges:
br0:
interfaces: [enp0s3]
dhcp4: false
addresses: [192.168.1.162/24]
macaddress: 08:00:27:4b:1d:45
routes:
- to: default
via: 192.168.1.1
metric: 100
nameservers:
addresses: [4.2.2.2]
parameters:
stp: false
dhcp6: false
version: 2
```
2022-06-01 08:48:51 +08:00
保存并推出文件。
2022-05-25 10:49:49 +08:00
2022-06-01 08:48:51 +08:00
注:上述文件的配置是我环境中的,请根据您实际环境替换 IP 地址、网口名称以及mac地址。
2022-05-25 10:49:49 +08:00
2022-06-01 08:48:51 +08:00
您可以通过运行 ‘ netplan apply’ 命令应用上述变更。T
2022-05-25 10:49:49 +08:00
```
$ sudo netplan apply
```
2022-05-31 11:41:43 +08:00
您可以通过如下命令,验证网桥 ‘ br0’ :
2022-05-25 10:49:49 +08:00
```
$ ip add show
```
![Network-Bridge-br0-ubuntu-linux][5]
2022-05-31 11:41:43 +08:00
### 6) 启动 KVM 虚拟机管理器
2022-05-25 10:49:49 +08:00
With KVM installed, you can begin creating your virtual machines using the virt-manager GUI tool. To get started, use the GNOME search utility and search for ‘ Virtual machine Manager’ .
Click on the icon that pops up.
![Access-Virtual-Machine-Manager-Ubuntu-Linux][6]
This launches the Virtual Machine Manager Interface.
![Virtual-Machine-Manager-Interface-Ubuntu-Linux][7]
Click on “File” then select “New Virtual Machine”. Alternatively, you can click on the button shown.
![New-Virtual-Machine-Icon-Virt-Manager][8]
This pops open the virtual machine installation wizard which presents you with the following four options:
* Local install Media ( ISO image or CDROM )
* Network Install ( HTTP, HTTPS, and FTP )
* Import existing disk image
* Manual Install
In this guide, we have downloaded a Debian 11 ISO image, and therefore, if you have an ISO image, select the first option and click ‘ Forward’ .
![Local-Install-Media-ISO-Virt-Manager][9]
In the next step, click ‘ Browse’ to navigate to the location of the ISO image,
![Browse-ISO-File-Virt-Manager-Ubuntu-Linux][10]
In the next window, click ‘ Browse local’ in order to select the ISO image from the local directories on your Linux PC.
![Browse-Local-ISO-Virt-Manager][11]
As demonstrated below, we have selected the Debian 11 ISO image. Then click ‘ Open’
![Choose-ISO-File-Virt-Manager][12]
Once the ISO image is selected, click ‘ Forward’ to proceed to the next step.
![Forward-after-browsing-iso-file-virt-manager][13]
Next, define the RAM and the number of CPU cores for your virtual machine and click ‘ Forward’ .
![Virtual-Machine-RAM-CPU-Virt-Manager][14]
2022-05-31 11:41:43 +08:00
下一页中,输入虚拟机磁盘空间,并点击 ‘ Forward’ 继续。
2022-05-25 10:49:49 +08:00
![Storage-for-Virtual-Machine-KVM-Virt-Manager][15]
To associate virtual machine’ s nic to network bridge, click on ‘ Network selection’ and choose br0 bridge.
![Network-Selection-KVM-Virtual-Machine-Virt-Manager][16]
2022-05-31 11:41:43 +08:00
最后,点击 ‘ Finish’ 按钮结束设置虚拟机。
2022-05-25 10:49:49 +08:00
![Choose-Finish-to-OS-Installation-KVM-VM][17]
2022-05-31 11:41:43 +08:00
稍等片刻,虚拟机的创建过程将开始。
2022-05-25 10:49:49 +08:00
![Creating-Domain-Virtual-Machine-Virt-Manager][18]
2022-05-31 11:41:43 +08:00
当创建结束时,虚拟机将开机并进入系统安装界面。如下是 Debian 11 的安装选项。在这里您可以根据需要进行系统安装。
2022-05-25 10:49:49 +08:00
![Virtual-Machine-Console-Virt-Manager][19]
2022-05-31 11:41:43 +08:00
##### 小结
2022-05-25 10:49:49 +08:00
2022-05-31 11:41:43 +08:00
至此,本文向您演示了如何在 Ubuntu 22.04 上 安装 KVM 虚拟化引擎。您的反馈对我们至关重要。
2022-05-25 10:49:49 +08:00
--------------------------------------------------------------------------------
via: https://www.linuxtechi.com/how-to-install-kvm-on-ubuntu-22-04/
作者:[James Kiarie][a]
选题:[lkxed][b]
2022-05-26 10:49:24 +08:00
译者:[turbokernel](https://github.com/turbokernel)
2022-05-25 10:49:49 +08:00
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT ](https://github.com/LCTT/TranslateProject ) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://www.linuxtechi.com/author/james/
[b]: https://github.com/lkxed
[1]: https://www.linuxtechi.com/wp-content/uploads/2022/05/SVM-VMX-Flags-Cpuinfo-linux.png
[2]: https://www.linuxtechi.com/wp-content/uploads/2022/05/KVM-OK-Command-Not-Found-Ubuntu.png
[3]: https://www.linuxtechi.com/wp-content/uploads/2022/05/KVM-OK-Command-Output.png
[4]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Libvirtd-Status-Ubuntu-Linux.png
[5]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Network-Bridge-br0-ubuntu-linux.png
[6]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Access-Virtual-Machine-Manager-Ubuntu-Linux.png
[7]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Virtual-Machine-Manager-Interface-Ubuntu-Linux.png
[8]: https://www.linuxtechi.com/wp-content/uploads/2022/05/New-Virtual-Machine-Icon-Virt-Manager.png
[9]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Local-Install-Media-ISO-Virt-Manager.png
[10]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Browse-ISO-File-Virt-Manager-Ubuntu-Linux.png
[11]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Browse-Local-ISO-Virt-Manager.png
[12]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Choose-ISO-File-Virt-Manager.png
[13]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Forward-after-browsing-iso-file-virt-manager.png
[14]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Virtual-Machine-RAM-CPU-Virt-Manager.png
[15]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Storage-for-Virtual-Machine-KVM-Virt-Manager.png
[16]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Network-Selection-KVM-Virtual-Machine-Virt-Manager.png
[17]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Choose-Finish-to-OS-Installation-KVM-VM.png
[18]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Creating-Domain-Virtual-Machine-Virt-Manager.png
[19]: https://www.linuxtechi.com/wp-content/uploads/2022/05/Virtual-Machine-Console-Virt-Manager.png