Play WSL – Replace Win10’s native file sharing with Samba services
People who are used to Linux systems must feel that Samba services are more flexible and convenient. How to build efficient Samba services on Windows systems? This paper introduces how to build Samba file sharing service on Win10 system through WSL.
The background is that there is only one desktop computer in the family. On the one hand, it is occasionally used by the family. On the other hand, it is the data center of the family, which mounts several hard disks. So the system uses Windows 10, but in order to facilitate flexible file sharing, Samba service has been considered. There are many ways to achieve this goal. We can consider using virtual machines, such as VMWare, VirtualBox, Hyper-V, or CygWin. Because Windows 10 provides WSL, this paper introduces another way, which is also a more efficient way to build Samba services using WSL.
Windows File Sharing Service
Using the network to provide file access services to the outside world, it is now widely used for file sharing within the LAN, such as millet box, mobile phone, PC, including Mac, through ports 139 or 445.
- 139 – Early Windows SMB runs on NBT (NetBIOS over TCP/IP), using port 139
- 445 – Windows 2000 and above version, SMB can run directly with TCP/IP, using 445 port, more efficient
The test results show that WinXP only accesses 139 ports, Win7 accesses 139 and 445 ports, and Win10 accesses 445 ports. The test results are as follows:
WSL（Windows Subsystem for Linux）
It is on Windows 10 system that the Windows kernel is encapsulated so that it can directly support the Linux executable program in ELF format. It feels a little like GNU/Windows.
Now, based on WSL, there are many Linux distributions, including Ubuntu, Debian, Suse, Kali, etc. It is very convenient to run Linux distributions in Windows, to learn and use various Linux tools, and to seamlessly integrate various services in Linux into Windows environment, such as Samba services, SSH services, etc.
In order to realize that UNIX, Linux and other non-Windows can access the file sharing resources of Windows, a Daniel has analyzed the SMB (Server Message Block) protocol in reverse, and then developed an open source software Samba according to the reverse analysis protocol, which can enable UNIX, Linux, Mac and other systems to share files with Windows. At present, the latest Samba can even provide AD, DC and other services.
Based on the above background knowledge, the scheme of building Samba service through WSL on Windows 10 is as follows:
- Release 139,445 ports occupied by Windows
- Install WSL and Ubuntu distributions
- Install the Samba service and configure it
- Change firewall configuration
- Final test
Release 139,445 ports occupied by Windows
Release port 139
- The installed Windows 10 system will listen on ports 139 and 445 by default.
netstat -a|findstr LISTENINGYou can see:
- Disable NetBIOS over TCP/IP. Disable it through the following operations. In the “Change adapter settings” interface, double-click the corresponding network card and open the “Ethernet status” interface:
- Click “Properties” to open the “Ethernet Properties” interface:
- Select the “TCP/IPv4” project and click on “Properties” to open the “TCP/IPv4 Properties” interface:
- Click “Advanced” and select the “WINS” tab. Check “Disable NetBIOS on TCP/IP”:
- After clicking “OK”, it will take effect. If there are multiple network cards, each network card should follow the steps mentioned above. After the operation is finished, it will be checked through the netstat command. It is found that port 139 is no longer listening.
Release port 445
- Open the running window by “WIN+R” and enter:
services.mscOpen the service management interface:
- Find the Computer Browser service, double-click the service to open the property interface, change “Start Type” to disable, and click “Stop”:
- Find Server Service:
- Double-click the Server service, open the property interface, change “Start Type” to disable, and click “Stop”:
- Restart the system after setting up.
- After the reboot is complete, it will pass again.
netstat -a|findstr LISTENINGThe command checks the port usage and finds that port 445 has been released successfully.
Install WSL and Ubuntu distributions
- In the “Programs and Functions” management interface, click “Enable or Close Windows Functions” and in the “Windows Functions” list, check “Windows Subsystem for Linux” (Note: Different Windows versions may have different names, I tried version 17134.191)
- Restart the system, then open the “Microsoft Store” application mall, and enter it in the search bar
- Select Ubuntu 16.04 from the search results (other distributions should be OK, this test is Ubuntu), and install it:
- The installation process is faster, thanks to the broadband at home:
- After installation, you can find Ubuntu in the Start Menu:
- Click on the icon to open the console, or through the “Run” interface, first open the “CMD” window, and then through
bashCommand into Ubuntu’s command line mode:
Install the Samba service and configure it
Install Samba services:
- Upgrade apt-get tool first, command:
sudo apt-get update：
- Install Samba package, command:
sudo apt install samba：
Configure Samba services:
Here is just a configuration example. Let the service run with the simplest configuration. If you need to know more about the configuration of samba service, it is recommended to find more strategies online.
- Open the configuration file and edit it:
sudo vi /etc/samba/smb.confHere, a user private directory is configured
homesAnd a shared directory that everyone can read and write
shareThe corresponding directories on windows are
C:\private\username>As well as
The details of the configuration file are as follows:
[global] workgroup = WORKGROUP server string = jamieli-vm log file = /var/log/samba/log.%m log level = 0 max log size = 1000 security = user map to guest = Bad User passdb backend = smbpasswd smb passwd file = /etc/samba/smbpasswd username map = /etc/samba/smbusers guest account = nobody os level = 64 [homes] comment = user private path = /mnt/c/private/%u read only = No [share] comment = home share guest ok = Yes path = /mnt/c/share read only = No public = Yes
- Write the configuration file, because it is user security mode, so you need to add an SMB user, command:
sudo smbpasswd -a jamieliAnd enter the password:
- Start the smbd service,
sudo service smbd start：
- Looking at the temporary port usage in the Windows CMD window, we found that 139 and 445 were occupied by smbd services:
- Shared directories can be accessed by inputting local IP addresses through Windows local resource managers:
homesIn the directory, create a
testCatalog, you can see the corresponding
c:\private\jamieliIn the catalog, there is another same catalog:
- But through another machine access, we found that the access failed because of the firewall settings. In the next section, we will talk about how to configure the firewall:
Change firewall configuration
- In the “Run” interface, enter:
wf.mscStart the firewall configuration interface, select the “Inbound Rules” menu, and click “New Rules” on the right side:
- In the New Inbound Rules Wizard interface, select Port:
- Select “TCP” and input port “139,445”:
- Select Allow Connection:
- Call it “samba service” and click “finish”:
The final test:
- Access tests are conducted under Mac. CMD+K is used under Finder interface. Connect Server interface is opened and the server address configured before is entered.
- Enter the SMB account and password added through the smbpasswd command:
- Successful access to Samba services built through WSL will not affect the closure of the Ubuntu window at this time, and the service will continue to run in the background:
Through the process of service configuration, we have a general understanding of the use of WSL. Through this way, we can also deploy SSH and other services on Windows. It is a good news for those who are familiar with Linux system.
At the same time, through some tools, these services can also be packaged as Windows Services, which are automatically loaded when Windows starts.
Welcome to exchange with each other, Email: [email protected], WeChat: jamieli
NetBIOS over TCP/IP
Windows Subsystem for Linux Documentation