The Linux Page

Copy files between a guest (virtualbox) and the host

Brdige between your Host and Guest computers

I use VirtualBox and had a problem with copying files between my host (main computer on which you boot your computer) and the guests (virtual computer that you run on your host).

The fact is that it is all explained right here:

https://www.virtualbox.org/manual/ch04.html

There are a few steps as follow.

Using scp

Here, I have an example of GParted where I could not install the VirtualBox tools as mentioned below. This is mainly a matter if setting up an SSH server and allowing connections for a given user.

Install the VirtualBox Tools

Install the Virtual Box Tools (called Guest Additions) on your guest; these tools allow your guest to communicate with the host to allow files to be written in a directory on the host.

To install those, it should be as easy as using the Install Guest Additions... menu option (under Devices). That auto-mounts an .iso and autoexec's whatever needs to be done. However, it does not always work right. For example, if you already have a CDROM in your virtual machine, it won't be able to load another CD in there. Might have to reboot to remove the existing CD and get the additions CD inserted with that menu option.

Also, the autorun.sh script may fail. In that case use df to find your CD path and look into it. Just try to run the autorun.sh by hand or whatever installation command with whatever package there is on the CD. For Solaris I ran the following:

pkgadd /media/VBOXADDITIONS-<version>/VBoxSolarisAdditions.pkg

Missing Menus

It is possible that the top bar does not include any menu in your VirtualBox guest window. The missing menu is because the window is scaled. The scaling can be canceled using:

Right-Ctrl-C

The default Host commands are triggered by Right Ctrl. If you changed that key, make sure to use the key you changed it to. The command is a switch. If you hit it again, it again allows you to scale your window.

From the Command Line

Installing the additional features has not been working right with the menu. It seems that the menu option assumes that you are running X11 when I generally don't want to install the whole X11 for a tiny super simple server. So this requires you to do the installation manually.

In Ubuntu 14.04, I ended up having to install the additions from the Ubuntu servers with apt-get:

sudo apt-get install virtualbox-guest-additions-iso

This installs the ISO that you can then mount with:

sudo mount /usr/share/virtualbox/VBoxGuestAdditions.iso /media/cdrom

Before running the installation script, you may need to install some kernel tools (you'd think that the script can do it, and maybe it does, but that won't hurt much):

sudo apt-get install build-essential module-assistant

Now you can install the actual additions by running one of the scripts. Which one is mainly determine by the operating system you're running. For Linux:

sudo sh /media/cdrom/VBoxLinuxAdditions.run

That will take care of all the necessary installations so you can create shared folders.

Create Shared Folder

Create a shared folder (or several...) where you place your data that can be transferred both ways (unless you make the shared folder read only or give it an owner / group such that you cannot write to the directory!?)

To do that, use the menu entry "Shared Folders ..." from the Devices menu. There you should be able to create the shared folders without too much trouble. That should also work when you tweak the settings of your guest.

Mounting the Shared Folder

Once you have these two things, you are ready to mount the partition. The mount instruction changes depending on the guest you are running. Note that this is specific to the guest, not the other way around. That is, you can mount the host directory on the guest. Got it? If your main computer runs Linux and your guest is Windows, then you add a drive with:

net use x: \\vboxsvr\sharename

from your Windows virtual computer.

Similarly, from your Guest Linux, use mount as in:

mount -t vboxsf [-o OPTIONS] sharename mountpoint

The Solaris mount command is the same, only it uses -F instead of -t:

mount -F vboxsf [-o OPTIONS] sharename mountpoint

Note that all those features, installing the the Virtual Box Additions, creating a shared folder, mounting, are availabe via the command line. I just did not try those but if you need to script all of that, it's possible.

Mount not working...

At least in version 14.04 of Ubuntu, the guest additions place the mount.vboxsf in a new place. This causes problems when trying to mount a shared folder. You will have to fix the soft link found in /usr/lib to the new location:

sudo rm /usr/lib/VBoxGuestAdditions
sudo ln -s /opt/VBoxGuestAdditions-4.3.10/lib/VBoxGuestAdditions /usr/lib/VBoxGuestAdditions

The mount may not be the old thing that has problems, but that's what many people bump into.

Common error when the mount fails:

mount: wrong fs type, bad option, bad superblock on /mnt/path,
missing codepage or helper program, or other error
In some cases useful info is found in syslog - try
dmesg | tail or so

Source: https://forums.virtualbox.org/viewtopic.php?f=3&t=60921

Copying Files

Assuming you create a directory on your host (main computer) named /mnt/solaris and you mounted that as /mnt/host on your Solaris guest, you can copy files with:

# On the host
cp ~/my-file.txt /mnt/solaris

# On the guest
cp ~/this-other-file.txt /mnt/host

Here I show how to copy from the host to the guest, and from the guest to the host. The copy works both ways as long as you create a read/write shared folder.

Note that by default a directory such as /mnt/solaris is likely going to be read-only for most users. You probably will want to use a directory under your home directory or change the owner/group of the /nmt/solaris directory.

The /mnt/host on the guest system has no bearing on the permission to read or write files. It is just a mount point.

Ultra Confused While Running

While running, a shared folder name cannot really be changed. You may create it, then mount it, and then it is pretty much like a read-only information.

I tried to even delete all the shared folders, re-creating a new one, and then rebooting, the wrong path is still used!

To fix such problems, turn off your virtual box, enter the settings, click on Shared Folder (last option at this time) and fix your shared folders there. Once done, reboot your virtual box and all should work like a charm.

Positive Potential Side Effects

The side effects of installing the Guest Additions that come with VirtualBox, outside of the ability to copy files, is to allow various capabilities such as copy/paste between the host and guest. This is not always something you might want, but I find that quiet useful. (That being said, it only works if you install X-Windows on your VPS, most of my VPS have no GUI at all).

You may want to read more about the Guest Additions on the VirtualBox website for additional details about it.

Network Setup

As a side note, in order for the VM to talk to the Host and Vice Versa, I use a Bridget Network.

Here I have a table that shows what type of network you can select in order to make one or the other system work as expected.

Network Modes supported by VirtualBox
Mode VM ←→ VM VM → Host VM ← Host VM → LAN VM ← LAN
Not
Attached
NAT + Port Forward + Port Forward
NAT
Network
+ + Port Forward + Port Forward
Bridged + + + + +
Internal Network +
Host-only + + +
 

As we can see, the Bridged Network gives you access to all possibilities which in many cases is what you want for a test VM on your LAN. However, if you have a VM to run a server and want to limit the interactions, then Host-only may be more appropriate for you. Either way, you can use your Host firewall to let the VM do or prevent the VM from doing things to your Host and other VMs.