在 Ubuntu 上安装和使用 Qemu
Qemu
![](/data/attachment/album/202305/22/105942liixku1fx7uufgi1.jpg)



> 
> 学习如何在 Ubuntu 中正确安装 Qemu，然后在虚拟机中配置 Linux 发行版。
> 
> 
> 


如果你需要运行 Linux 虚拟机，Qemu 是目前最快的管理程序之一，甚至没有“之一”。


除了速度之外，你还可以获得出色的配置选项。即使你不是一个资深用户，它也给你足够的调整空间。


在本教程中，我将引导你完成：


* 在 Ubuntu 上安装 Qemu 和 KVM
* 在 Qemu 上安装另一个发行版
* 在 Qemu 中启用共享文件夹、自动调整大小和复制粘贴（可选，但会使你的虚拟机体验更顺畅）。


那么让我们从安装开始吧。


### 在 Ubuntu 上安装 Qemu 和 KVM



> 
> ✋ 在开始安装之前，让我们检查一下你的机器是否支持虚拟化。
> 
> 
> 


要做到这一点，请使用以下命令：



```
LC_ALL=C lscpu | grep Virtualization

```

如果你有一个英特尔处理器，它应该带来以下输出：


![检查该计算机是否支持基于硬件的虚拟化](/data/attachment/album/202305/22/110025q7mqi6kl89zwrmy6.png)


如果你有一个 AMD 处理器，它应该得到以下输出：



```
Virtualization:                  AMD-V

```

另外，[确保你的系统有多个处理器核心](https://linuxhandbook.com/check-cpu-info-linux/?ref=itsfoss.com)，这样你可以分配一些给你的虚拟机，并且仍然有足够的处理能力提供给宿主机。


如果你不确定，使用下面的命令，**输出应该大于零**：



```
egrep -c '(vmx|svm)' /proc/cpuinfo

```

![检查 Linux 中系统的核心总数和线程数](/data/attachment/album/202305/22/110025ecux6lmju7za66r0.png)


一旦你都准备好了，你就可以用以下命令在 Ubuntu 上安装 Qemu 和 KVM：



```
sudo apt install qemu qemu-kvm virt-manager bridge-utils

```

安装结束后，**重新启动你的系统**。


#### 将用户添加到适当的组中


为了使 Qemu 工作，你必须 [将你的用户加入两个组](https://learnubuntu.com/add-user-group/?ref=itsfoss.com)：`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](/data/attachment/album/202305/22/110025rbuyqablqnai6baa.png)


接下来，点击 “<ruby> 文件 <rt>  File </rt></ruby>” 菜单，选择 “<ruby> 新建虚拟机 <rt>  New Virtual Machine </rt></ruby>” 选项：


![在 Qemu 中创建新的虚拟机](/data/attachment/album/202305/22/110026u37a7c373703i3c7.png)


从这里，选择第一个选项 “<ruby> 本地安装介质 <rt>  Local install media </rt></ruby>”，这将允许你从文件管理器中选择 ISO：


![选择本地 ISO 文件以在 Qemu 中创建新的虚拟机](/data/attachment/album/202305/22/110026vdglgson2dsx2xdi.png)


接下来，你将被要求添加 ISO 文件。在这里，点击 “<ruby> 浏览 <rt>  Browse </rt></ruby>” 按钮；它将打开一个新的提示窗口，在那里，点击 “<ruby> 浏览本地 <rt>  Browse Local </rt></ruby>”。


它将打开文件管理器，从那里选择 ISO 文件：


![在 Qemu 中导入 ISO 文件](/data/attachment/album/202305/22/110026gq6qfqm8u9anhydl.png)


**在大多数情况下，ISO 文件会被检测到，但如果你发现它没有自动检测到，请按照以下步骤操作：**


* 取消勾选 “<ruby> 自动从安装介质/源检测 <rt>  Automatically detect from the installtion media / source </rt></ruby>” 选项
* 选择 “<ruby> 常见的 Linux <rt>  Generic Linux </rt></ruby>” 选项


![解决 Qemu 中未检测到 ISO 的问题](/data/attachment/album/202305/22/110027xbpp8wehdyopgdhw.png)


接下来，你将被要求根据你的需要分配内存和存储。我建议桌面版至少要有 2 个核心、4GB 内存和 25GB 的存储空间：


![在 Qemu 中为虚拟机分配内存、内核和存储空间](/data/attachment/album/202305/22/110027tlq7707wq757qnyq.png)



> 
> ? 除了磁盘空间，CPU 和内存等系统资源只在 VM 中运行操作系统时使用。
> 
> 
> 


最后，给你的虚拟机起一个合适的名字；完成后，点击 “<ruby> 完成 <rt>  Finish </rt></ruby>” 按钮：


![在 Qemu 中命名你的虚拟机](/data/attachment/album/202305/22/110028fwkkikkvswnrddn0.png)


它将加载 ISO，所以你可以从这里开始安装。


这就完成了。你可能会觉得这已经很不错了，但如果你启用共享文件夹、剪贴板共享等，你会更喜欢它。下面的几节将介绍如何做到这一点。


### 在 Qemu 中启用共享文件夹（可选）


在本节中，我将向你展示如何将宿主机的现有目录与虚拟机共享。


要做到这一点，你必须执行以下步骤：


* 通过 Qemu 中的虚拟机设置添加一个共享文件系统
* 在虚拟机中挂载文件系统


因此，首先从系统菜单中打开虚拟机管理器，选择虚拟机，并点击 “<ruby> 打开 <rt>  Open </rt></ruby>” 按钮来管理控制台：


![打开 Qemu 中虚拟机的设置](/data/attachment/album/202305/22/110028n8bh8d8b7jgzhod8.png)


现在，点击 “<ruby> 信息 <rt>  Info </rt></ruby>” 按钮，选择 “<ruby> 内存 <rt>  Memory </rt></ruby>” 并启用共享内存：


![在 Qemu 中启用共享内存](/data/attachment/album/202305/22/110029jbz3kgwgkgxhfhoh.png)


接下来，点击 “<ruby> 添加硬件 <rt>  Add Hardware </rt></ruby>” 按钮，选择 “<ruby> 文件系统 <rt>  Filesystem </rt></ruby>” 选项。


在这里，你必须做以下工作：


* 在 “<ruby> 源路径 <rt>  Source Path </rt></ruby>” 部分添加一个你想共享的目录的路径
* 在 “<ruby> 目标路径 <rt>  Target Path </rt></ruby>” 部分添加该目录的名称


![在 Qemu 中创建共享文件夹](/data/attachment/album/202305/22/110029fboooea9ghowoerr.png)


完成后，点击 “<ruby> 完成 <rt>  Finish </rt></ruby>” 按钮，启动虚拟机。


在虚拟机中，打开终端，使用下面的语法来挂载共享目录：



```
sudo mount -t virtiofs sharename path/to/shared/directory

```

在我的例子中，它是 `Downloads` 目录，所以我将使用下面的方式：



```
sudo mount -t virtiofs Downloads /home/sagar/Downloads

```

![在 Qemu 中挂载共享文件夹](/data/attachment/album/202305/22/110029erof5y7xzwc55xxj.png)


这就行了。


**但这是一个临时的解决方案。**


要使它成为永久性的，你必须在虚拟机的 `/etc/fstab` 中创建一个条目。


要这样做，首先，用下面的方法打开 `/etc/fstab` 配置文件：



```
sudo nano /etc/fstab

```

按下`Alt + /`，[在 nano 文本编辑器中转到文件的末尾](https://linuxhandbook.com/beginning-end-file-nano/?ref=itsfoss.com) ，并使用以下语法创建一个条目：



```
sharename path/to/shared/directory virtiofs defaults 0 0

```

这是我的配置，看起来像这样：


![使 Qemu 中的共享文件夹永久化](/data/attachment/album/202305/22/110030b2cc367b8h3ws3ha.png)


一旦完成，[保存更改并退出 nano](https://linuxhandbook.com/nano-save-exit/?ref=itsfoss.com) 文本编辑器。


这里我展示了我是如何在主机上的 `Downloads` 目录下创建一个新文件，并且这些变化反映在我的虚拟机上：


![在 Qemu 中使用共享文件夹](/data/attachment/album/202305/22/110030r4x9w7bg7abwfvcc.gif)


因此，现在你可以使用这个共享文件夹在主机和虚拟机之间传输文件，没有任何问题了！


### 在 Qemu 中启用共享剪贴板（可选）


要启用共享剪贴板，你所要做的就是在虚拟机中安装 `spice-vdagent` 工具。


因此，如果你的虚拟机是基于 Ubuntu/Debian 的，你可以使用以下方法：



```
sudo apt install spice-vdagent

```

对于基于 Arch 的发行版：



```
sudo pacman -S spice-vdagent

```

对于基于 Fedora 的发行版：



```
sudo yum install spice-vdagent

```

一旦你完成了安装，**重启你的虚拟机**，剪贴板应该可以如期工作了。


### 在 Qemu 中启用自动调整大小（可选）


自动调整大小的功能没什么，但当你调整虚拟机窗口的大小时，虚拟机的显示会立即适应大小的变化：


![Qemu 中的自动调整大小](/data/attachment/album/202305/22/110030kadoszs96a9lw9w1.gif)


要启用 Qemu 中的自动调整大小功能，你必须遵循 2 个简单的步骤：


* 点击 “<ruby> 视图 <rt>  View </rt></ruby>”（从顶部菜单栏）。
* 选择 “<ruby> 缩放显示 <rt>  Scale Display </rt></ruby>” 并选择 “<ruby> 总是 <rt>  Always </rt></ruby>” 选项。


![](/data/attachment/album/202305/22/110308h81q5sc5f50c4ecf.png)


这就是我这边看到的情况了。


### 想要使用 Qemu 的即插即用版本？


在本教程中，我介绍了如何在 Qemu 中手动安装虚拟机，但如果我告诉你，你可以自动完成整个过程，如分配存储空间、内存等步骤呢？


是的，它提供了同样的效率，但当你想尽快创建一个新的虚拟机时，它就会派上用场！这个工具叫 Quickgui，我们有一个专门的安装教程：



> 
> **[用基于 Qemu 的 Quickgui 轻松地创建虚拟机](https://itsfoss.com/quickgui/)**
> 
> 
> 


我希望你会发现这个指南对你有帮助。如果你有任何疑问，欢迎在评论中提问。


*（题图：MJ/b3c4d5b2-e727-4b70-9bb8-e864941eef9a）*




---


via: <https://itsfoss.com/qemu-ubuntu/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[lkxed](https://github.com/lkxed/) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


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


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

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

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](https://itsfoss.com/content/images/2023/04/check-where-the-computer-supports-hardware-based-virtualization-or-not.png)

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](https://linuxhandbook.com/check-cpu-info-linux/) 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](https://itsfoss.com/content/images/2023/04/Check-total-number-of-cores-and-threads-of-system-in-linux.png)

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](https://learnubuntu.com/add-user-group/): `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

First, open the Virtual Machine Manager from the system menu:

![Start Qemu from the system menu](https://itsfoss.com/content/images/2023/04/Start-Qemu-from-the-system-menu-1.png)

Next, click on the File menu and choose the `New Virtual Machine`

option:

![Create new VM in Qemu](https://itsfoss.com/content/images/2023/04/Create-new-VM-in-Qemu.png)

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](https://itsfoss.com/content/images/2023/04/Choose-the-local-ISO-file-to-create-new-VM-in-Qemu.png)

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](https://itsfoss.com/content/images/2023/04/Import-the-ISO-file-in-QEMU.png)

**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](https://itsfoss.com/content/images/2023/04/Deal-with-ISO-not-detected-in-Qemu.png)

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](https://itsfoss.com/content/images/2023/04/Allocate-RAM--cores-and-storage-space-to-VM-in-Qemu.png)

And finally, give the appropriate name to your VM; once done, click on the finish button:

![Name your VM in Qemu](https://itsfoss.com/content/images/2023/04/Name-your-VM-in-Qemu.png)

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](https://itsfoss.com/content/images/2023/04/open-the-settings-for-VM-in-Qemu.png)

Now, click on the info button, select the `Memory`

and enable the shared memory:

![enable shared memory in Qemu](https://itsfoss.com/content/images/2023/04/enable-shared-memory-in-Qemu.png)

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](https://itsfoss.com/content/images/2023/04/enable-shared-directory-in-Qemu.png)

Once done, click on the Finish button and start the VM. [Create a directory](https://itsfoss.com/make-directories/) in the same location with the same name and location in 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](https://itsfoss.com/content/images/2023/04/use-shared-folders-in-Qemu.png)

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](https://linuxhandbook.com/beginning-end-file-nano/) 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](https://itsfoss.com/content/images/2023/04/Make-shared-folders-in-Qemu-permenent.png)

Once done, [save changes and exit from the nano](https://linuxhandbook.com/nano-save-exit/) 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](https://itsfoss.com/content/images/2023/04/use-shared-folder-in-Qemu.gif)

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](https://itsfoss.com/content/images/2023/04/Auto-resize-in-Qemu-1.gif)

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

![Enabling auto-resize in VM](https://itsfoss.com/content/images/2023/05/Enable_auto-resize_in_Qemu.png)

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:

[Create Virtual Machines Easily With QEMU-based QuickguiQuickgui aims to be a simpler alternative to VirtualBox and help create virtual machines quickly. Let us take a look at it.](https://itsfoss.com/quickgui/)![](https://itsfoss.com/content/images/wordpress/2021/11/Quickgui.png)


![](https://itsfoss.com/content/images/wordpress/2021/11/Quickgui.png)

I hope you will find this guide helpful. And if you have any queries, feel free to ask in the comments.