Map a directory on a MyBook Live NAS to a Windows drive letter over the internet with SSHFS and Samba

February 3rd, 2013 Leave a comment Go to comments

We needed to be able to access a directory on a MyBook Live via a Windows drive letter and evaluated a few options.  Samba works well if the file server and client are on the same LAN, but we discovered it painfully slow when used via an SSH tunnel over the internet.  WebDAV is typically a good solution, but we found it unstable when used with the MyBook Live.  We settled on two techniques that we use in different situations.  The first technique is SSHFS and Samba - SSHFS for the internet portion and Samba for the LAN portion.  This works relatively well with only a moderate performance decrease.

This article describes our technique for setting up SSHFS and Samba on a Western Digital My Book Live NAS device.

Before we start, we recommend you update the MyBook Live's firmware.  We were originally using a version of its firmware that included Samba 3.2.5.  This is partially incompatible with SSHFS and causes unexpected behaviour including the inability to delete non-empty directories.  At the time of this writing, we're using firmware 02.32.05-046 which includes Samba 3.6.5 - this works properly.

First, navigate to your My Book Live's configuration and create a new empty share.  If you're not using a My Book Live, set up a shared directory that you have access to via your Windows computer.  For the purposes of this example, our shared directory is /shares/sshfs.

Next, we need to install sshfs on the My Book Live.  Note that you'll need to do this every time you upgrade its firmware, but it's quite easy:

Warning: Do not run apt-get upgrade as it can brick your device!
apt-get update
apt-get install sshfs
Warning: Do not run apt-get upgrade as it can brick your device!

We need to permit the allow_other option so that other (Samba) users will work properly:
echo user_allow_other >> /etc/fuse.conf
Before you mount the filesystem, we recommend setting ServerAliveInterval in ~/.ssh/config:
ServerAliveInterval 60
Mounting a filesystem is relatively simple, but since we're using Samba, we're going to do some extra work with permissions.  When you mount a filesystem with sshfs, the permissions are the same as the system the files are actually on.  Since your files probably aren't all world read/writable, things aren't likely to work properly.

Why not set things up so that files appear to be part of a group that all the Samba users will have access to?  In the case of the My Book Live, this group is called "share".
grep share /etc/group
Now that we know the gid of the share group is 1000, we can mount the filesystem.
sshfs /shares/sshfs -o allow_other -o umask=007 -o gid=1000
Let's explain the options.  -o allow_other allows other users to access the filesystem.  -o umask=007 sets permissions so that the owner and the group members have full access to the files and directories.  -o gid=1000 sets the group to the share group we identified earlier.  (Note that the actual file permissions will not be altered - this affects the client only.) You may wish to evaluate these options and be sure they are appropriate for your situation.

Now, users of any Windows computer with permission to access the sshfs share will be able to treat directory on as a network folder or drive letter.
~/directory# ls -lah
total 8.0K
drwxrwxrw- 2 user user 4.0K Feb  5 19:39 .
drwxr-xr-x 9 user user 4.0K Feb  5 19:39 ..
-rw------- 1 user user    0 Feb  5 19:40 test_file
C:\>net use s: \\nas\sshfs /persistent:yes
The command completed successfully.
 Volume in drive S is sshfs
 Directory of S:\
2013-02-05  07:39 PM    <DIR>          .
2013-02-05  07:39 PM    <DIR>          ..
2013-02-05  07:40 PM                 0 test_file
               1 File(s)              0 bytes
               2 Dir(s)  1,589,200,748,544 bytes free
If you wish to unmount the filesystem, use the following technique.  In this case, the share will appear empty.
fusermount -u /shares/sshfs
Once everything is working the way you want, perhaps you'd like to configure /etc/fstab so that the filesystem will be automatically mounted.  Unmount the filesystem if it's already mounted, then add the following to /etc/fstab: /shares/sshfs fuse allow_other,umask=007,gid=1000 0 0
When you're finished, run mount -a to remount the filesystem.

In testing this concept, we discovered relatively predictable results: since we access our server over the internet, speeds were slower than LAN speeds, but very usable.  We found it interesting that copying small files via SSHFS was faster than via SCP; perhaps because the SSH session was already established.

Until there's a stable open-source SFTP client that integrates with Windows Explorer, we think this is the best method for mapping a Linux directory to a Windows drive letter over the internet.

Update: We have noticed that sshfs may not recover if the internet connection is lost and then re-established.  If you've added sshfs to /etc/fstab, you may also want to use the following cron job:
*/5 * * * * mount | grep sshfs || mount -a


If you would like a reply to your comment, you must leave your email address! We receive dozens of questions every month from people who don't leave us with any way to contact them, so we have no choice but to ignore the question. We try to reply to as many questions as we can, if we know the email address of the person who asked the question. Thanks in advance for writing in :)

Allowed HTML: <b>, <i>, <em>, <strong>. All other < and > will be replaced with &lt; and &gt;.