TranslateProject/sources/tech/20230429.1 ⭐️⭐️ Install and Use Qemu on Ubuntu.md

281 lines
9.7 KiB
Markdown
Raw Normal View History

[#]: 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