如何使用 virt-manager 运行虚拟机
libvirt
![](/data/attachment/album/201907/27/064028u9yjhvx44wy1ix66.jpg)


在早些年，在同一台笔记本中运行多个操作系统只能双启动。当时，这些操作系统很难同时运行或彼此交互。许多年过去了，在普通的 PC 上，可以通过虚拟化在一个系统中运行另一个系统。


最近的 PC 或笔记本（包括价格适中的笔记本电脑）都有硬件虚拟化，可以运行性能接近物理主机的虚拟机。


虚拟化因此变得常见，它可以用来测试操作系统、学习新技术、创建自己的家庭云、创建自己的测试环境等等。本文将指导你使用 Fedora 上的 Virt Manager 来设置虚拟机。


### 介绍 QEMU/KVM 和 Libvirt


与所有其他 Linux 系统一样，Fedora 附带了虚拟化扩展支持。它由作为内核模块之一的 KVM（基于内核的虚拟机）提供支持。


QEMU 是一个完整的系统仿真器，它可与 KVM 协同工作，允许你使用硬件和外部设备创建虚拟机。


最后，[libvirt](https://libvirt.org/) 能让你管理基础设施的 API 层，即创建和运行虚拟机。


这三个技术都是开源的，我们将在 Fedora Workstation 上安装它们。


### 安装


#### 步骤 1：安装软件包


安装是一个相当简单的操作。 Fedora 仓库提供了 “virtualization” 软件包组，其中包含了你需要的所有包。



```
sudo dnf install @virtualization
```

#### 步骤 2：编辑 libvirtd 配置


默认情况下，系统管理仅限于 root 用户，如果要启用常规用户，那么必须按以下步骤操作。


打开 `/etc/libvirt/libvirtd.conf` 进行编辑：



```
sudo vi /etc/libvirt/libvirtd.conf
```

将 UNIX 域套接字组所有者设置为 libvirt：



```
unix_sock_group = "libvirt"
```

调整 UNIX 域套接字的读写权限：



```
unix_sock_rw_perms = "0770"
```

#### 步骤 3：启动并启用 libvirtd 服务



```
sudo systemctl start libvirtd
sudo systemctl enable libvirtd
```

#### 步骤 4：将用户添加到组


为了管理 libvirt 与普通用户，你必须将用户添加到 `libvirt` 组，否则每次启动 `virt-manager` 时，都会要求你输入 sudo 密码。



```
sudo usermod -a -G libvirt $(whoami)
```

这会将当前用户添加到组中。你必须注销并重新登录才能应用更改。


### 开始使用 virt-manager


可以通过命令行 （`virsh`） 或通过 `virt-manager` 图形界面管理 libvirt 系统。如果你想做虚拟机自动化配置，那么命令行非常有用，例如使用 [Ansible](https://fedoramagazine.org/get-the-latest-ansible-2-8-in-fedora/)，但在本文中我们将专注于用户友好的图形界面。


`virt-manager` 界面很简单。主窗口显示连接列表，其中包括本地系统连接。


连接设置包括虚拟网络和存储定义。你可以定义多个虚拟网络，这些网络可用于在客户端系统之间以及客户端系统和主机之间进行通信。


### 创建你的第一个虚拟机


要开始创建新虚拟机，请按下主窗口左上角的按钮：


![](/data/attachment/album/201907/27/064029q8v1158bn1pz2bpb.png)


向导的第一步需要选择安装模式。你可以选择本地安装介质、网络引导/安装或导入现有虚拟磁盘：


![](/data/attachment/album/201907/27/064029ry51fc5vc50fqqz5.png)


选择本地安装介质，下一步将需要选择 ISO 镜像路径：


![ ](/data/attachment/album/201907/27/064030mp0i1q149p95y0v0.png)


随后的两个步骤能让你调整新虚拟机的 CPU、内存和磁盘大小。最后一步将要求你选择网络选项：如果你希望虚拟机通过 NAT 与外部隔离，请选择默认网络。如果你希望从外部访问虚拟机，那么选择桥接。请注意，如果选择桥接，那么虚拟机则无法与主机通信。


如果要在启动设置之前查看或更改配置，请选中“安装前自定义配置”：


![](/data/attachment/album/201907/27/064030l38gfkv98skxyuf9.png)


虚拟机配置窗口能让你查看和修改硬件配置。你可以添加磁盘、网络接口、更改引导选项等。满意后按“开始安装”：


![](/data/attachment/album/201907/27/064031vdw3pjjpyj3pp7yf.png)


此时，你将被重定向到控制台来继续安装操作系统。操作完成后，你可以从控制台访问虚拟机：


![](/data/attachment/album/201907/27/064031d3kmlwwaawlnqes9.png)


刚刚创建的虚拟机将出现在主窗口的列表中，你还能看到 CPU 和内存占用率的图表：


![](/data/attachment/album/201907/27/064031fttotnss74pdo12n.png)


libvirt 和 `virt-manager` 是功能强大的工具，它们可以以企业级管理为你的虚拟机提供出色的自定义。如果你需要更简单的东西，请注意 Fedora Workstation [预安装的 GNOME Boxes 已经能够满足基础的虚拟化要求](https://fedoramagazine.org/getting-started-with-virtualization-in-gnome-boxes/)。




---


via: <https://fedoramagazine.org/full-virtualization-system-on-fedora-workstation-30/>


作者：[Marco Sarti](https://fedoramagazine.org/author/msarti/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


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


OK
In the beginning there was dual boot, it was the only way to have more than one operating system on the same laptop. At the time, it was difficult for these operating systems to be run simultaneously or interact with each other. Many years passed before it was possible, on common PCs, to run an operating system inside another through virtualization.

Recent PCs or laptops, including moderately-priced ones, have the hardware features to run virtual machines with performance close to the physical host machine.

Virtualization has therefore become normal, to test operating systems, as a playground for learning new techniques, to create your own home cloud, to create your own test environment and much more. This article walks you through using Virt Manager on Fedora to setup virtual machines.

## Introducing QEMU/KVM and Libvirt

Fedora, like all other Linux systems, comes with native support for virtualization extensions. This support is given by KVM (Kernel based Virtual Machine) currently available as a kernel module.

QEMU is a complete system emulator that works together with KVM and allows you to create virtual machines with hardware and peripherals.

Finally [libvirt](https://libvirt.org/) is the API layer that allows you to administer the infrastructure, ie create and run virtual machines.

The set of these three technologies, all open source, is what we’re going to install on our Fedora Workstation.

## Installation

### Step 1: install packages

Installation is a fairly simple operation. The Fedora repository provides the “virtualization” package group that contains everything you need.

sudo dnf install @virtualization

### Step 2: edit the libvirtd configuration

By default the system administration is limited to the root user, if you want to enable a regular user you have to proceed as follows.

Open the /etc/libvirt/libvirtd.conf file for editing

sudo vi /etc/libvirt/libvirtd.conf

Set the domain socket group ownership to libvirt

unix_sock_group = "libvirt"

Adjust the UNIX socket permissions for the R/W socket

unix_sock_rw_perms = "0770"

### Step 3: start and enable the libvirtd service

sudo systemctl start libvirtd

sudo systemctl enable libvirtd

### Step 4: add user to group

In order to administer libvirt with the regular user you must add the user to the libvirt group, otherwise every time you start virtual-manager you will be asked for the password for sudo.

sudo usermod -a -G libvirt $(whoami)

This adds the current user to the group. You must log out and log in to apply the changes.

## Getting started with virt-manager

The libvirt system can be managed either from the command line (virsh) or via the virt-manager graphical interface. The command line can be very useful if you want to do automated provisioning of virtual machines, for example with [Ansible](https://fedoramagazine.org/get-the-latest-ansible-2-8-in-fedora/), but in this article we will concentrate on the user-friendly graphical interface.

The virt-manager interface is simple. The main form shows the list of connections including the local system connection.

The connection settings include virtual networks and storage definition. it is possible to define multiple virtual networks and these networks can be used to communicate between guest systems and between the guest systems and the host.

## Creating your first virtual machine

To start creating a new virtual machine, press the button at the top left of the main form:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-09-41-45.png)

The first step of the wizard requires the installation mode. You can choose between a local installation media, network boot / installation or an existing virtual disk import:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-09-30-53.png)

Choosing the local installation media the next step will require the ISO image path:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-10-42-39.png)

The subsequent two steps will allow you to size the CPU, memory and disk of the new virtual machine. The last step will ask you to choose network preferences: choose the default network if you want the virtual machine to be separated from the outside world by a NAT, or bridged if you want it to be reachable from the outside. Note that if you choose bridged the virtual machine cannot communicate with the host machine.

Check “Customize configuration before install” if you want to review or change the configuration before starting the setup:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-10-43-21.png)

The virtual machine configuration form allows you to review and modify the hardware configuration. You can add disks, network interfaces, change boot options and so on. Press “Begin installation” when satisfied:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-10-44-58.png)

At this point you will be redirected to the console where to proceed with the installation of the operating system. Once the operation is complete, you will have the working virtual machine that you can access from the console:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-10-55-35.png)

The virtual machine just created will appear in the list of the main form, where you will also have a graph of the CPU and memory occupation:

![](https://fedoramagazine.org/wp-content/uploads/2019/07/Screenshot-from-2019-07-14-11-09-22.png)

libvirt and virt-manager is a powerful tool that allows great customization to your virtual machines with enterprise level management. If something even simpler is desired, note that Fedora Workstation comes with [GNOME Boxes pre-installed and can be sufficient for basic virtualization needs](https://fedoramagazine.org/getting-started-with-virtualization-in-gnome-boxes/).

## Jakfrost

Since late 1990’s into the early 2000’s, I have used virtualization on many PC’s in Industrial Control applications. For the Systems Integrator/Solution Provider of Industrial Control applications programming, it has been a must have. This is due in large part to the fact that different OEM vendors of the control equipment (PLC’s, HMI’s etc…) didn’t play well together, and it was a constant, sometimes very painful, process of reconfiguring your system to accomidate that. VM’s wiped that out overnight.

## Rubem Mota

Hi,

I have a question, which is the best pratice to use another directory to store de ISO’s and the VM’s files, like an external drive, instead the main disk.

## Marco Sarti

In the connection details, storage, you can add a large number of storage pool type. If you have an external drive you can mount it on a directory and add this directory as storage pool.

## Shaun Assam

Nice article Marco. Virt-Manager has been my go-to for KVM VM management for many years due to it’s incredible detail in the settings. One thing I’d like to point out, in the article it is stated, “Note that if you choose bridged the virtual machine cannot communicate with the host machine.” In my experience I’ve been able to communicate with the host machine after configuring bridge mode. My guest VM would get the IP address from the router instead of the default NAT. I used the instructions from Lief Madsen’s site to do it. Even though it was published almost 3 years ago it still works today. Maybe using these instructions allows communication to the host.

## Marco Sarti

Great suggestion! Thanks.

## John L. Ries

Curiously enough, IBM’s OS/2 employed virtualization to run its DOS and Windows subsystems back in the 1990s.

## Marco Sarti

I recently found my old OS/2 Warp CD and reinstalled it in a virtual machine, fully functional 🙂

## Walter van der Schee

What OS/2 called virtualization, was the i386 ‘virtual 8086’ mode, which allowed the Intel 80386 to run up to 8 fully protected 8086 mode PC VM’s. If the DOS mode needed no more than 16-bit addressing, this worked nicely and quick (for a i386 machine…). DOS extenders or Windows enhanched mode did not run inside those VMs, since it was 16-bit 8086 mode nothing more.

## Jean-Francois Messier

Very interesting article. I knew there was a way to manage VMs without VMware or VirtualBox. Did you make an article about the “Debian” side of things (I use Ubuntu servers) ?

Thanks 🙂

## Marco Sarti

Debian has packages in its repository, https://wiki.debian.org/KVM I’m pretty sure also Ubuntu has.

## Ron

Is this a viable alternative for recent-ish Windows-targeted games? Or am I stuck with a native installation?

## Marco Sarti

I think at least there is a display performance problem. Spice console has pretty good performances but not enough for gaming.

## Shaun Assam

Ho Ron, have you checked out the virtio option for graphics instead of QXL. According to https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/Virtualization_Deployment_and_Administration_Guide/sect-KVM_Para_virtualized_virtio_Drivers-Using_KVM_virtio_drivers_for_GPU_devices.html. this enhances GPU 3d acceleration performance. This may work with some — or maybe all — your games. You can find the virtio drivers and installation guide at https://docs.fedoraproject.org/en-US/quick-docs/creating-windows-virtual-machines-using-virtio-drivers/index.html

## rr4r

i need example how run raspbery pi zero w in qemu

and fedora

## Marco Sarti

If you install the qemu-system-arm package you can choose the arm architecture at the first step of VM creation. This article helps you https://fedoraproject.org/wiki/Architectures/ARM/HowToQemu

## Guus Bonnema

Does this open up an security hole if someone gains access to the normal user? Isn’t the reason for sudo the wish to isolate systemstuff from the regular user should someone enter the user’s id?

## Marco Sarti

Consider that virt-manger can be used (and it is used) to administer libvirt remotely. In this case, using a regular user instead of root is a security enhancement.

## Guus Bonnema

I am confused. Why would administering libvirt remotely through a regular user be more secure than root? Isn’t the root user harder to come by? Or does this have to do with usually disallowing root to log on remotely?

P.S. I am a longtime fedora user, but I guess there is a lot about security that I do not know.

## gotmunchies

How is the performance. I had tried this setup a couple years ago and found the performance to be horrible to the point of being unusable. So I have resorted to virtualbox. If there has been vast performance improvements to the virt-manager system of virtualization then I may give it a try again.

## Marco Sarti

The computational performances are close to those of the host machine, this because the CPU virtualization is not emulated but managed directly by the hardware (if it supports). This applies to all hypervisors including VirtualBox. Of course processes that benefit from other low-level computational resources, such as GPUs, cannot have comparable performance.

## Marco Sarti

Also consider that KVM is widely used in cloud implementations, for example OpenStack.

## Ravindu Meegasmulla

This is actually brilliant compare to Virtual Box and other variants. I had bit of trouble when installing a windows machine using VirtIO. Here is a guide i found if any one needed.

https://dennisnotes.com/note/20180614-ubuntu-18.04-qemu-setup/

## Stan Sykes

Superb article, many thanks for this. I’m also a long time user of Fedora and Virtualbox. Now I feel more confident to run virtual machine with virt-manager so I can release Virtualbox to regain space on my SSD … 🙂

## Justin W. Flory

I was surprised virt-manager was mentioned. I noticed in the RHEL 8.0 release notes, virt-manager is deprecated in favor of Cockpit. Will virt-manager continue to receive active development attention or should Fedora users look more closely at using Cockpit to manage libvirt VMs?

## Marco Sarti

Currently the management of virtual machines in Cockipt has limited functionality compared to virt-manager. This is the current version of Fedora.

## Jamison Lofthouse

Still waiting for the ability to create virtual networks on the libvirt user session. Then I wouldn’t have to have permissions to the system instance and wouldn’t have to reconfigure the storage path so that the images aren’t stored on /var.