Blog - Samba and Bash on Ubuntu on Windows

Added on Saturday, 2017-06-17 20:14 CEST in category Programming
Besides being able to open remote Mutt attachments locally, it would be handy to be able to access all the files on my server locally. I could of course use FTPS/SFTP or smth. similar, but I would have to connect every time, and file timestamps may not be kept the same when copying files over. Instead I put some work into tunneling Samba over SSH, so that my server is accessible via a locally mapped network drive.

Most of this is based on similar instructions for Windows 7, except under Windows 10 things are a bit easier.

Adding a loopback adapter

The following adds a loopback adapter, which is needed to keep normal Windows file sharing working on the main network adapter.
  • Run hdwwiz.exe, then click Next, Install the hardware I manually select from a list (Advanced), Next, (wait a bit), Microsoft => Microsoft KM-TEST Loopback Adapter => Next => Next => Finish.
  • In the Control Panel open the Network and Sharing Center, then select Change adapter settings.
  • Open the properties of the newly added loopback adapter.
  • Disable: Client for Microsoft Networks, File and Printer Sharing for Microsoft Networks, Internet Protocol Version 6 (TCP/IPv6).
  • Select Internet Protocol version 4 (TCP/IPv4) and click Properties.
  • Give the adapter the IP address 10.255.255.1 (or smth. similar) and subnet mask 255.255.255.0; the other fields can remain empty.
  • Press Advanced and change the Interface metric to 9999.
  • On the WINS tab, select Disable NetBIOS over TCP/IP.

Enabling port forwarding on the loopback adapter

In older versions of Windows it was necessary to disable file sharing on boot, and then enable it again later. By Windows 10, it's possible to set up a delayed start instead:
  • Run services.msc and select Server.
  • In its properties, select Startup type: Automatic (Delayed Start).
This allows for another service to take port 445 before Windows assigns normal file sharing to every network adapter. To do so:
  • Open a command prompt as administrator.
  • Set up the following (persistent) proxy (using unprivileged port 44445):
    netsh interface portproxy add v4tov4 listenaddress=10.255.255.1 listenport=445 connectaddress=10.255.255.1 connectport=44445
    Check if it works using:
    netsh interface portproxy show v4tov4
  • After a reboot, run:
    netstat -an | find ":445 "
    It should explicitly show it's listening on 10.255.255.1, not on 0.0.0.0. If it doesn't do that, i.e., it listens on 0.0.0.0 after all, then a timing issue starts the Server too early anyway, despite the Delayed Start. In that case you could consider disabling the Server altogether, which fixed the issue for me (but also disabled file/printer sharing).

Setting up the tunnel

Add the following line to ~/.ssh/config in Bash:
Host nieko.net
  LocalForward 10.255.255.1:44445 localhost:445
This makes SSH bind to the unprivileged port from above, so no root permissions are needed.

Again, after connecting via SSH you can check if port 445 is indeed listening by running in a command prompt as administrator:
netstat -an | findstr ":445"

Connecting to the Samba share

In Explorer you can now access \\10.255.255.1\, and by right-clicking the desired share map it to a network drive.

Since I always have SSH open anyway, I can now always easily access my server's files :)