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:
- Have access to the shared directory on the host machine
- 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:
- Launching a CentOS7 Virtual Machine on Windows10
- Launching a CentOS7 Virtual Machine on Windows10: Part 2: Networking
Step 1: Add Shared Folder in VirtualBox
- In VirtualBox, select your virtual machine from the list.
- Click ‘Settings’ at the top.
- Click ‘Shared Folders’ on the left-hand side.
- Click the ‘folder with green plus’ icon on the right-hand side.
- 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’.
- Folder name: choose a name for the virtual machine folder, e.g. ‘share’.
- Check the boxes for ‘Auto-mount’ and ‘Make-permanent’.
- Click ‘OK’.
- 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.
- In VirtualBox, find the version by going to Help -> About VirtualBox.
- Go to http://download.virtualbox.org/virtualbox/ in a browser, find your version of VirtualBox and click on it.
-
Find the
VBoxGuestAdditions_x.x.x.iso
file in the list (where x.x.x is your VirtualBox version). - Right-click on the iso link and select ‘Copy link address’.
-
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. -
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
- Once these have all finished, reboot the virtual machine to pick up the kernel changes.
- When the virtual machine has rebooted, SSH into the machine using PuTTY.
-
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. - 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).
- Run the following:
$ cd /etc/sudoers.d $ echo "osboxes ALL=(ALL) NOPASSWD: ALL" > osboxes $ chmod 0440 osboxes
- 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).
- SSH onto the virtual machine via PuTTY (if not already).
- Run the following:
$ sudo usermod -a -G vboxsf osboxes
- Now create a new directory for the shared directory to mount to:
$ mkdir /home/osboxes/vboxshare
- Next run:
$ echo "sudo mount -t vboxsf -o uid=1000,gid=1000 share /home/osboxes/vboxshare" >> /home/osboxes/.bashrc
Note thatshare
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. -
Now you can either close your PuTTY session and SSH in again, or from the osboxes home directory, run:
$ cd $ . .bashrc
-
Now try the following:
$ cd vboxshare $ touch test
- 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.
-
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!
1 Response
[…] Launching a CentOS7 Virtual Machine on Windows10: Part 3: Shared Folder March 25, 2022 […]