VirtualBox Shared Folders
There's no delight in owning anything unshared. — Seneca https://en.wikipedia.org/wiki/Seneca_the_Younger
When you need to share folders between Windows and Oracle VirtualBox then you are probably looking for the VirtualBox Shared Folders feature.
I’m doing this on Windows 11 host for a VirtualBox guest of Ubuntu 20.04 (see: Virtual machine - Simple English Wikipedia). Note that the following assumes you have installed VirtualBox Guest Additions installed on your Ubuntu guest.
Select a Folder on the Host
I’m just using my personal folder in C:\Users\<username> but if I needed to create a directory I could also do so. Note this can take some time if the folder includes a lot of files.
Share the Folder
Security note: do this for your local Windows user only! “Everyone” in this case means “Everyone on the Internet!” If you must share with everyone (and I would NOT recommend this) ensure it is at the very least read only.
Right-click on the folder, select Properties and switch to the Sharing tab. Click on the Advanced sharing button and click the Share This Folder button. Then, click on the Permissions button and click the Full Control checkbox.
Add the Folder to VirtualBox
Right-click on the virtual machine, select Settings and switch to the Shared Folders tab.
Click the folder with the green plus and choose the shared folder path you created earlier. Then, click the Auto-mount checkbox and click the OK button.
See below screen shots:
Note that these details are important going forward. If you use other values (for example, your Folder Path will definitely be different) be sure to substitute them. My folder share details are:
Folder Path: C:\Users\mday39
Folder Name: C
Mount Point: /mnt/C
Read Only unchecked
Auto-mount checked.
Start Ubuntu Guest and Log In
I have also verified these instructions will work for Manjaro Linux.
Then, at a command prompt enter the following:
sudo usermod -aG vboxsf $(whoami)
This will add your user to the vboxsf group. After that, shutdown and restart the VirtualBox guest Ubuntu instance.
Log in again to the Ubuntu guest instance and enter a terminal. There you should be able to enter:
cd /mnt/C
ls
And Voila! A list of files in your C:\Users\<username> should appear.
I used to automate this process further by adding the mount point to the /etc/fstab table https://en.wikipedia.org/wiki/Fstab, but I do not find that to be necessary now because VirtualBox does it for me with the automatic mount point.
Bonus: VirtualBox SSH Keys
We will now show how to setup your VirtualBox to recognize your Secure Shell SSH keys (see: What is an SSH Key?) for things such as GitHub. This may require reading my article https://www.keypuncher.net/blog/git-ssh-keys-windows because you will need to first have set up your keys on Windows. Alternatively, you could generate the keys yourself, it’s up to you. I will use the method where I already have generated the keys in Windows.
When you’ve got your keys set up in Windows go to a command prompt in the VirtualBox Ubuntu guest. Enter the following commands:
cd
mkdir .ssh (if necessary)
cd /mnt/c/.ssh
cp id_rsa* $HOME/.ssh
cp id_ed25519* $HOME/.ssh… you get the idea … just copy whatever SSH keys you have.
cd $HOME/.ssh
chmod 600 id_rsa
chmod 600 id_ed25519
You will also need to install the Ubuntu packages openssl-client and git-core. Then you need to put this at the bottom of your $HOME/.bashrc file:
#source: https://stackoverflow.com/a/18915067
SSH_ENV=$HOME/.ssh/environment
function start_agent {
echo "Initializing new SSH agent..."
/usr/bin/ssh-agent | sed 's/^echo/#echo/' > "${SSH_ENV}"
chmod 600 "${SSH_ENV}"
. "${SSH_ENV}" > /dev/null
/usr/bin/ssh-add
}
if [ -f "${SSH_ENV}" ]; then
. "${SSH_ENV}" > /dev/null
ps -ef | grep ${SSH_AGENT_PID} | grep ssh-agent$ > /dev/null || {
start_agent;
}
else
start_agent;
fi
Then close all terminal windows in the Ubuntu guest. Then and reopen one terminal and if you have done everything correctly you will be prompted to enter your SSH key passphrase, if you have set one up. See: Passphrase Explained: What is a Good Passphrase? (ssh.com). After that you should be able to pull down your GitHub files!
Summary
That’s all there is to it folks! You now are able to share folders and SSH keys with VirtualBox!
Feedback
As always, do make a comment or write me an email if you have something to say about this post!