Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Sharing Files Between Ubuntu Host and Virtual Machines

· 5 min read · Home Tech

In this article
  1. Why File Sharing Matters
  2. VirtualBox: Shared Folders and Drag-and-Drop
  3. VMware Workstation: Shared Folders and HGFS
  4. QEMU/KVM: Virtiofs and Samba
  5. How to Choose the Right Method
  6. Common Problems and Fixes
  7. Security Considerations
  8. Alternatives: Network Shares and Cloud Sync
  9. Frequently Asked Questions

Sharing files between an Ubuntu host and its virtual machines is done through shared folders, network shares, or drag-and-drop, depending on your virtualization software. This article covers the main methods for VirtualBox, VMware, and QEMU/KVM, including setup steps, performance considerations, and troubleshooting.

Why File Sharing Matters

When you run a virtual machine on Ubuntu, you often need to move files between the host and the guest. Common reasons include transferring source code, sharing documents, or moving installation media. Without a proper sharing mechanism, you might resort to USB drives or uploading to a cloud service, which is slow and cumbersome. The right method depends on your virtualization platform, the guest OS, and whether you need real-time synchronization or just occasional transfers.

VirtualBox: Shared Folders and Drag-and-Drop

VirtualBox is a popular free hypervisor on Ubuntu. It offers two main ways to share files:

  • Shared Folders: A host directory is mounted inside the guest as a network filesystem. This works with Linux, Windows, and macOS guests.
  • Drag-and-Drop: If Guest Additions are installed, you can drag files between the host and guest windows. This is convenient for small files but not reliable for large transfers.

To set up a shared folder in VirtualBox:

  1. Open the VM settings and go to Shared Folders.
  2. Add a new folder, choose a host path, and enable Auto-mount.
  3. Start the VM. In Linux guests, the folder appears under /media/sf_folder_name. You may need to add your user to the vboxsf group to access it.

For drag-and-drop, install Guest Additions (Devices → Insert Guest Additions CD image) and set the drag-and-drop mode to Bidirectional in VM settings.

VMware Workstation: Shared Folders and HGFS

VMware Workstation (and the free Player) uses the HGFS (Host-Guest File System) protocol for shared folders. Setup is similar:

  1. In VM settings, go to Options → Shared Folders.
  2. Add a host folder and enable sharing.
  3. In the guest, the folder appears under /mnt/hgfs/ (Linux) or as a network drive (Windows).

You must have VMware Tools installed in the guest. If the folder doesn't mount automatically, run sudo vmhgfs-fuse .host:/ /mnt/hgfs to mount it manually.

QEMU/KVM: Virtiofs and Samba

QEMU/KVM is the native virtualization stack on Ubuntu. It offers two robust options:

  • Virtiofs: A high-performance shared filesystem designed for VMs. It requires a Linux guest with virtiofsd running on the host. Example command: qemu-system-x86_64 -virtfs local,path=/home/user/share,mount_tag=hostshare,security_model=none. In the guest, mount with mount -t virtiofs hostshare /mnt/share.
  • Samba: Export a host directory via SMB and mount it in the guest as a network share. This works with any guest OS that supports SMB (Windows, Linux, macOS).

Virtiofs is faster than Samba for many workloads because it bypasses network overhead. However, it requires kernel support (Linux 5.4+ for virtiofs).

How to Choose the Right Method

Consider your priorities:

  • Ease of setup: VirtualBox and VMware shared folders are the simplest, with GUI configuration.
  • Performance: Virtiofs is generally the fastest, followed by Samba over a virtio network. Shared folders in VirtualBox and VMware have moderate overhead.
  • Guest OS compatibility: Samba works with any OS that supports SMB. Virtiofs is Linux-only. Shared folders work with most guests but may require extra drivers.
  • Persistence: Shared folders are automatically mounted at boot if configured. Samba mounts require an fstab entry or manual mount.

Common Problems and Fixes

  • Shared folder not visible in guest: Ensure Guest Additions (VirtualBox) or VMware Tools are installed and match the kernel version. Reboot the guest after installation.
  • Permission denied: In VirtualBox, add your user to the vboxsf group: sudo usermod -aG vboxsf $USER then log out and back in.
  • Slow transfer speeds: Use a shared folder over a virtio network (for KVM) or enable hardware acceleration in VirtualBox (Settings → System → Acceleration).
  • Drag-and-drop not working: Check that bidirectional mode is enabled and that Guest Additions are installed. Some Linux guests require the VBoxClient service to run.

Security Considerations

Shared folders are a two-way street: a compromised guest can read and write files on the host. To minimize risk:

  • Share only the specific directory you need, not your entire home folder.
  • Use read-only sharing when the guest only needs to read files.
  • Keep the guest OS and virtualization tools updated.
  • For sensitive data, consider using a one-way transfer method like a network share with read-only permissions.

Alternatives: Network Shares and Cloud Sync

If you prefer not to use platform-specific features, you can use standard network file sharing:

  • Samba/SMB: Run a Samba server on the host and mount the share in the guest. This is platform-agnostic and works over any network.
  • NFS: Linux-to-Linux sharing is simple with NFS. Export a directory on the host and mount it in the guest.
  • Cloud sync: Use a service like Nextcloud or Syncthing to synchronize files between host and guest. This works even if the VM is offline, but adds latency and requires internet.

Frequently Asked Questions

Why is my shared folder not mounted automatically in Ubuntu guest?

In VirtualBox, auto-mounted folders appear under /media/sf_name only if Guest Additions are installed and the user has permission. If it's not there, run sudo mount -t vboxsf name /mnt/point. For VMware, the HGFS module must be loaded; check with lsmod | grep vmhgfs.

Can I share a folder between two virtual machines?

Yes, you can share a host folder with multiple VMs by adding it to each VM's shared folders. Alternatively, set up a Samba share on the host and mount it in both guests.

What is the fastest way to share files with a KVM guest?

Virtiofs is the fastest, with near-native performance. If you can't use it, Samba over a virtio network is a good second choice.

Is it safe to enable drag-and-drop in VirtualBox?

It's generally safe if you trust the guest OS. However, it increases the attack surface; a malicious guest could potentially send files to the host. For sensitive environments, disable it.

How do I share a folder that is on an external drive?

You can share any host path, including mounted external drives. Just make sure the drive is mounted before the VM starts, otherwise the shared folder may not appear.