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.

@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.

@goodmeow
Copy link

thanks, this way still working on Ubuntu Server 24.04.1 LTS

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