Setup folder sharing between windows host and linux guest
This note illustrates how to share folders and files between a Windows host and Linux guest through a interconnected virtual network interface. In this setting, where Windows has the Hyper-V platform and the Samba service (including cifs support) installed and Linux OS is deployed in a Hyper-V virtual environment, host OS and guest OS are connect to a same network so that they can communicate each other.
本篇笔记记录如何利用虚拟网卡在Windows操作系统中与Hyper-V平台中的Linux虚拟机共享文件和文件夹。本文的情景为:Linux虚拟机安装在Hyper-V平台中利用虚拟网卡与主机通讯,并且需要读写Windows系统中的文件或文件夹。
Terminologies
Common Internet File System, developed by IBM at around mid 80s, is an open and cross-platform file sharing protocol which is a particular implementation of Server Message Block (SMB) protocol developed by Microsoft. Nowadays CIFS is seldomly used because SMB 2.0 and 3.0 are significant upgrades for CIFS and SMB 1.0. Although CIFS is almost outdated, the use of word CIFS is still kind of interchangeable with SMB. The misnomer of words do not really cause misunderstand in today’s world, but it is always good to know its background and reason.
Samba is a collection of softwares which allow Linux OS perform network related operations like creating file sharing server, printer sharing, name resolution etc. It implements the SMB protocol, which enables the interoperability. In terms of the relationship with SMB, it is kind of similar with CIFS.
Network File System (NFS) is a different file sharing protocol which is incompatible with SMB and its derivatives. Since SMB is the standard protocol for communication between Windows hosts, NFS is mainly used when the two hosts run Linux OS.
Preparation
Install the CIFS mounting service on Ubuntu
To access and mount the shared folder from Windows host, we need to install SMB utility tools. Simply run following in console, to install cifs-utils
.
sudo apt-get install cifs-utils
Install SMB Service on Windows
Since we are using a virtual environment in Hyper-V platform, we can boost the SMB performance by enable SMB Direct in Windows features. SMB Direct allows remote file server to resemble local storage with full speed at very low latency and very little computing resource utilization. This is crucial if user wish to perform heavy file IO operations.
SMB 1.0/CIFS support is not necessary if you plan to mount the folder using SMB 2.0 or higher in Linux guest system.
Then enable pubic folder sharing and make sure 128-bit encryption is selected in advanced sharing settings.
Communication between host and guest
make sure host and guest can communicate with each other, by some specific IP address (better to be static, but not a must).
run ipconfig /all
in Windows system, lookup the IP address for the virtual switch. Attempt to ping the host IP address in Linux guest console. Fix any connection issue if the ping does not respond.
Folder sharing and mounting
Create a shared folder in Windows host
Once the preparation is done, now you can create a folder or select an existing folder to share. Right click the file, select properties. in Sharing
tab, click button Share...
. Make sure the user under your Windows username have the permissions to read and write, then click Share
. It is actually not a mandatory to use your current user to access the shared folder, you can specify any existing user in this step. Just to remember who has the permission to access this folder and of course the password for the user.
Mount the network folder in Linux guest
sudo mount.cifs //[IP]/[win-share-name] /[linux-path] -o username=[win-usr],password=[pwd],uid=$(id -u [linux-user]),gid=$(id -g [linux-user]),forceuid,forcegid,vers=3.0
Type your Windows system name in the [IP]
section, or you can type the IP address directly. [win-share-name]
is the share name you specify when set the share properties. You can check this share name under share
section in properties at any time. [linux-path]
is the mounting point in Linux guest system. Remember to type your Windows username and password in [win-usr]
and [pwd]
section.
By default, the mounted folder and its entire content are granted permission to root user. You will need to specify your linux username in [linux-user]
section to allow specific user to read from and to write to this its content.
Note on accessing by Computer name
If you can access windows smb service by your computer name, you can just type //Desktop9511C3A2/folder instead of IP address. This sometimes does not work, and may cause a connection lost(in my experience it does cause a connection problem with heavy r/w operations, maybe that is a bug). IP address is preferred in this case. If static IP address is not available, we can always get the IP address by lookup in network config. Following section describes how to automatically detect the host/guest IP address, using powershell and bash scripts.
Startup mounting
Automatically IP detection
References
https://www.varonis.com/blog/cifs-vs-smb/ https://websiteforstudents.com/share-files-on-ubuntu-16-04-lts-with-windows-10-systems/