Setting up shared folders in VirtualBox between a host operating system (Windows, macOS, or Linux) and a guest Linux system is a common and convenient way to share files. Here’s a comprehensive guide:
Prerequisites:
- VirtualBox Installed: You have VirtualBox installed on your host machine. Guest OS Installed: You have a guest Linux operating system installed in VirtualBox. Guest Additions: This is the most important prerequisite. The VirtualBox Guest Additions must be installed within the guest Linux operating system. These drivers and system applications optimize performance and enable features like shared folders.
Steps:
1. Install VirtualBox Guest Additions (if not already installed):
- Start the Virtual Machine: Boot up your Linux guest OS in VirtualBox. Mount the Guest Additions CD Image: In the VirtualBox window, go to “Devices” -> “Insert Guest Additions CD image…”. This will mount the Guest Additions ISO image as a virtual CD-ROM drive within the guest OS. Open the CD-ROM Drive: In your Linux guest OS, open the CD-ROM drive that now contains the Guest Additions. The exact location will depend on your Linux distribution (e. g., /media/cdrom, /media/VBox_GAs_xxx, /mnt/cdrom). Run the Installation Script: Open a terminal in your Linux guest OS and navigate to the CD-ROM drive. Execute the appropriate installation script. The most common script is:
· sudo./VBoxLinuxAdditions. run
- Important: You may need to install build tools and kernel headers for your Linux distribution before the Guest Additions can be compiled and installed correctly. The error message from the ./VBoxLinuxAdditions. run script will often tell you which packages are missing. Common packages include:
- build-essential (for Debian/Ubuntu) gcc, make, kernel-devel (for CentOS/RHEL/Fedora)
Example (Debian/Ubuntu):
O sudo apt update
O sudo apt install build-essential linux-headers-$(uname — r)
O sudo ./VBoxLinuxAdditions. run
- Example (CentOS/RHEL/Fedora):
O sudo yum update
O sudo yum install kernel-devel-$(uname — r) kernel-headers-$(uname — r) gcc make
O sudo ./VBoxLinuxAdditions. run
- Example (Arch Linux):
O sudo pacman — S linux-headers
O sudo ./VBoxLinuxAdditions. run
- Uname — r command: This command returns the currently running kernel version.
Reboot the Guest OS: After the Guest Additions are successfully installed, reboot your Linux guest OS.
2. Configure Shared Folders in VirtualBox Settings:
- Shut Down the Virtual Machine: Power off the Linux guest OS. (Important: Do not just save the machine state). Open VirtualBox Manager: In VirtualBox Manager, select your Linux virtual machine and click on “Settings”. Go to “Shared Folders”: In the Settings window, navigate to the “Shared Folders” section. Add a New Shared Folder: Click the “+” (Add new shared folder) icon. Folder Path: Browse and select the directory on your host machine that you want to share with the guest OS. Folder Name: Enter a name for the shared folder. This name will be used to access the shared folder in the guest OS. Keep it simple (e. g., “Shared”). Avoid spaces and special characters. Read-Only (Optional): Check this box if you only want the guest OS to be able to read files from the shared folder and not write to it. Auto-mount (Recommended): Check this box to automatically mount the shared folder when the guest OS starts. Make Permanent (Optional): Check this box to make the shared folder permanent, so it will be available every time you start the VM. If unchecked, it will only be available for the current session. Click “OK” to save the shared folder settings. Click “OK” to close the Settings window.
3. Access the Shared Folder in the Guest OS:
The location where the shared folder is mounted in the guest OS depends on the Linux distribution and the settings you’ve configured. However, the most common location is under /media or /mnt.
- Check /media: Open a terminal in your Linux guest OS and check the /media directory:
· ls /media
You might see a directory with the same name as the “Folder Name” you specified in the VirtualBox settings (e. g., /media/sf_Shared). The sf_ prefix is often added automatically by VirtualBox.
- Check /mnt: If the shared folder is not in /media, check /mnt:
· ls /mnt
You might find a directory named after your shared folder.
- If the shared folder is not mounted: You may need to manually mount it.
- Create a Mount Point (if necessary): Choose a directory where you want to mount the shared folder. If it doesn’t exist, create it:
O sudo mkdir /mnt/sharedfolder # Replace "sharedfolder" with your desired mount point name
- Mount the Shared Folder Manually:
O sudo mount — t vboxsf Shared /mnt/sharedfolder
- Replace "Shared" with the “Folder Name” you specified in the VirtualBox settings. Replace /mnt/sharedfolder with the actual path to your mount point.
Add to /etc/fstab for Automatic Mounting (Alternative to Auto-mount in VirtualBox): If you want the shared folder to be automatically mounted at boot time and the “Auto-mount” option in VirtualBox isn’t working, you can add an entry to the /etc/fstab file. This is a more advanced technique.
- Edit /etc/fstab:
O sudo nano /etc/fstab
- Add a line similar to the following:
O Shared /mnt/sharedfolder vboxsf defaults 0 0
- Replace "Shared" with the “Folder Name” you specified in the VirtualBox settings. Replace /mnt/sharedfolder with the actual path to your mount point.
Save the changes and close the editor. Test the mount:
O sudo mount — a
This will mount all file systems listed in /etc/fstab. If there are any errors, they will be displayed.
4. Permissions Issues:
- User Permissions: You may encounter permissions issues when accessing the shared folder from the guest OS. This is because the user account in the guest OS may not have the necessary permissions to access files created by the host OS user (or vice versa). Adding Your User to the Vboxsf Group: The simplest solution is to add your user account in the guest OS to the vboxsf group:
· sudo usermod — a — G vboxsf $USER
- $USER is a variable that represents your current username.
Log Out and Log Back In: After adding yourself to the vboxsf group, you Must log out and log back in for the changes to take effect.
Troubleshooting:
- Guest Additions Not Installed Correctly: This is the most common cause of shared folder problems. Make sure the Guest Additions are installed correctly and that you’ve rebooted the guest OS after installation. Incorrect Mount Point: Double-check that you’re using the correct mount point for the shared folder. Incorrect Shared Folder Name: Make sure the “Folder Name” in the VirtualBox settings matches the name you’re using in the mount command or in /etc/fstab. File System Errors: Run a file system check on the host and guest operating systems to rule out any file system errors. Conflicting Mounts: Make sure there are no other file systems mounted on the same mount point. Firewall: Ensure that your firewall isn’t blocking access to the shared folder. SELinux (CentOS/RHEL/Fedora): If you’re using SELinux, you may need to adjust the SELinux context of the shared folder. This is a more advanced topic.
By following these steps carefully, you should be able to successfully set up shared folders in VirtualBox between your host OS and your guest Linux system. Remember to prioritize installing the VirtualBox Guest Additions correctly, as this is essential for shared folders to function properly. If you encounter issues, check the logs and consult the VirtualBox documentation.