在 Linux 的 VirtualBox 中从 USB 驱动器启动
USB VirtualBox
![](/data/attachment/album/202309/22/101826lcduo6etry9ep2rb.jpg)



> 
> 你可以在 VirtualBox 中的虚拟机上使用立付 Linux USB。这样可以省去重新启动主机的麻烦。
> 
> 
> 


你有一个 <ruby> 立付 <rt>  Live </rt></ruby> Linux USB 吗？在当前系统上测试它的常用方法是重新启动，并在系统重新启动时选择从立付 USB 启动。


但这很烦人，因为你需要停止计算机上的工作并重新启动它。


一种干扰较小的方法是使用虚拟机。


是的，你可以使用 VirtualBox 从虚拟机中的 USB 启动。这样，你不必为了尝试而在 VM 中安装发行版，而是改用立付会话。


在本教程中，我将展示在 Linux 系统使用 VirtualBox 从 USB 驱动器启动的步骤。这需要花费一些时间和精力，但可以让你免于重启系统。


### 在 Linux 的 VirtualBox 中从 USB 驱动器启动


由于这是针对高级用户的教程，因此我省略了你事先需要的几个步骤：


* 在 Linux 系统上 [安装 VirtualBox](https://itsfoss.com/install-virtualbox-ubuntu/)
* 创建立付 Linux USB，最好 [使用 Ventoy](https://itsfoss.com/use-ventoy/)


本指南分为三个部分：


* 创建虚拟机磁盘文件
* 在 VM 中使用 USB 启动
* 删除虚拟机磁盘（可选）


那么让我们从第一个开始。


#### 步骤 1：创建虚拟机磁盘文件（VMDK）


首先，你需要识别 USB 驱动器的磁盘名称，为此，你需要 [列出系统的驱动器](https://linuxhandbook.com/linux-list-disks/)。


为此，我将使用 lsblk 命令：



```
lsblk

```

![](/data/attachment/album/202309/22/101904kr5vh5wqwtahavrd.png)



> 
> ? 确保使用不带任何数字的磁盘名称。就我而言，Ventoy 以 sdb1 命名，但我仍然只能使用 sdb。
> 
> 
> 


从上图中，你可以看到，Ventoy 列出了 `sdb11`，但你必须使用不带任何数字的名称。这意味着我必须只使用 `sdb`，否则，它会抛出错误。


找到驱动器名称后，使用以下命令中的 `VBoxManage` 命令来创建：



```
sudo VBoxManage createmedium disk --filename=/path/to/rawdisk.vmdk --variant=RawDisk --format=VMDK --property RawDrive=/dev/sda

```

在上面的命令中，将 `/path/to/rawdisk.vmdk` 替换为要保存文件的路径 ，将 `/dev/sda` 替换为目标驱动器。


就我而言，我想在我的主目录中创建一个名为 `IF.vmdk` 的文件，并且我的目标驱动器是 `/dev/sdb`，然后，我将使用以下命令：



> 
> ? 你需要提供绝对路径来创建 vmdk 文件！
> 
> 
> 



```
sudo VBoxManage createmedium disk --filename=/home/sagar/IF.vmdk --variant=RawDisk --format=VMDK --property RawDrive=/dev/sdb

```

![](/data/attachment/album/202309/22/101904ar55zyyg2yiwwdrw.png)


最后，使用 `chmod` 命令更改权限：



```
sudo chmod 777 Filename.vmdk

```

![](/data/attachment/album/202309/22/101905anqp9tqdbsavqpbp.png)


#### 步骤 2：在 Linux 的 VirtualBox 中从 USB 启动


首先，从系统菜单中打开 VirtualBox，然后单击 “<ruby> 新建 <rt>  New </rt></ruby>” 按钮。


在那里，为你的虚拟机命名并选择操作系统类型及其版本：


![](/data/attachment/album/202309/22/101905b683bmvv8xm8b3z8.png)


现在，单击 “<ruby> 下一步 <rt>  Next </rt></ruby>” 按钮，它会要求你为虚拟机分配硬件资源：


![](/data/attachment/album/202309/22/101906czcobzoqbfvmi9cd.png)


完成硬件资源分配后，单击“下一步”按钮。


在这里，你将找到创建或添加虚拟磁盘的选项。现在，请执行 3 个简单步骤：


* 选择第二个选项 “<ruby> 使用现有虚拟硬盘文件 <rt>  Use an Existing Virtual Hard Disk File </rt></ruby>”。
* 点击“文件”图标。
* 点击“<ruby> 添加 <rt>  Add </rt></ruby>”按钮并选择你最近创建的以 `.vmdk` 结尾的文件。


![](/data/attachment/album/202309/22/101906iv3fjgvf33a8gq0h.png)


选择文件后，它将显示文件的名称，选择它并点击 “<ruby> 选择 <rt>  Choose </rt></ruby>” 选项：


![](/data/attachment/album/202309/22/101907xp94wku9c4y4mwp6.png)


它将向你显示已选择从中启动的文件。单击下一步，它将显示你所做的选择的摘要。


点击 “<ruby> 完成 <rt>  Finish </rt></ruby>” 按钮：


![](/data/attachment/album/202309/22/101907kw0k5obbp823yz88.png)


就是这样！ 虚拟机已创建。


要启动虚拟机，首先选择虚拟机并单击启动按钮：


![](/data/attachment/album/202309/22/101908f3vkon8o9d58755o.png)


由于我的 USB 有 Ventoy，你可以看到，此处列出了多个发行版：


![](/data/attachment/album/202309/22/101908y1ygen1sbf25aeuo.png)


很酷。是么？


#### 步骤 3：使用删除虚拟机及 vmdk 文件（可选）


当我在删除虚拟机后尝试删除 vmdk 文件时，当然，它被删除了，但每当我尝试创建具有相同名称的新文件时，它都会给我一个错误，说该文件已经存在！


因此，在这里，我将引导你了解如何删除虚拟机及 vmdk 文件。


首先，关闭虚拟机并将其删除。


![](/data/attachment/album/202309/22/101909e24905v53g1ztr0m.png)


现在，如果你尝试 [使用 rm 命令](https://linuxhandbook.com/remove-files-directories/) 并提升权限删除 vmdk 文件，你可以删除它（但这就是幻觉的开始）。


例如，在这里，我删除了 `IF.vmdk` 文件：



```
sudo rm IF.vmdk

```

![](/data/attachment/album/202309/22/101909p8udwzdvzd6w40j0.png)


现在，如果我尝试创建一个具有相同名称的新 vmdk 文件，则会出现错误，指出该文件已存在：


![](/data/attachment/album/202309/22/101909k3oz1y5j7b73a39b.png)


要删除 vmdk 文件，首先，你必须使用以下命令取消注册该文件：



```
sudo VBoxManage closemedium disk /path/to/MyDrive.vmdk

```

![](/data/attachment/album/202309/22/101910wqc9hs4afpe9j9tn.png)


完成后，你可以使用 `rm` 命令删除该文件，它将轻松删除：



```
sudo rm Filename.vmdk

```

然后，如果你尝试创建具有相同文件名的 vmdk 文件，你可以这样做：


![](/data/attachment/album/202309/22/101910hhkqu4tq25x5qfho.png)


这就行了！


### 更多虚拟机技巧


如果你正在寻找最快的 VM，可以使用 Qemu + KVM 的组合。我知道这听起来很复杂。


但为了更简单一些，我们制作了一份关于如何在 Ubuntu 上安装和使用 Qemu 的专门指南，包括启用共享文件夹、剪贴板和自动调整大小：



> 
> **[如何在 Ubuntu 上安装和使用 Qemu](https://itsfoss.com/qemu-ubuntu/)**
> 
> 
> 


我希望本指南对你有所帮助。


*（题图：MJ/f74a3b33-2bcf-445b-8868-1dfc420f5b9d）*




---


via: <https://itsfoss.com/virtualbox-boot-from-usb/>


作者：[Sagar Sharma](https://itsfoss.com/author/sagar/) 选题：[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
![Warp Terminal](/assets/images/warp-terminal.webp)


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

Got a live Linux USB? The usual way to test it on your current system is to reboot and choose to boot from the live USB when the system restarts.

But that's annoying as you need to quit working on your computer and restart it.

A less disruptive way is to use a VM.

Yes, you can boot from a USB in a virtual machine with VirtualBox. This way, you don't have to install a distribution in VM just to try it. Use the live session instead.

In this tutorial, I'll show the steps to boot from a USB drive in VirtualBox on Linux systems. It takes some time and effort but saves you from rebooting your system.

## Boot from USB drive in VirtualBox in Linux

Since this is a tutorial for advanced users, I am leaving out a few steps that you need beforehand**:**

- VirtualBox installed on your Linux system
- A live Linux USB, preferably
[Using Ventoy](https://itsfoss.com/use-ventoy/)

[How to Install VirtualBox on Ubuntu Linux [3 Simple Ways]This beginner’s tutorial explains various ways to install VirtualBox on Ubuntu and other Debian-based Linux distributions.](https://itsfoss.com/install-virtualbox-ubuntu/)![](https://itsfoss.com/content/images/2023/08/install-vbox-on-ubuntu.png)


![](https://itsfoss.com/content/images/2023/08/install-vbox-on-ubuntu.png)

This guide is divided into three parts:

- Create a virtual machine disk file
- Boot using USB in VM
- Removing virtual machine disk (optional)

So let's start with the first one.

### Step 1: Create a virtual machine disk file (VMDK)

First, you need to identify the disk name of the USB drive, and to do so, you have to [list the drives of your system](https://linuxhandbook.com/linux-list-disks/).

For that purpose, I'm going to use the lsblk command:

`lsblk `


![list drives in Linux using the lsblk command](https://itsfoss.com/content/images/2023/07/list-drives-in-Linux.png)

From the above image, you can see, Ventoy is listed having the `sdb1`

but you have to use the name without any numbers. This means I have to use the `sdb`

only, or else, it will throw an error.

Once you find your drive name, use the VBoxManage command in the following command to create

`sudo VBoxManage createmedium disk --filename=/path/to/rawdisk.vmdk --variant=RawDisk --format=VMDK --property RawDrive=/dev/sda`


In the above command, replace the path with `/path/to/rawdisk.vmdk`

to where you want to save the file and `/dev/sda`

with your target drive.

In my case, I want to create a file named `IF.vmdk`

in my home directory and my target drive is `/dev/sdb`

, then, I will be using the following:

`sudo VBoxManage createmedium disk --filename=/home/sagar/IF.vmdk --variant=RawDisk --format=VMDK --property RawDrive=/dev/sdb`


![create virtual machine disk drive for virtualbox to boot from USB drive in Linux](https://itsfoss.com/content/images/2023/07/create-virtual-machine-disk-drive-for-virtualbox-to-boot-from-USB-drive-in-Linux.png)

And finally, change the permissions using the chmod command:

`sudo chmod 777 Filename.vmdk`


![use chmod command to change the permissions](https://itsfoss.com/content/images/2023/07/use-chmod-command-to-change-the-permissions.png)

### Step 2: Boot from USB in VirtualBox in Linux

First, open the VirtualBox from your system menu and click on the `New`

button.

There, name your VM and select the operating system type and its version:

![Create VM in VirtualBox to boot from USB in Linux](https://itsfoss.com/content/images/2023/07/Create-VM-in-VirtualBox-to-boot-from-USB-in-Linux.png)

Now, click on the `Next`

button and it will ask you to allocate hardware resources for your VM:

![allocate RAM and cores to Vm to boot from USB in VirtualBox in Linux](https://itsfoss.com/content/images/2023/07/allocate-RAM-and-cores-to-Vm-to-boot-from-USB-in-VirtualBox-in-Linux.png)

Once you are done allocating the hardware resources, click on the `Next`

button.

Here, you will find an option to create or add a virtual disk. Now, follow 3 simple steps:

- Select the 2nd option saying
`Use an Existing Virtual Hard Disk File`

. - Click on the
`File`

icon. - Hit the
`Add`

button and select the file ending the`.vmdk`

that you created recently.

![](https://itsfoss.com/content/images/2023/07/add-virtual-machine-disk-drive-in-VirtualBox-to-boot-from-USB-in-Linux.png)

Once you select the file, it will show you the name of the file, select it and hit the `Choose`

option:

![select the vmdk file](https://itsfoss.com/content/images/2023/07/select-the-vmdk-file.png)

It will show you the file that has been selected to boot from. Click on the next and it will show the summery of choices you've made.

Hit the `Finish`

button:

![Finish the VM creation to boot from USB in VirtualBox in Linux](https://itsfoss.com/content/images/2023/07/Finish-the-VM-creation-to-boot-from-USB-in-VirtualBox-in-Linux.png)

That's it! The VM has been created.

To start the VM, first, select the VM and hit the start button:

![start the VM](https://itsfoss.com/content/images/2023/07/start-the-VM.png)

As my USB had Ventoy, you can see, multiple distros listed here:

![Create Ventoy USB and boot from Virtual machine in Linux](https://itsfoss.com/content/images/2023/07/Boot-from-USB-in-VirtualBox-in-Linux.png)

Pretty cool. Right?

### Step 3: Remove VM with vmdk file (optional)

When I tried removing the vmdk file after deleting the VM, sure, it was getting deleted but whenever I tried creating a new file having the same name, it gave me an error saying the file already exists!

So here, I will walk you through how you can remove the VM with the vmdk file.

First, turn off the VM and remove it

![Remove VM from VirtualBox](https://itsfoss.com/content/images/2023/07/Remove-VM-from-VirtualBox.png)

Now, if you try to remove the vmdk file [using the rm command](https://linuxhandbook.com/remove-files-directories/) and elevated privileges, you can remove it (but there's where the illusion starts).

For example, here, I removed the IF.vmdk file:

`sudo rm IF.vmdk`


![use rm command to remove vmdk file](https://itsfoss.com/content/images/2023/07/use-rm-command-to-remove-vmdk-file.png)

And now, if I try to create a new vmdk file with the same name, it gives me an error saying the file already exists:

![unable to create vmdk file in Linux, file already exist](https://itsfoss.com/content/images/2023/07/unable-to-create-vmdk-file-in-Linux--file-already-exist.png)

To remove the vmdk file, first, you have to unregister the file using the following:

`sudo VBoxManage closemedium disk /path/to/MyDrive.vmdk`


![unregister vmdk file in Linux to remove it](https://itsfoss.com/content/images/2023/07/unregister-vmdk-file-in-Linux-to-remove-it.png)

Once done, you can remove the file using the rm command and it will be removed easily:

`sudo rm Filename.vmdk`


And then, if you try to create a vmdk file with the same filename, you can do that:

![how to remove the vmdk file in Linux](https://itsfoss.com/content/images/2023/07/how-to-remove-the-vmdk-file-in-Linux.png)

There you have it!

## More VM tips

If you are looking for the fastest VM, you can use the combination of Qemu + KVM. I know it sounds complex.

But to make things easy, we made a dedicated guide on how to install and use Qemu on Ubuntu including enabling shared folders, clipboard, and auto-resizing:

[How to Install and Use Qemu on UbuntuLearn how to properly install Qemu in Ubuntu and then configure a Linux distro in VM.](https://itsfoss.com/qemu-ubuntu/)![](https://itsfoss.com/content/images/2023/04/install-qemu-on-ubuntu.png)


![](https://itsfoss.com/content/images/2023/04/install-qemu-on-ubuntu.png)

I hope you will find this guide helpful.