VirtualBox是目前最好的(而且是免费的!)虚拟机(virtual machine)应用程序之一,但如果您的虚拟计算机可以与主机更紧密地集成,它会更加有用。
虽然您可以很容易地将主机和来宾计算机联网在一起,但大多数人可能只是想要一种简单的方法来在VirtualBox中的主机和来宾操作系统之间共享文件夹。好消息是这很容易做到!
清理术语(Clearing Up The Terminology)
重要的是我们要快速弄清楚本文将使用的几个关键术语:
- 虚拟机是在另一台计算机上运行的模拟计算机。
- “主机”计算机是您面前的物理计算机。
- “来宾”计算机是通过VirtualBox(VirtualBox)运行的虚拟机。
- “裸机(Bare)”是指直接在物理计算机上运行。
如果您曾经看过电影The Matrix,那么您可以将虚拟机视为生活在模拟世界中的计算机。它认为它是一台真正的计算机,但它实际上只是软件。
操作系统兼容性(Operating System Compatibility)
为了使用VirtualBox内置的共享文件夹功能,您需要在来宾计算机上安装一个名为“ Guest Additions ”的东西。(Additions)在撰写本文时,还有以下内容:
对于大多数人来说,来宾机器可能会运行Windows或Linux。如果您正在运行更奇特的东西(或尝试运行 macOS),那么此方法将不起作用。
安装访客添加(Installing Guest Additions)
在使共享文件夹工作之前,您需要在相关虚拟机上安装来宾添加(guest additions )。这是通过将虚拟 CD 放入虚拟机的虚拟 CD 驱动器来实现的。我们在这里使用的是Ubuntu Linux,但VirtualBox会自动检测哪些添加项适合来宾(Guest)操作系统。
您所要做的就是运行您的虚拟机,然后单击设备( Devices ),然后单击插入访客添加 CD 映像...( Insert Guest Additions CD Image…)
在我们的例子中,“CD”会自动播放,并且添加的安装没有问题。我们重新启动了我们的虚拟机以防万一,但这并不是绝对需要的。安装来宾添加后,您就可以将共享文件夹添加到虚拟机了。
在主机上创建共享文件夹(Creating a Shared Folder On The Host Machine)
VirtualBox将主机上的共享文件夹作为共享网络驱动器或作为文件系统的特殊扩展名提供给来宾计算机的操作系统。因此,您需要在裸机计算机上指定该文件夹以供其使用。
您可以为每个虚拟机来宾分配不同的共享文件夹。它可以是已经存在的文件夹,也可以是您专门为此目的创建的文件夹。无论哪种情况,一旦您决定要与虚拟机共享哪个文件夹,我们就必须选择并安装它。
出于演示目的,我们创建了一个包含测试文本文件的文件夹。
在来宾计算机上启用文件共享(Enable File Sharing On The Guest Machine)
您可以将共享文件夹添加到运行或停止的虚拟机中。我们将在Ubuntu机器运行的情况下挂载共享文件夹。
- 单击设备(Devices)菜单,然后单击Shared Folders>Shared Folder Settings。
- 在此设置菜单中,单击蓝色图标以添加新的共享文件夹。
- 选择文件夹路径下拉菜单并选择其他(other). 选择您要共享的文件夹,然后单击选择文件夹(Select Folder)。
- 选择自动挂载(auto-mount ),然后单击确定。( OK. )然后再次单击确定( OK )。
共享文件夹出现在我们的Ubuntu桌面上,但您可能需要在操作系统的网络驱动器列表或文件系统资源管理器中查找它。在Ubuntu中,我们必须在打开文件夹时输入管理员密码。
要删除共享文件夹,只需返回您最初添加它的同一窗口并将其从列表中删除。虽然文件夹是共享的,但您基本上可以将其用作任何其他已安装的磁盘或驱动器。
在主机和访客之间共享文件夹的替代方法(Alternative Ways To Share Folders Between Host & Guest)
使用在主机和来宾之间共享文件的官方方法并不是在裸机计算机和虚拟机之间移动文件的唯一方法。
传输文件的一种简单方法是使用USB闪存驱动器。VirtualBox允许您将USB设备的控制权交给虚拟机。只需(Simply)将要传输的所有文件复制到闪存驱动器,然后使用VirtualBox菜单将控制权切换到虚拟机。
由于您的虚拟机也可以访问 Internet,因此您可以在两个系统上安装云存储包,例如Dropbox,假设有一个可用于来宾操作系统的版本。尽管这确实意味着文件将通过您的互联网连接同步,如果您有一个,这可能会很慢并且会占用您的数据上限。
如果您只想复制适合剪贴板的内容(例如密码),那么您最好在VirtualBox中启用共享剪贴板,这也是来宾添加的一部分。
几乎完美的文件共享?(Virtually Flawless File Sharing?)
虚拟机是一项令人难以置信的技术壮举,但就优雅而言,它仍然是相当于胶带和纸板的计算机。最终,在主机和来宾系统之间共享文件的任何解决方案都将是某种程度的杂物。
话虽如此,我们让它在我们的Windows(Windows)主机和Ubuntu来宾之间运行没有任何问题。由于主机和来宾(Plenty)操作系统类型之间可能存在很大差异,许多用户会遇到一些困难,但幸运的是,VirtualBox 社区(VirtualBox community)已经看到了这一切。
因此,如果您确实遇到无法解决的问题,我们建议您先搜索论坛,然后在没有答案的情况下发布问题。但是,大多数遵循上述说明的人都会好起来的。
Share Folders between Host and Guest OS in VirtualBox
VirtualBox is one of the best (and free!) virtual machine applications out there, but it’s even more useful if your virtual computer can integrate more tightly with the host computer.
While you can network the host and guest computer together pretty easily, most people probably just want a simple way to share folders between the host and guest OS in VirtualBox. The good news is that this is quite easy to do!
Clearing Up The Terminology
It’s important that we quickly clear up a few key terms that will be used in this article:
- A virtual machine is a simulated computer that runs on another computer.
- The “host” computer is the physical computer in front of you.
- The “guest” computer is a virtual machine that runs thanks to VirtualBox.
- “Bare metal” means to run directly on the physical computer.
If you’ve ever seen the movie The Matrix then you can think of the virtual machine as a computer living in a simulated world. It thinks it’s a real computer, but it’s actually just software.
Operating System Compatibility
In order to use the shared folder function built into VirtualBox, you need to install something called “Guest Additions” on the guest computer. At the time of writing there are additions for:
For most people the guest machine will probably run Windows or Linux. If you’re running something more exotic (or trying to run macOS) then this method won’t work.
Installing Guest Additions
Before you can make shared folders work, you need to install guest additions on the virtual machine in question. This works by putting a virtual CD into the virtual machines imaginary CD drive. We’re using Ubuntu Linux here, but VirtualBox will automatically detect which additions are right for the Guest operating system.
All you have to do is run your virtual machine and then click Devices and then Insert Guest Additions CD Image…
In our case the “CD” auto-played and the additions installed without issue. We restarted our virtual machine just in case, but it isn’t strictly needed. With guest additions installed, you’re ready to add a shared folder to your virtual machine.
Creating a Shared Folder On The Host Machine
VirtualBox presents a shared folder on the host computer as a shared network drive or as a special extension of the file system to the guest computer’s operating system. So you’ll want to designate that folder on the bare metal computer for it to use.
You can assign different shared folders to each virtual machine guest. It can be a folder that already exists or it can be one you’ve created especially for this purpose. In either case, once you’ve decided which folder you want to share with the virtual machine, we have to select and mount it.
We’ve created a folder with a test text file inside for demonstration purposes.
Enable File Sharing On The Guest Machine
You can add the shared folder to your virtual machine with it running or stopped. We’re going to mount the shared folder with our Ubuntu machine running.
- Click on the Devices menu and then Shared Folders>Shared Folder Settings.
- In this settings menu, click the blue icon to add a new shared folder.
- Select the folder path dropdown and choose other. Choose the folder you want to share and click Select Folder.
- Select auto-mount and then click OK. Then click OK again.
The shared folder appeared on our Ubuntu desktop, but you may have to look for it in your operating system’s network drive list or in the file system explorer. In Ubuntu we had to enter the administrator password when opening the folder.
To remove the shared folder, simply go back to the same window you added it in the first place and remove it from the list. While the folder is shared, you can basically use it as any other mounted disk or drive.
Alternative Ways To Share Folders Between Host & Guest
Using the official method of sharing files between host and guest isn’t the only way you can move files between your bare metal computer and the virtual machine.
One easy way to transfer files is by using a USB flash drive. VirtualBox lets you give control of a USB device to the virtual machine. Simply copy all of the files you want to transfer to a flash drive and then switch control to the virtual machine using the VirtualBox menu.
Since your virtual machine also has internet access, you can install a cloud storage package such as Dropbox on both systems, assuming there’s a version available for the guest operating system. Although this does mean that files will sync over your internet connection, which could be slow and eat your data cap, if you have one.
If all you want to do is copy something that will fit in the clipboard (such as a password) then you’re better off enabling the shared clipboard in VirtualBox, which is also a part of guest additions.
Virtually Flawless File Sharing?
Virtual machines are an incredible feat of technology, but it’s still the computer equivalent of duct-tape and cardboard as far as elegance goes. Ultimately any solution to share files between a host and guest system will be some degree of kludge.
That being said, we didn’t have any trouble getting it to work between our Windows host and Ubuntu guest. Plenty of users are going to run into some rough spots thanks to how much variation there can be between host and guest operating system types, but luckily the VirtualBox community has seen it all.
So if you do run into an issue you can’t resolve, we recommend first giving the forums a search and then posting a question if the answer isn’t already there. However, most people who follow the instructions above are going to be just fine.