Advanced VirtualBox Shared Folder

Hi all. I’m hoping that you are well in this strange time. In this post I will show you different way of setting up Shared Folder between your virtual machine and host PC when using Oracle’s VirtualBox. I will show you how to setup Samba Shared folder between virtual box and host PC. For Advanced VirtualBox Shared Folder using Samba we need to have Linux Guest OS, so that we can install Samba Server. For this post I’m using Windows 10 Host and Manjaro KDE guest OS.

If you are interested check out my first online course with topic Virtualization with VirtualBox.

What is Samba?

Samba is the standard Windows interoperability suite of programs for Linux and Unix.

Samba is a software package that gives network administrators flexibility and freedom in terms of setup, configuration, and choice of systems and equipment. Because of all that it offers, Samba has grown in popularity, and continues to do so, every year since its release in 1992.

Quote from samba.org

In plaint old English, with samba we can share folder between Windows and Linux PCs over the network.

Why Samba?

VirtualBox’s shared folders work fine, but there are couple of corner cases where Samba share folder is useful. What I mean. VirtualBox’s shared folders doesn’t play well when you want to access program files that are saved in those shared folders. In my casem I have problem with anti virus program that was erasing my .exe files from shared folder that we generated inside my VM. Because this, I tried this Advanced VirtualBox Shared Folder with Samba.

Setup process for Shared Folder

I will split Samba Shared Folder in couple of logical steps, so please follow careful.

Configure VM — Add additional network interface

We want to create Samba Shared Folder between or Virtual machine and our host OS. We don’t want for that folder to be visible on our network or to have access form then internet. This folder should be shared only internally between host and guest OS only. Because this reasons, we want to create additional virtual network interface that will do exactly that.

  • power off your VM
  • open VM settings and go to Network tab
  • Click on Adapter 2 and select Enable network adapter to activate second adapter
  • Change Attached to setting for Adapter 2 to Host-Only Adapter
  • Under Advanced option, set Promiscuous Mode to Allow All
  • Save configuration by clicking OK

Establish connection on second adapter

Now, we want to configure connection on our second network adapter. This part can be different on different Host and Guest OS, but logic behind is the same. We want to be able to ping our host OS form our guest OS using second network adapter and vise versa.

Follow the next steps:

  • Check IP address of VirtualBox Host-Only Adapter inside Windows 10 in Control Panel
    • Open Control Panel
    • Click on Network and Internet
    • Click on Network and Sharing Center
    • Then on the right hand side, click on Change adapter settings
    • Right click on VirtualBox Host-Only Adapter and click Properties
    • In Properties window select Internet Protocol Version 4 (TCP/IPv4) and click on Properties
    • Check IP address of VirtualBox Host-Only Adapter

In my case IP address of the VirtualBox Host-Only Adapter is 192.168.171.1. Now we need to set up second network adapter to the IP address that is in the same subnet, for the example 192.168.171.2.

To set second network adapter inside VirtualBox to 192.168.171.2. If you are using Windows 10 as Guest OS the steps are the same as steps for checking IP address above, just at the end we need to edit IP address. In this post, however, I will be using Manjaro KDE as a Guest OS.

Steps to change IP address in the Manjaro KDE Guest OS are shown at the images bellow. These steps will different if you are using different Guest OS, but logic is the same.

Now we can test ping between our host and guest on the VirtualBox Host-Only, and if this is successful we can continue on.

Configure Samba Server for Shared Folder

Install Samba

First, let’s check do we have samba installed on our guest OS. Next command will install samba, or give you a notification if it’s already installed. For this post I’m using Manjaro KDE, so if you are using different OS you will need to change this command a bit.

sudo pacman -S samba

Get Samba default configuration file

Next, we need to get default samba configuration file because right now we don’t have any. On Manjaro system we can do that with next command.

sudo pacman -S manjaro-settings-samba

We can now edit file in /etc/samba directory with name smb.conf. The default smb.conf looks like:

[global]
   workgroup = WORKGROUP
   dns proxy = no
   log file = /var/log/samba/%m.log
   max log size = 1000
   client min protocol = SMB2
   server role = standalone server
   passdb backend = tdbsam
   obey pam restrictions = yes
   unix password sync = yes
   passwd program = /usr/bin/passwd %u
   passwd chat = *New*UNIX*password* %n\n *ReType*new*UNIX*password* %n\n *passwd:*all*authentication*tokens*updated*successfully*
   pam password change = yes
   map to guest = Bad Password
   usershare allow guests = yes
   name resolve order = lmhosts bcast host wins
   security = user
   guest account = nobody
   usershare path = /var/lib/samba/usershare
   usershare max shares = 100
   usershare owner only = yes
   force create mode = 0070
   force directory mode = 0070

[homes]
   comment = Home Directories
   browseable = no
   read only = yes
   create mask = 0700
   directory mask = 0700
   valid users = %S

[printers]
   comment = All Printers
   browseable = no
   path = /var/spool/samba
   printable = yes
   guest ok = no
   read only = yes
   create mask = 0700

[print$]
   comment = Printer Drivers
   path = /var/lib/samba/printers
   browseable = yes
   read only = yes
   guest ok = no

Edit global section

For this post, I want to create Samba Share Folder, so we don’t need and shared home directories and printers. Because of that we can remove sections homes, printers and print$.

Check you workgroup name and update workgroup variable in global section. Default value is WORKGROUP and this is fine if you didn’t change your workgroup name. If you did enter this workgroup name of your host OS.

We need to add section to enable windows support, and this is done by adding next lines.

   # Windows Internet Name Serving Support Section:
   # WINS Support - Tells the NMBD component of Samba to enable its WINS Server
   wins support = yes

Also, we want this Samba connection to be establish only between our host and guest, so now we want to allow samba communication only on our Host-Only adapter. For that we need to add section Networking in our smb.conf file. We are specifying the interfaces 127.0.0.0/8 that is our localhost and 192.168.171.0/24 that is Host-Only interface.

   #### Networking ####

   # The specific set of interfaces / networks to bind to
   # This can be either the interface name or an IP address/netmask;
   # interface names are normally preferred
   ;
   interfaces = 127.0.0.0/8 192.168.171.0/24

   # Only bind to the named interfaces and/or networks; you must use the
   # 'interfaces' option above to use this.
   # It is recommended that you enable this feature if your Samba machine is
   # not protected by a firewall or is a firewall itself.  However, this
   # option cannot handle dynamic or non-broadcast interfaces correctly.
   ;   
   bind interfaces only = yes

And that is pretty much all that we need to change in global section of our smb.conf file. Next thing is to add Share Folder section.

Add Shared Folder section

I have prepared a template for share folder settings and it looks something like, so you just need to copy and paste this section and update it a bit.

[ShareFolderName]
   path = <path-to-shared-folder>
   read only = no
   guest ok = yes
   browseable = yes
   create mask = 0755
   directory mask = 0744
   valid users = <user-access-this-folder> %S
  • Change ShareFolderName into desired name for Samba Shared Folder. In my case that is blnLabs.
  • Create Folder and update path (I created folder blnLabsShare in home directory, so my path is /home/blnlabs/blnLabsShare)
  • add user to valid users list (I added my user for Manjaro guest OS and that user is blnlabs)

Final smb.conf

And finally our smb.conf file looks like:

[global]
   workgroup = BLNGROUP
   
   # Windows Internet Name Serving Support Section:
   # WINS Support - Tells the NMBD component of Samba to enable its WINS Server
   wins support = yes

   dns proxy = no
   log file = /var/log/samba/%m.log
   max log size = 1000
   client min protocol = SMB2
   server role = standalone server
   
   #### Networking ####

   # The specific set of interfaces / networks to bind to
   # This can be either the interface name or an IP address/netmask;
   # interface names are normally preferred
   ;
   interfaces = 127.0.0.0/8 192.168.171.0/24

   # Only bind to the named interfaces and/or networks; you must use the
   # 'interfaces' option above to use this.
   # It is recommended that you enable this feature if your Samba machine is
   # not protected by a firewall or is a firewall itself.  However, this
   # option cannot handle dynamic or non-broadcast interfaces correctly.
   ;   
   bind interfaces only = yes

   passdb backend = tdbsam
   obey pam restrictions = yes
   unix password sync = yes
   passwd program = /usr/bin/passwd %u
   passwd chat = *New*UNIX*password* %n\n *ReType*new*UNIX*password* %n\n *passwd:*all*authentication*tokens*updated*successfully*
   pam password change = yes
   map to guest = Bad Password
   usershare allow guests = yes
   name resolve order = lmhosts bcast host wins
   security = user
   guest account = nobody
   usershare path = /var/lib/samba/usershare
   usershare max shares = 100
   usershare owner only = yes
   force create mode = 0070
   force directory mode = 0070

[blnLabs]
   path = /home/blnlabs/blnLabsShare
   read only = no
   guest ok = yes
   browseable = yes
   create mask = 0755
   directory mask = 0744
   valid users = blnlabs %S

Configure password for valid user

To configure Samba password for valid user we need to enter next command and create samba password.

sudo smbpasswd -a <user-access-this-folder>

Reset Samba Server

And at the end we only need to restart our Samba service once and reboot our VM and we are done with Samba Server Configuration.

We can reset Samba service with next command.

sudo systemctl restart nmb

Connect from Host OS for Shared Folder

We are done with setting up our Samba Server on our Guest OS. Now we need to setup our client side, in my case that means to configure Windows 10. This is pretty simple task.

  • Open File manager
  • Right click on Networks on the left panel
  • Select Map Network driver…
  • In pop-up windows select desired drive letter
  • In Folder section you need to enter location to your shared folder. Location is consist of IP address of your Samba Server and name of your Samba shared folder (in my case share folder is located at \\192.168.171.2\blnlabs)
  • select Connect using different credentials (this is very impotent because, by default windows will try to connect with credentials for user that is currently logged in. In this case we don’t want that because we already prepared user for Samba access)
  • Click on Finish
  • Input Samba username and password
  • And you are connected

Final Words

And that is all. Now you can play around with Share Folder and Virtual Machine and test it’s limits. This was the solution for me when I needed to create executable form inside VirtualBox and then run that executable from Windows 10. My problem was because antivirus on Windows 10 started to removing .exe file from my shared folder. This problem was solved by switching to Samba Shared folder.

So, tell me. Did you manage to follow this instructions and setup Samba Shared Folder? Do you have some suggestions? Please comment down bellow, check out my other post regarding Virtualization How to start with virtual-machines in 2020 and see you in the next post.

Leave a Comment