Launching a CentOS7 Virtual Machine on Windows10: Part 3: Shared Folder

In my previous post, Launching a CentOS7 Virtual Machine on Windows10: Part 2: Networking, I went through the steps I took to configure networking on a CentOS7 virtual machine. In this post, I’ll go through how I added a shared directory to the virtual machine, which is a directory on the host machine that the virtual machine can access. This involves adding a shared directory in VirtualBox, installing VirtualBox Guest Additions, setting up passwordless sudo and adding a mount command to your .bashrc file.

Once the shared directory is configured, the virtual machine will:

  1. Have access to the shared directory on the host machine
  2. Be able to use a directory on the host machine as if it’s a local directory

Prerequisites

Before you start, you will need to set up the virtual machine, PuTTY SSH and networking by following the steps in:

  1. Launching a CentOS7 Virtual Machine on Windows10
  2. Launching a CentOS7 Virtual Machine on Windows10: Part 2: Networking

Step 1: Add Shared Folder in VirtualBox

  1. In VirtualBox, select your virtual machine from the list.
  2. Click ‘Settings’ at the top.
  3. Click ‘Shared Folders’ on the left-hand side.
  4. Click the ‘folder with green plus’ icon on the right-hand side.
  5. Folder Path: Choose a folder path on the Host machine. This should be a folder that exists on your Windows10 host machine, e.g. ‘C:\Users\yourname\osbox’.
  6. Folder name: choose a name for the virtual machine folder, e.g. ‘share’.
  7. Check the boxes for ‘Auto-mount’ and ‘Make-permanent’.
  8. Click ‘OK’.
  9. Stop and start the virtual machine.

Step 2: Install VirtualBox Guest Additions

In order for the mount you set up in VirtualBox to work on your virtual machine, there are a couple of things you need to do. One is installing VirtualBox Guest Additions on the virtual machine.

  1. In VirtualBox, find the version by going to Help -> About VirtualBox.
  2. Go to http://download.virtualbox.org/virtualbox/ in a browser, find your version of VirtualBox and click on it.
  3. Find the VBoxGuestAdditions_x.x.x.iso file in the list (where x.x.x is your VirtualBox version).
  4. Right-click on the iso link and select ‘Copy link address’.
  5. SSH into your virtual machine using PuTTY. Run:
    $ wget http://download.virtualbox.org/virtualbox/x.x.x/VBoxGuestAdditions_x.x.x.iso
    
    Be sure to replace the url with the iso link you copied.
  6. Now run the following to update the virtual machine kernel and install everything that VirtualBox Guest Additions needs:
    $ sudo su
    $ yum -y install epel-release
    $ yum -y update
    $ yum install make gcc kernel-headers kernel-devel perl dkms bzip2
    
  7. Once these have all finished, reboot the virtual machine to pick up the kernel changes.
  8. When the virtual machine has rebooted, SSH into the machine using PuTTY.
  9. Run:
    $ sudo su
    $ export KERN_VER=$(uname -r)
    $ mount -r VBoxGuestAdditions_x.x.x.iso /media
    $ cd /media
    $ ./VBoxLinuxAdditions.run
    
    Be sure to replace ‘x.x.x’ with your version of VirtualBox.
  10. If the script finishes with the line ‘VirtualBox Guest Additions: Starting’, then it’s completed successfully.

Step 3: Set Up Passwordless Sudo

In this step, we will set up passwordless sudo. This will enable sudo commands to be run automatically, without needing a password to be entered (for example in the .bashrc file in the next step).

  1. Run the following:
    $ cd /etc/sudoers.d
    $ echo "osboxes ALL=(ALL) NOPASSWD: ALL" > osboxes
    $ chmod 0440 osboxes
    
  2. Try running:
    $ exit
    $ sudo su
    
    If all goes well, you should be able to elevate to root without having to enter your password.

Step 4: Set Up a Recurring Mount

In this step, we will add the osboxes user to the vboxsf group, which will allow it to see shared directories. We’ll also set up a mount command in the .bashrc file. This will enable the shared directory to be mounted each time the osboxes user starts a new bash terminal session (e.g. via PuTTY).

  1. SSH onto the virtual machine via PuTTY (if not already).
  2. Run the following:
    $ sudo usermod -a -G vboxsf osboxes
    
  3. Now create a new directory for the shared directory to mount to:
    $ mkdir /home/osboxes/vboxshare
    
  4. Next run:
    $ echo "sudo mount -t vboxsf -o uid=1000,gid=1000 share /home/osboxes/vboxshare" >> /home/osboxes/.bashrc
    
    Note that share should be the name of the shared folder you gave in Step 1, and /home/osboxes/vboxshare should be the directory just created as the mountpoint for the shared directory.
  5. Now you can either close your PuTTY session and SSH in again, or from the osboxes home directory, run:
    $ cd
    $ . .bashrc
    
  6. Now try the following:
    $ cd vboxshare
    $ touch test
    
  7. Navigate to the directory on the host machine that you selected for the share in Step 1. You should see a file named ‘test’ in the directory.
  8. Try creating a new file inside the host machine shared directory, and call it ‘test2’. Go back to your PuTTY session and run:
    $ ls vboxshare
    
    You should be able to see both the ‘test’ and ‘test2’ files listed. If so, the shared directory is working.

Conclusion

You should now have a virtual machine with a shared directory, that is accessible both from your virtual machine and your host machine.

If you want to learn about how to set up your virtual machine as an API server, see my follow-on post, Launching a CentOS7 Virtual Machine on Windows10: Part 4: API Server.

Thanks for reading!