Samba File Share Over SSH Tunnel· 3 minute read
This is not a post about setting up Samba shares. If that is what you are looking for then I can recommend the following book; Using Samba: A File & Print Server for Linux, Unix & Mac OS X.
Sometimes you need to be able to access a remote Samba server in a secure manner from a Windows machine. This is a relatively simple procedure on an XP SP3 machine like mine linking into an Ubuntu server pre setup with Samba file sharing.
Windows is a little bit annoying as it binds all filesharing operations to port 139 so you cannot have more than one filesharing system in operation at once. If you were to tunnel directly across to your Samba server it would bang heads with the Windows filesharing layer. You could just disable file sharing in Windows but that is an in elegant method and you may need access to both Windows and remote Samba shares. So we will need to setup a new loopback adapter with a local ip address that we can tunnel Samba request through thus allowing Windows filesharing to operate normally along side Samba. This effectively makes Windows think that it is accessing Samba shares on a seperate machine whereas a tunnel usually acts as a port on the local machine.
Add the Loopback Adapter to the Windows client machine
- Open up the Add Hardware control panel (Start > Control Panel > Add Hardware)
- Click next and wait for the annoying wizard to finish hunting around your system
- Choose ‘Yes, I have already connected the hardware’
- Then scroll to the bottom of the ‘Installed hardware’ list box and choose ‘Add new hardware device’
- Now choose ‘Install the hardware that I manually select from a list (Advanced)’
- Select ‘Network adapters’
- Under ‘Manufacturer’ you want ‘Microsoft’
- For ‘Network Adapter’ choose ‘Microsoft Loopback Adapter’
- You may have a to wait a little while for the adapter to be fully installed
Set the Loopback Adapters Configuration
- Pull up the adapters properties dialogue (Start > Control Panel > Network Connections and then right click on the adapter and choose properties)
- Disable ‘File and Printer Sharing for Microsoft Networks’
Highlight ‘Internet Protocol (TCP/IP)’ and click the ‘Properties’ button
Choose ‘Use the following IP address’
- Enter ‘10.0.0.1′ for ‘IP address’
- Enter ‘255.255.255.0′ for ‘Subnet mask’
Click the ‘Advanced’ button and on the ‘WINS’ tab
- Enable ‘Enable LMHOSTS Lookup’
- Check ‘Disable NetBIOS over TCP/IP’
You will now need to restart you computer even though Windows does not prompt for this step
Configure the SSH Tunnel
- On your PuTTY session configuration dialogue choose Connection > SSH > Tunnels
- Check/enable ‘Local ports accept connections from other hosts’
- In ‘Source port’ enter ‘10.0.0.1:139′
- In ‘Destination’ enter ‘localhost:139′ (127.0.0.1:139 did not work for me)
Test and Map the Connection
In the Run command console (Start > Run) enter
\\10.0.0.1 and you should be presented with file explorer window containing the contents of your Samba share.
So if that worked we are ready to roll, but you can give your Samba share ’server’ a more friendly name by opening
C:\WINDOWS\system32\drivers\etc\hosts in your favourite editor (Vim in my case). Scroll to the bottom and enter the following
\\10.0.0.1 samba. You can now access
//samba in the same way we did above via the Run dialogue. If you have assigned the loopback device to a different subnet then you will need use the lmhosts file in the same directory instead – please see Microsoft KB Article Q105997.
Now you can Map the Samba share like any other by using the ‘Tools’ menu in a Windows file explorer window. In the ‘Folder’ input enter \\samba or for a home directory called simon
\\samba\simon (you must have enabled home directory sharing in your Samba smb.conf (/etc/samba/smb.conf)).