Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save estorgio/0c76e29c0439e683caca694f338d4003 to your computer and use it in GitHub Desktop.
Save estorgio/0c76e29c0439e683caca694f338d4003 to your computer and use it in GitHub Desktop.
Mounting VirtualBox shared folders on Ubuntu Server 18.04 LTS (Bionic Beaver)

Mounting VirtualBox shared folders on Ubuntu Server 18.04 LTS (Bionic Beaver)

This guide will walk you through the steps on how to setup a VirtualBox shared folder inside your Ubuntu Server guest.

Prerequisites

This guide assumes that you are using the following setup:

You could still make this guide work with other setups (possibly with some modifications to the commands and whatnot). But if you want to do it the way I did then please feel free to use my setup above.

Initial Steps

  • Open VirtualBox

    screenshot

  • Right-click your VM, then click Settings

    screenshot

  • Go to Shared Folders section

    screenshot

  • Add a new shared folder

    screenshot

  • On Add Share prompt, select the Folder Path in your host that you want to be accessible inside your VM. Type shared for the Folder Name. Make sure that Read-only and Auto-mount are unchecked and Mount point is blank. Then click OK.

    screenshot

  • Start your VM

    screenshot

  • Once your VM is up and running, go to Devices -> Insert Guest Additions CD image

    screenshot

  • Use the following command to mount the CD

    sudo mkdir /media/cdrom
    sudo mount -t iso9660 /dev/cdrom /media/cdrom
    
  • Install dependencies for VirtualBox guest additions

    sudo apt-get update
    sudo apt-get install -y build-essential linux-headers-`uname -r`
    
  • Run the installation script for the guest additions. Wait until the installation completes.

    sudo /media/cdrom/./VBoxLinuxAdditions.run
    
  • Reboot VM

    sudo shutdown -r now
    
  • Create shared directory in your home

    mkdir ~/shared
    
  • Mount the shared folder from the host to your ~/shared directory

    sudo mount -t vboxsf shared ~/shared
    
  • The host folder should now be accessible inside the VM.

    cd ~/shared
    

Make the mount folder persistent

This directory mount we just made is temporary and it will disappear on next reboot. To make this permanent, we'll set it so that it will mount our ~/shared directory on system startup

  • Edit fstab file in /etc directory

    sudo nano /etc/fstab
    
  • Add the following line to fstab (separated by tabs). Make sure to replace <username> with your username. Save the file.

    shared	/home/<username>/shared	vboxsf	defaults	0	0
    
  • Edit modules

    sudo nano /etc/modules
    
  • Add the following line to /etc/modules and save

    vboxsf
    
  • Reboot the VM and log-in again

    sudo shutdown -r now
    
  • Go to your home directory and check to see if the directory is highlighted in green.

    screenshot

If it is then congratulations! You successfully linked the directory within your VM with your host folder.

Bonus: Using shared folders as Apache root directory

How to point apache's web directory to our folder in the host.

  • Remove apache's old html directory (WARNING! Backup your data if necessary)
    sudo rm -rf /var/www/html	
    
  • Add a symbolic link in its place
    sudo ln -s ~/shared /var/www/html
    

Note: This setup works fine with Windows hosts. But if you are using Linux or Mac as the host then you may have to set appropriate file permissions on your host directory with chmod in order to make it work.

Still not working?

If you've followed the steps above and it still doesn't work, please let me know by posting a comment below. Please use the following format and be sure to be as detailed as possible so that I can have enough information to help you out.

VirtualBox version: <insert VirtualBox version here>
Host: <OS name> <version>
Guest: <OS name> <version>
Description: <detailed description of the problem>

Do note that I'm mostly a Windows user and I'm not that well versed with Linux but I will do my best to help you out.

@tomsasson-fibernet
Copy link

tomsasson-fibernet commented Aug 29, 2021

VirtualBox version: Version 6.1.26 r145957 (Qt5.6.2)
Host: Windows 10 Version 21H1 (OS Build 19043.1165)
Guest: ubuntu 20.04 LTS
Description:
I succeeded mounting "shared" folder like in the description, but then I wanted to mount another folder.
I followed the same steps, but I receive a message:
/sbin/mount.vboxsf: shared folder '/home/fibernet/UHE01-RX' was not found (check VM settings / spelling)

This is the configuration in the VirtualBox Settings:
image

And this is how the directories are shown on the Ubuntu machine
image

Then what am I missing out? Can't I share another folder of my choice?

EDIT: I want to note that the persistent solution worked, after reboot I had it all shared, only the local commands created issues.

@ST1LLY
Copy link

ST1LLY commented Oct 27, 2021

@tomsasson-fibernet
Hi, I've faced the same issue.

VirtualBox: 6.1.28 r147628 (Qt5.6.2)
Host: Windows 10 Home 10.0.19042
Guest: Ubuntu 20.04.3 LTS (GNU/Linux 5.4.0-89-generic x86_64)

/sbin/mount.vboxsf: shared folder '/home/user/some-folder' was not found (check VM settings / spelling)

This answer helped me.

@anhhaibkhn
Copy link

It works !!! @estorgio
Thanks for saving my day.

@JWingUtk
Copy link

JWingUtk commented Apr 21, 2022

This guide saved me! No kidding. Been trying to get it to work for HOURS.

My only remark is the following portion did not work in my 20.04 LTS Ubuntu VM.

sudo mkdir /media/cdrom sudo mount -t iso9660 /dev/cdrom /media/cdrom

It mentioned that it would not allow the disk to be written to the directory as it was read only.

This wasn't a real issue as adding the guest additions CD prompted the system to request to run it automatically.

@laurencefass
Copy link

Ive installed the extensions and Im using virtualbox 6

when i try: sudo mount -t vboxsf vboxshare~/vboxshare

i see message: "mount: /root/vboxshare: mount point does not exist"

if i manually created /root/vboxshare and rerun the mount command i see:

"mount: /root/vboxshare: mount(2) system call failed: No such file or directory"

@wb6vpm
Copy link

wb6vpm commented Aug 23, 2022

I actually need the opposite of this!

Host: Ubuntu 22.04 LTS (CLI only, no GUI available) running VirtualBox 6.1.34
VM: Windows 10 Pro

Anyone have any pointers to where I can find a walkthrough for this?

@Sharaf5
Copy link

Sharaf5 commented Aug 23, 2022

@wb6vpm : what exactly you mean by this word ?

I actually need the opposite of this!

what do you need exactly ?

@Sharaf5
Copy link

Sharaf5 commented Aug 23, 2022

@wb6vpm : if you mean reverse direction sharing then you should see this answer https://superuser.com/a/243768/1050329

@DomCoccorese
Copy link

this guide seems to be broken. Black screen after this instruction:

sudo apt-get update
sudo apt-get install -y build-essential linux-headers-uname -r

The black screen persists also after reboot. Now I need to recover the whole VM from another device

@sergioponguta
Copy link

Working perfectly on Ubuntu Server 22.10.

Thank you so much for sharing!

@gelldur
Copy link

gelldur commented Aug 19, 2023

Consider adding (In guest OS) user to group with:
sudo adduser your-user vboxsf
See link
In my case I don't have to mount manually as work as expected.

@akshayhs
Copy link

akshayhs commented Sep 8, 2023

Thanks, adding a comment to confirm its working with Ubuntu Server 22.04 and VirtualBox 7.0.10

@sinalalebakhsh
Copy link

I want connect from Ubuntu22.04 host to Windows10 guest.
How to connect ?
do U No?

@1nkid
Copy link

1nkid commented Jan 26, 2024

And if you want e.g. copy a file from host: Windows10 to guest: Ubuntu-server — u can use scp (over ssh) in PowerShell:
scp C:\Users\user\shared\archive.tar.gz user@localhost:~/anyFolder

@sinalalebakhsh
Copy link

Thanks so much.

@SPDhanush07
Copy link

SPDhanush07 commented Feb 7, 2024

Hi While trying to make use of this document, I had trouble executing this command "sudo apt-get install -y build-essential linux-headers-uname -r" thus used "sudo apt-get install -y build-essential linux-headers-$(uname -r)". worked well.

Also, While installation scripts of the guest editions instead of using this cmd "sudo /media/cdrom ./VBoxLinuxAdditions.run" use "cd /media/cdrom && sudo ./VBoxLinuxAdditions.run" helped in getting job done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment