Merge pull request #29427 from wxy/20230429.1-️-Install-and-Use-Qemu-on-Ubuntu

RP:published/20230429.1 ️ Install and Use Qemu on Ubuntu.md
This commit is contained in:
Xingyu.Wang 2023-05-22 11:08:01 +08:00 committed by GitHub
commit ed60f1325d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 286 additions and 280 deletions

View File

@ -0,0 +1,286 @@
[#]: subject: "Install and Use Qemu on Ubuntu"
[#]: via: "https://itsfoss.com/qemu-ubuntu/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: "wxy"
[#]: reviewer: "wxy"
[#]: publisher: "wxy"
[#]: url: "https://linux.cn/article-15834-1.html"
在 Ubuntu 上安装和使用 Qemu
======
![][0]
> 学习如何在 Ubuntu 中正确安装 Qemu然后在虚拟机中配置 Linux 发行版。
如果你需要运行 Linux 虚拟机Qemu 是目前最快的管理程序之一,甚至没有“之一”。
除了速度之外,你还可以获得出色的配置选项。即使你不是一个资深用户,它也给你足够的调整空间。
在本教程中,我将引导你完成:
- 在 Ubuntu 上安装 Qemu 和 KVM
- 在 Qemu 上安装另一个发行版
- 在 Qemu 中启用共享文件夹、自动调整大小和复制粘贴(可选,但会使你的虚拟机体验更顺畅)。
那么让我们从安装开始吧。
### 在 Ubuntu 上安装 Qemu 和 KVM
> ✋ 在开始安装之前,让我们检查一下你的机器是否支持虚拟化。 
要做到这一点,请使用以下命令:
```
LC_ALL=C lscpu | grep Virtualization
```
如果你有一个英特尔处理器,它应该带来以下输出:
![检查该计算机是否支持基于硬件的虚拟化][1]
如果你有一个 AMD 处理器,它应该得到以下输出:
```
Virtualization: AMD-V
```
另外,[确保你的系统有多个处理器核心][2],这样你可以分配一些给你的虚拟机,并且仍然有足够的处理能力提供给宿主机。
如果你不确定,使用下面的命令,**输出应该大于零**
```
egrep -c '(vmx|svm)' /proc/cpuinfo
```
![检查 Linux 中系统的核心总数和线程数][3]
一旦你都准备好了,你就可以用以下命令在 Ubuntu 上安装 Qemu 和 KVM
```
sudo apt install qemu qemu-kvm virt-manager bridge-utils
```
安装结束后,**重新启动你的系统**。
#### 将用户添加到适当的组中
为了使 Qemu 工作,你必须 [将你的用户加入两个组][4]`libvirt-kvm` 和 `libvirt`
要做到这一点,请逐一使用以下命令:
```
sudo useradd -g $USER libvirt
sudo useradd -g $USER libvirt-kvm
```
接下来,启用并启动 `libvirt` 服务:
```
sudo systemctl enable libvirtd.service && sudo systemctl start libvirtd.service
```
这就行了Qemu 的安装已经完成。
### 用 Qemu 安装另一个 Linux 虚拟机
> 📋 请下载你想在 Qemu 虚拟机中安装的 Linux 发行版的 ISO 文件。
首先,从系统菜单中打开 <ruby>虚拟机管理器<rt>Virtual Machine Manager</rt></ruby>
![从系统菜单中启动 Qemu][5]
接下来,点击 “<ruby>文件<rt>File</rt></ruby>” 菜单,选择 “<ruby>新建虚拟机<rt>New Virtual Machine</rt></ruby>” 选项:
![在 Qemu 中创建新的虚拟机][6]
从这里,选择第一个选项 “<ruby>本地安装介质<rt>Local install media</rt></ruby>”,这将允许你从文件管理器中选择 ISO
![选择本地 ISO 文件以在 Qemu 中创建新的虚拟机][7]
接下来,你将被要求添加 ISO 文件。在这里,点击 “<ruby>浏览<rt>Browse</rt></ruby>” 按钮;它将打开一个新的提示窗口,在那里,点击 “<ruby>浏览本地<rt>Browse Local</rt></ruby>”。
它将打开文件管理器,从那里选择 ISO 文件:
![在 Qemu 中导入 ISO 文件][8]
**在大多数情况下ISO 文件会被检测到,但如果你发现它没有自动检测到,请按照以下步骤操作:**
- 取消勾选 “<ruby>自动从安装介质/源检测<rt>Automatically detect from the installtion media / source</rt></ruby>” 选项
- 选择 “<ruby>常见的 Linux<rt>Generic Linux</rt></ruby>” 选项
![解决 Qemu 中未检测到 ISO 的问题][9]
接下来,你将被要求根据你的需要分配内存和存储。我建议桌面版至少要有 2 个核心、4GB 内存和 25GB 的存储空间:
![在 Qemu 中为虚拟机分配内存、内核和存储空间][10]
> 📋 除了磁盘空间CPU 和内存等系统资源只在 VM 中运行操作系统时使用。
最后,给你的虚拟机起一个合适的名字;完成后,点击 “<ruby>完成<rt>Finish</rt></ruby>” 按钮:
![在 Qemu 中命名你的虚拟机][11]
它将加载 ISO所以你可以从这里开始安装。
这就完成了。你可能会觉得这已经很不错了,但如果你启用共享文件夹、剪贴板共享等,你会更喜欢它。下面的几节将介绍如何做到这一点。
### 在 Qemu 中启用共享文件夹(可选)
在本节中,我将向你展示如何将宿主机的现有目录与虚拟机共享。
要做到这一点,你必须执行以下步骤:
- 通过 Qemu 中的虚拟机设置添加一个共享文件系统
- 在虚拟机中挂载文件系统
因此,首先从系统菜单中打开虚拟机管理器,选择虚拟机,并点击 “<ruby>打开<rt>Open</rt></ruby>” 按钮来管理控制台:
![打开 Qemu 中虚拟机的设置][12]
现在,点击 “<ruby>信息<rt>Info</rt></ruby>” 按钮,选择 “<ruby>内存<rt>Memory</rt></ruby>” 并启用共享内存:
![在 Qemu 中启用共享内存][13]
接下来,点击 “<ruby>添加硬件<rt>Add Hardware</rt></ruby>” 按钮,选择 “<ruby>文件系统<rt>Filesystem</rt></ruby>” 选项。
在这里,你必须做以下工作:
- 在 “<ruby>源路径<rt>Source Path</rt></ruby>” 部分添加一个你想共享的目录的路径
- 在 “<ruby>目标路径<rt>Target Path</rt></ruby>” 部分添加该目录的名称
![在 Qemu 中创建共享文件夹][14]
完成后,点击 “<ruby>完成<rt>Finish</rt></ruby>” 按钮,启动虚拟机。
在虚拟机中,打开终端,使用下面的语法来挂载共享目录:
```
sudo mount -t virtiofs sharename path/to/shared/directory
```
在我的例子中,它是 `Downloads` 目录,所以我将使用下面的方式:
```
sudo mount -t virtiofs Downloads /home/sagar/Downloads
```
![在 Qemu 中挂载共享文件夹][15]
这就行了。
**但这是一个临时的解决方案。**
要使它成为永久性的,你必须在虚拟机的 `/etc/fstab` 中创建一个条目。
要这样做,首先,用下面的方法打开 `/etc/fstab` 配置文件:
```
sudo nano /etc/fstab
```
按下`Alt + /`[在 nano 文本编辑器中转到文件的末尾][16] ,并使用以下语法创建一个条目:
```
sharename path/to/shared/directory virtiofs defaults 0 0
```
这是我的配置,看起来像这样:
![使 Qemu 中的共享文件夹永久化][17]
一旦完成,[保存更改并退出 nano][18] 文本编辑器。
这里我展示了我是如何在主机上的 `Downloads` 目录下创建一个新文件,并且这些变化反映在我的虚拟机上:
![在 Qemu 中使用共享文件夹][19]
因此,现在你可以使用这个共享文件夹在主机和虚拟机之间传输文件,没有任何问题了!
### 在 Qemu 中启用共享剪贴板(可选)
要启用共享剪贴板,你所要做的就是在虚拟机中安装 `spice-vdagent` 工具。
因此,如果你的虚拟机是基于 Ubuntu/Debian 的,你可以使用以下方法:
```
sudo apt install spice-vdagent
```
对于基于 Arch 的发行版:
```
sudo pacman -S spice-vdagent
```
对于基于 Fedora 的发行版:
```
sudo yum install spice-vdagent
```
一旦你完成了安装,**重启你的虚拟机**,剪贴板应该可以如期工作了。
### 在 Qemu 中启用自动调整大小(可选)
自动调整大小的功能没什么,但当你调整虚拟机窗口的大小时,虚拟机的显示会立即适应大小的变化:
![Qemu 中的自动调整大小][20]
要启用 Qemu 中的自动调整大小功能,你必须遵循 2 个简单的步骤:
- 点击 “<ruby>视图<rt>View</rt></ruby>”(从顶部菜单栏)。
- 选择 “<ruby>缩放显示<rt>Scale Display</rt></ruby>” 并选择 “<ruby>总是<rt>Always</rt></ruby>” 选项。
![在 Qemu 中启用自动调整大小][21]
这就是我这边看到的情况了。
### 想要使用 Qemu 的即插即用版本?
在本教程中,我介绍了如何在 Qemu 中手动安装虚拟机,但如果我告诉你,你可以自动完成整个过程,如分配存储空间、内存等步骤呢?
是的,它提供了同样的效率,但当你想尽快创建一个新的虚拟机时,它就会派上用场!这个工具叫 Quickgui我们有一个专门的安装教程
> **[用基于 Qemu 的 Quickgui 轻松地创建虚拟机](https://itsfoss.com/quickgui/)**
我希望你会发现这个指南对你有帮助。如果你有任何疑问,欢迎在评论中提问。
*题图MJ/b3c4d5b2-e727-4b70-9bb8-e864941eef9a*
--------------------------------------------------------------------------------
via: https://itsfoss.com/qemu-ubuntu/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[wxy](https://github.com/wxy)
校对:[wxy](https://github.com/wxy)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/content/images/2023/04/check-where-the-computer-supports-hardware-based-virtualization-or-not.png
[2]: https://linuxhandbook.com/check-cpu-info-linux/?ref=itsfoss.com
[3]: https://itsfoss.com/content/images/2023/04/Check-total-number-of-cores-and-threads-of-system-in-linux.png
[4]: https://learnubuntu.com/add-user-group/?ref=itsfoss.com
[5]: https://itsfoss.com/content/images/2023/04/Start-Qemu-from-the-system-menu-1.png
[6]: https://itsfoss.com/content/images/2023/04/Create-new-VM-in-Qemu.png
[7]: https://itsfoss.com/content/images/2023/04/Choose-the-local-ISO-file-to-create-new-VM-in-Qemu.png
[8]: https://itsfoss.com/content/images/2023/04/Import-the-ISO-file-in-QEMU.png
[9]: https://itsfoss.com/content/images/2023/04/Deal-with-ISO-not-detected-in-Qemu.png
[10]: https://itsfoss.com/content/images/2023/04/Allocate-RAM--cores-and-storage-space-to-VM-in-Qemu.png
[11]: https://itsfoss.com/content/images/2023/04/Name-your-VM-in-Qemu.png
[12]: https://itsfoss.com/content/images/2023/04/open-the-settings-for-VM-in-Qemu.png
[13]: https://itsfoss.com/content/images/2023/04/enable-shared-memory-in-Qemu.png
[14]: https://itsfoss.com/content/images/2023/04/enable-shared-directory-in-Qemu.png
[15]: https://itsfoss.com/content/images/2023/04/use-shared-folders-in-Qemu.png
[16]: https://linuxhandbook.com/beginning-end-file-nano/?ref=itsfoss.com
[17]: https://itsfoss.com/content/images/2023/04/Make-shared-folders-in-Qemu-permenent.png
[18]: https://linuxhandbook.com/nano-save-exit/?ref=itsfoss.com
[19]: https://itsfoss.com/content/images/2023/04/use-shared-folder-in-Qemu.gif
[20]: https://itsfoss.com/content/images/2023/04/Auto-resize-in-Qemu-1.gif
[21]: hhttps://itsfoss.com/content/images/2023/05/Enable_auto-resize_in_Qemu.png
[0]: https://img.linux.net.cn/data/attachment/album/202305/22/105942liixku1fx7uufgi1.jpg

View File

@ -1,280 +0,0 @@
[#]: subject: "Install and Use Qemu on Ubuntu"
[#]: via: "https://itsfoss.com/qemu-ubuntu/"
[#]: author: "Sagar Sharma https://itsfoss.com/author/sagar/"
[#]: collector: "lkxed"
[#]: translator: " "
[#]: reviewer: " "
[#]: publisher: " "
[#]: url: " "
Install and Use Qemu on Ubuntu
======
If you need to run Linux virtual machines, Qemu is one of the fastest hypervisors out there, if not the fastest.
Along with the speed, you also get excellent configuration options. Even if you are not an advanced user, it gives you enough room for tweaks.
In this tutorial, I will walk you through:
- Installing Qemu and KVM on Ubuntu
- Installing another distro on Qemu
- Enabling shared folder, auto-resize, and copy-paste in Qemu (optional but makes your VM experience smoother)
So let's start with the installation.
### Installing Qemu and KVM on Ubuntu
> ✋ Before jumping to the installation, let's check whether your machine supports the virtualization or not. 
To do so, use the following command:
```
LC_ALL=C lscpu | grep Virtualization
```
If you have an intel processor, it should bring the following output:
![check where the computer supports hardware-based virtualization or not][1]
And if you have an AMD processor, it should get you the following output:
```
Virtualization: AMD-V
```
Also, [make sure your system has multiple cores][2] so that you can allocate some to your VM and still have enough to power the host machine.
And if you're not sure, use the following command and the **output should be more than zero:**
```
egrep -c '(vmx|svm)' /proc/cpuinfo
```
![Check total number of cores and threads of system in linux][3]
Once you are all set, you can install Qemu and KVM on Ubuntu using the following command:
```
sudo apt install qemu qemu-kvm virt-manager bridge-utils
```
**Reboot your system** once the installation is over.
#### Add user to appropriate groups
To make Qemu work, you'd have to [add your user to two groups][4]: `libvirt-kvm` and `libvirt`.
To do so, use the following commands one by one:
```
sudo useradd -g $USER libvirt
```
```
sudo useradd -g $USER libvirt-kvm
```
Next, enable and start the `libvirt` service:
```
sudo systemctl enable libvirtd.service && sudo systemctl start libvirtd.service
```
That's it! The installation of Qemu is done.
### Installing another Linux virtually with Qemu
> 📋 Please download the ISO file of the Linux distro you want to install in Qemu VM.
First, open the Virtual Machine Manager from the system menu:
![Start Qemu from the system menu][5]
Next, click on the File menu and choose the `New Virtual Machine` option:
![Create new VM in Qemu][6]
From here, choose the first option `Local install media` which will allow you to choose the ISO from your file manager:
![Choose the local ISO file to create new VM in Qemu][7]
Next, you will be asked to add the ISO file. Here, **click on the Browse button; it** will open a new prompt, and there, **click on the browse local.**
It will open the file manager and from there, select the ISO file:
![Import the ISO file in QEMU][8]
**In most cases, the ISO file will be detected but if you find a situation where it does not detect it automatically, follow the given steps:**
- Uncheck the Automatically detect from the source option
- Choose the `Generic Linux` option
![Deal with ISO not detected in Qemu][9]
Next, you will be asked to allocate memory and storage as per your needs. I would recommend going with a minimum of 2 cores, 4 GB of RAM, and 25 GB of storage for the desktop version:
![Allocate RAM, cores and storage space to VM in Qemu][10]
> 📋 Except for the disk space, system resources like CPU and RAM are only used when running the OS in VM.
And finally, give the appropriate name to your VM; once done, click on the finish button:
![Name your VM in Qemu][11]
It will load the ISO, so you can start the installation from here.
That's done. You may enjoy your VM but you'll even enjoy it better if you enable shared folder, clipboard sharing, etc. The following few sections show how to do that.
### Enabling shared folders in Qemu (optional)
In this section, I will show you how you can share the existing directory of the host machine with the VM.
To do so, you'd have to perform the following:
- Adding a shared filesystem through VM settings in Qemu
- Mounting the FS in the VM
So first, open the Virtual Machine Manager from the system menu, select the VM, and click the `Open` button to manage the console:
![open the settings for VM in Qemu][12]
Now, click on the info button, select the `Memory` and enable the shared memory:
![enable shared memory in Qemu][13]
Next, click on the `Add Hardware` button, select the `Filesystem` option.
Here, you'd have to do the following:
- Add a path to the directory you want to share in the `Source path` section
- Add the name of the directory in the `Target path` section
![create shared folder in qemu][14]
Once done, click on the Finish button and start the VM.
In VM, open the terminal and use the following syntax to mount the shared directory:
```
sudo mount -t virtiofs sharename path/to/shared/directory
```
In my case, it was the `Downloads` directory, so I will be using the following:
```
sudo mount -t virtiofs Downloads /home/sagar/Downloads
```
![mount shared folders in Qemu][15]
That's it!
**But that's a temporary solution.**
To make it permanent, you'd have to make create an entry in `/etc/fstab` In the VM.
To do so, first, open the `/etc/fstab` config file using the following:
```
sudo nano /etc/fstab
```
[Go to the end of the file in the nano text editor][16] using `Alt + /` and use the following syntax to create an entry:
```
sharename path/to/shared/directory virtiofs defaults 0 0
```
Here's what my config looks like:
![Make shared folders in Qemu permenent][17]
Once done, [save changes and exit from the nano][18] text editor.
Here I've shown how I created a new file in my host machine in the `Downloads` directory and the changes were reflected in my VM:
![use shared folder in Qemu][19]
So now you can use this shared folder to transfer files between the host and VM without any issues!
### Enabling shared clipboard in Qemu (optional)
To enable the shared clipboard, all you have to do is install the  `spice-vdagent` utility in the VM.
So if your VM is Ubuntu/Debian based, you can use the following:
```
sudo apt install spice-vdagent
```
For Arch base:
```
sudo pacman -S spice-vdagent
```
For Fedora-based distros:
```
sudo yum install spice-vdagent
```
Once you are done with the installation, **reboot your VM**, and the clipboard should work as intended!
### Enabling auto-resize in Qemu (optional)
The auto-resize feature is nothing but when you resize the VM window, the VM display will adapt to the change in size immediately:
![Auto resize in Qemu][20]
To enable the auto-resize in Qemu, you will have to follow 2 simple steps:
- Click on the `View` (from top menubar)
- Select the `Scale Display` option and choose the `Always` option
![Enable auto-resize in Qemu][21]
That's it from my side.
### Want to use a plug-and-play version of Qemu?
In this tutorial, I went through how you can manually install a VM in Qemu but what if I tell you that you can automate the whole process like allocating storage, RAM, etc?
Yes, it gives the same efficiency but comes in handy when you want to create a new VM ASAP! The tool is called Quickgui and we have a dedicated tutorial for the installation:
I hope you will find this guide helpful. And if you have any queries, feel free to ask in the comments.
--------------------------------------------------------------------------------
via: https://itsfoss.com/qemu-ubuntu/
作者:[Sagar Sharma][a]
选题:[lkxed][b]
译者:[译者ID](https://github.com/译者ID)
校对:[校对者ID](https://github.com/校对者ID)
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
[a]: https://itsfoss.com/author/sagar/
[b]: https://github.com/lkxed/
[1]: https://itsfoss.com/content/images/2023/04/check-where-the-computer-supports-hardware-based-virtualization-or-not.png
[2]: https://linuxhandbook.com/check-cpu-info-linux/?ref=itsfoss.com
[3]: https://itsfoss.com/content/images/2023/04/Check-total-number-of-cores-and-threads-of-system-in-linux.png
[4]: https://learnubuntu.com/add-user-group/?ref=itsfoss.com
[5]: https://itsfoss.com/content/images/2023/04/Start-Qemu-from-the-system-menu-1.png
[6]: https://itsfoss.com/content/images/2023/04/Create-new-VM-in-Qemu.png
[7]: https://itsfoss.com/content/images/2023/04/Choose-the-local-ISO-file-to-create-new-VM-in-Qemu.png
[8]: https://itsfoss.com/content/images/2023/04/Import-the-ISO-file-in-QEMU.png
[9]: https://itsfoss.com/content/images/2023/04/Deal-with-ISO-not-detected-in-Qemu.png
[10]: https://itsfoss.com/content/images/2023/04/Allocate-RAM--cores-and-storage-space-to-VM-in-Qemu.png
[11]: https://itsfoss.com/content/images/2023/04/Name-your-VM-in-Qemu.png
[12]: https://itsfoss.com/content/images/2023/04/open-the-settings-for-VM-in-Qemu.png
[13]: https://itsfoss.com/content/images/2023/04/enable-shared-memory-in-Qemu.png
[14]: https://itsfoss.com/content/images/2023/04/enable-shared-directory-in-Qemu.png
[15]: https://itsfoss.com/content/images/2023/04/use-shared-folders-in-Qemu.png
[16]: https://linuxhandbook.com/beginning-end-file-nano/?ref=itsfoss.com
[17]: https://itsfoss.com/content/images/2023/04/Make-shared-folders-in-Qemu-permenent.png
[18]: https://linuxhandbook.com/nano-save-exit/?ref=itsfoss.com
[19]: https://itsfoss.com/content/images/2023/04/use-shared-folder-in-Qemu.gif
[20]: https://itsfoss.com/content/images/2023/04/Auto-resize-in-Qemu-1.gif
[21]: https://itsfoss.com/content/images/2023/04/Auto-resize-in-Qemu-2.gif