Introduction

Sharing files between a Windows host and Ubuntu virtual machine (VM) can be essential when you need to transfer data or collaborate between different environments. While the Hyper-V virtualization platform makes it easy to create Ubuntu VMs on a Windows host, the process of sharing files between the two systems can be a bit more complex. This is because Windows and Ubuntu use different file systems and protocols to access shared resources.

In this blog post, we will walk you through the steps required to share files between a Hyper-V Windows host and Ubuntu VM using the Common Internet File System (CIFS) protocol. This method allows you to mount a Windows shared folder on Ubuntu, giving you access to files on the Windows host as if they were on the Ubuntu machine itself. We will also cover the process of setting up a new Windows local user for authentication, creating a shared folder, and enabling network settings in Hyper-V. By the end of this guide, you will have a fully functional file sharing system that works seamlessly between Windows and Ubuntu.

1. Create a new Windows local user for sharing and authentication
To access a Windows shared folder from Ubuntu, you need to provide valid credentials for a user account that has permissions to access the shared folder. For security reasons, it’s not recommended to use your Windows user account for this purpose, as it could potentially expose your system to security risks. Instead, it’s best to create a new local user account that’s dedicated solely to file sharing.

Step-by-step guide for creating a new user in Windows
1. Press “Windows Key + R” on your keyboard to open the Run dialog box.
2. Type “netplwiz” in the box and click on “OK.”
3. In the User Accounts window that appears, click on the “Add” button.

4. Select “Sign in without a Microsoft account (not recommended)” at the bottom of the screen.
5. Click on “Local account” and then click on “Next.”
6. Enter a username and password for the new user and then click on “Next.”
7. You can choose whether to set a password hint for the new user account or not. Click on “Next” to proceed.
8. Click on “Finish” to complete the process.

I have created a user called “shareuser” with password as 123456. But please always use stronger password. Mine is just a test environment.

2. Create a Windows folder and enable sharing
In this step, we will create a new folder in Windows and enable sharing so that it can be accessed from our Ubuntu VM.

1. Open File Explorer and navigate to the location where you want to create a new folder.
2. Right-click on the empty space and select “New” > “Folder”.
3. Name the folder and press “Enter” on your keyboard.
4. Right-click on the newly created folder and select “Properties”.
5. In the Properties window, click on the “Sharing” tab.
6. Click on the “Share” button.
7. In the “Choose People to Share With” window, enter the name of the user you created earlier (e.g. “shareuser”).

8. Click on “Add” and then click on the “Share” button.
9. The folder should now be shared with the user you specified.

Note: If you don’t see the “Sharing” tab in the folder properties window, you may need to enable file and printer sharing in Windows by going to “Control Panel” > “Network and Sharing Center” > “Change advanced sharing settings” and selecting “Turn on file and printer sharing”. And I will be replacing the hostname “WAHAB” with an IP address in later stages.

Once you have shared the folder with the user, you can access it from your Ubuntu VM using the SMB protocol.

3. Enable default or external type network for VMs in Hyper-V
By default, the virtual machines are connected to the “Default Switch”. To communicate between the host and guest VM, you need to either use this default switch or external type virtual switch. If Private network type switch are used the Windows host will not be able to communicate or transfer files with guest VMs.

1. Open the Hyper-V Manager on the Windows host machine.
2. Select the virtual machine you want to connect to the network.
3. In the right-hand pane, click on “Settings”.
4. Click on “Network Adapter” and select “Virtual Switch” as the connection type.
5. Select either the “Default Switch” or an “External” virtual switch that you have previously created.
6. Click “OK” to save changes.
7. Start the virtual machine.

4. Find the private IP of Windows HyperV host
The private IP of the Windows host is needed to establish a connection between the Windows host and the Ubuntu VM. In order for the Ubuntu VM to access files on the Windows host, it needs to know the private IP address of the host so that it can connect to it over the network

1. Open the Command Prompt on the Windows host machine by pressing the Windows key + R and then typing “cmd” in the Run dialog box.
2. In the Command Prompt, type “ipconfig” and press Enter.
3. Look for the IPv4 Address entry. The number listed next to this entry is the private IP address of the Windows host.
Note: The private IP address is usually in the format of “192.168.x.x” or “172.x.x.x”.

5. Check folder shared is accessible using smbclient from Ubuntu VM

smbclient is a command-line tool used to connect to Windows and Samba file servers. It allows us to browse and manipulate files and directories on remote servers using the Server Message Block (SMB) protocol.

In this step, we will use smbclient to verify if the shared folder is accessible from the Ubuntu VM.

Step-by-step guide for checking if the share is accessible using smbclient:

1. Open the terminal on Ubuntu VM.
2. Install smbclient if it’s not installed using the following command:

sudo apt-get install smbclient

3. Connect to the shared folder using the following command:

smbclient -U shareuser //172.30.96.1/FolderToShare

Note: Replace “shareuser” with the username of the Windows local user you created and “FolderToShare” with the name of the shared folder you created
4. Enter the password for the Windows local user when prompted.
5. If the connection is successful, you should see a prompt like this:

6. Mount the Windows share using cifs
CIFS (Common Internet File System) is a network protocol that allows Linux systems to access and share files and directories with Windows operating systems. It is needed to mount the Windows share on the Ubuntu VM so that the Ubuntu user can access the shared files and directories.

Here are the step-by-step instructions for mounting the Windows share using cifs:

Create a directory where you want to mount the Windows share. For example, let’s create a directory called “windows_share” under the home directory:

mkdir ~/windows_share

Install the cifs-utils package if it’s not already installed on the Ubuntu VM:

sudo apt-get update
sudo apt-get install cifs-utils

Create a file and add the Windows user credentials in it.

nano /home/wahab/.smbcredentials

username=shareuser
password=123456

Replace “shareuser” with the username you created for file sharing on your Windows host.
Replace “123456” with the password you set for the user.

Open the “/etc/fstab” file using a text editor with root privileges, such as nano:

sudo nano /etc/fstab

Add the following line at the end of the file:

//172.30.96.1/FolderToShare /home/wahab/windows_share cifs credentials=/home/wahab/.smbcredentials,uid=wahab,gid=wahab 0 0

Replace “172.30.96.1” with your Windows host IP address.
Replace “FolderToShare” with the name of the shared folder on your Windows host.
“/home/wahab/windows_share” will be the folder you mounting your Windows share. So you may choose different one as per your need.
The “uid” and “gid” options set the ownership of the mounted directory to the Ubuntu user “wahab”, replace them with yours.
The “0 0” options indicate that the filesystem should be dumped and checked by default.
Save and close the file.

Here’s how to mount and unmount the Windows share as the Ubuntu user “wahab”:

To mount the share:

sudo mount ~/windows_share

Check if the Windows share is mounted by running a “df -h” command

To unmount the share:

sudo umount ~/windows_share

In conclusion, sharing files between a Windows host and Ubuntu VM can be accomplished through the use of the Common Internet File System (CIFS) protocol. The process involves creating a new Windows local user for sharing and authentication, creating a Windows folder and enabling sharing, and configuring the network settings in Hyper-V. Once these steps are completed, you can easily access the shared folder from your Ubuntu VM as if it were on the local machine. It’s important to ensure that you follow security best practices by using a dedicated user account for file sharing and setting a strong password. With these steps, you can establish a seamless and secure file sharing system between Windows and Ubuntu.