Mapping network drive via SSH on Linux, Windows and Mac

Problem: Mapping a remote network drive via SSH on Linux, Windows and Mac OS.

Solution:

Tested on Windows XP-SP3/7, Linux (Redhat/Fedora, Ubuntu), and Mac OS (Snow Leopard 10.6).

Update (2/23/2015): Hopefully newer OS versions make this process easier. If SSH tunneling is sluggish, try setting the compression level and/or cipher options.

For Linux  (Redhat/CentOS/Fedora, Ubuntu, Debian), you can use SSH directly. The option is likely available through the GUI; if not, it is fairly easy to pull off in the terminal. (see sshfs)

For Windows or Mac drive mapping, SSH is not enough; Samba service is required. We tunnel it through SSH to keep the connection secure.

For Windows XP-SP3:

If you have ‘File and Printer Sharing’ enabled for your network device, Windows will not allow you to tunnel from another source to the samba port 139 for that network device.  Thus, assuming that you will likely want to keep both your Windows and Samba shares, you will need to create a loopback device and tunnel port 139 to this new device. The following are steps for doing this (derived from this posting):

First, add the Loopback Adapter to the Windows client machine:

  1. Open up the Add Hardware control panel (Start > Control Panel > Add Hardware)
  2. Click next and wait for search to conclude nothing was found.
  3. Choose ‘Yes, I have already connected the hardware’
  4. Scroll to the bottom of the ‘Installed hardware’ list box and choose ‘Add new hardware device’
  5. Now choose ‘Install the hardware that I manually select from a list (Advanced)’
  6. Select  ‘Network adapters’
  7. Under ‘Manufacturer’ you want ‘Microsoft’
  8. For ‘Network Adapter’ choose ‘Microsoft Loopback Adapter’. Continue with installation.
  9. Open the adapters properties dialogue (Start > Control Panel > Network Connections and then right click on the adapter and choose properties)
  10. Uncheck all items listed except for ‘Internet Protocol (TCP/IP).
  11. Highlight ‘Internet Protocol (TCP/IP)’ and click the ‘Properties’ button
  12. Choose ‘Use the following IP address’
  13. Enter ‘10.0.0.1′ for ‘IP address’ (or any other valid and unused local address: 10.x.x.x, 172.16.x.x, and 192.168.x.x). This will be the IP of your loopback adapter; make note of it for later.
  14. Enter ‘255.255.255.0′ for ‘Subnet mask’
  15. Click the ‘Advanced’ button and on the ‘WINS’ tab
  16. Enable ‘Enable LMHOSTS Lookup’
  17. Check ‘Disable NetBIOS over TCP/IP’
  18. Restart you computer (even though Windows does not prompt for this step)

Next, configure the SSH Tunnel

  1. Download and Open PuTTY.
  2. Using the configuration window on the left, navigate to Connection > SSH.
  3. Enable two boxes “Don’t start a shell or command at all” and “Enable Compression”.
  4. Navigate to Connection > SSH > Tunnels
  5. For ‘Source port’, enter ‘10.0.0.1:139′ (replacing IP as needed if a different loopback address was chosen before in step 13). For ‘Destination’, enter ‘localhost:139′. Click the ‘Add’ button.
  6. Navigate to “Sessions” (the initial screen) to enter the host information and save the connection profile (so you don’t have to reconfigure again).
  7. Start Putty session.

Finally, map the network drive:

  1. Open ‘My Computer’ and choose ‘Map network drive’ from the menu bar.
  2. For ‘Folder’, enter ‘\\ssh-host\samba-folder’, where ssh-host and samba-folder are substituted accordingly. If required, choose ‘Connect using different credentials’.

 

For Windows 7:

A different solution is required for Windows 7. You don’t need a loopback adapter to use both your Windows and Samba shares, but instead you will have to do some service management. You can do this service management manually (based on this post and explained below), or in an automated way using batch scripts.

Firstly, Disable Windows “Server” service and Reboot.

  1. Right click on ‘Computer’, select ‘Manage’.
  2. Navigate to ‘Services and Applications’ > ‘Services’
  3. Locate service named “Server” and open properties.
  4. Set ‘Startup Type’ to Disabled, and choose the “Stop” button to stop the service.
    • Important: this will stop your Windows shares.
  5. Reboot your system.

Next, configure the SSH Tunnel

  1. Download and Open PuTTY.
  2. Using the configuration window on the left, navigate to Connection > SSH.
  3. Enable two boxes “Don’t start a shell or command at all” and “Enable Compression”.
  4. Navigate to Connection > SSH > Tunnels
  5. For ‘Source port’, enter ‘139′. For ‘Destination’, enter ‘localhost:139′. Click the ‘Add’ button.
  6. For ‘Source port’, enter ‘445′. For ‘Destination’, enter ‘localhost:445′. Click the ‘Add’ button.
  7. Navigate to “Sessions” (the initial screen) to enter the host information and save the connection profile (so you don’t have to reconfigure again)
  8. Start Putty session.

Finally, map the network drive and resume Windows “Server” server:

  1. Open ‘My Computer’ and choose ‘Map network drive’ from the menu bar.
  2. For ‘Folder’, enter ‘\\localhost\samba-folder’, where samba-folder is substituted accordingly. If required, choose ‘Connect using different credentials’.
  3. Return to ‘Properties’ screen for windows “Server” service (as done initially).
  4. Set ‘Startup Type’ to Manual, and choose the “Start” button to stop the service.
  5. You can now access both your windows and Samba shares.

Important: Before shutting down Windows, you may need to disable Windows “Server” service (same as initial step above) if you want to make use of your Samba share again after Windows is restarted. As you can tell, this can be annoying to do manually and to even remember; one can automate this process by adding two scripts to the Windows scheduler.

For Mac OS (Snow Leopard 10.6):

This section is derived from this posting .

Firstly, configure the SSH tunnel:

  1. Open a terminal window.
  2. Use the following command (sudo or root access required) to set up a loopback alias and tunnel the required Samba ports, filing in ‘user’ and ‘ssh-host’ accordingly:
    • sudo ifconfig lo0 127.0.0.2 alias up
      • where 127.0.0.2 is an alias for your loopback adapter. This IP can be any other acceptable and unused local address: 10.x.x.x, 172.16.x.x, and 192.168.x.x . If different is chosen, make sure to reflect that change within the next command.
    • sudo ssh -NL 127.0.0.2:139:localhost:139 -NL 127.0.0.2:445:localhost:445 [email protected]

Finally, map the network drive:

  1. Choose the “Connect to Server” option from the Finder menu bar.
  2. Connect to the following, filing in ‘user’ accordingly: smb://[email protected]
  3. Enter Samba credentials and continue.

Skriv et svar