Jun 07 2007

HOWTO: Mount a remote folder using sshfs (fuse) on Ubuntu Linux

I needed to mount a remote folder on another computer across the net into my local linux filesystem on my Ubuntu 7.04 (Feisty) box. Here’s how I did it.

sudo apt-get install sshfs
sudo mkdir -p /mnt/sshfs/homebox

Make an entry in your /etc/fstab something like this: (Change ‘myname’, server and path to match your location. You also need to change the uid= and gid= numbers from 1000 to whatever number was assigned to your local user ID. See /etc/passwd and /etc/group.)

# <file system>       <mount point>         <type>  <options>
sshfs#myname@www.myhome.com:/home/myname    /mnt/sshfs/homebox    fuse    comment=sshfs,users,noauto,uid=1000,gid=1000,allow_other,reconnect,transform_symlinks 0 0

Next, you should put the following in /etc/fuse.conf (create the file if it doesn’t exist):

user_allow_other

Add yourself to this group.

sudo adduser YOUR-USERNAME fuse

Log out and back in again to grab the new group permissions.

Then you can manually mount your remote drive like this:

mount /mnt/sshfs/homebox

You need to authenticate each time unless you use passwordless ssh keys (but that’s another topic.) If you do have passwordless ssh keys working and want to auto-mount this drive whenever the network connection is established then read on …

You should have a pair of directories called /etc/network/if-up.d and /etc/network/if-down.d. Scripts in the first directory will be executed when the computer connects to the network, and scripts in the second one will be executes when it disconnects. Editing files in these directories requires administrator privileges. Create the following two files:

/etc/network/if-up.d/mountsshfs

<pre> #!/bin/sh # Not for loopback! [ "$IFACE" != "lo" ] || exit 0 # umount them first mounted=`grep 'sshfs#' /etc/mtab | awk '{ print $2 }'` [ -n "$mounted" ] && umount -l $mounted # now mount for mountpoint in `grep 'comment=sshfs' /etc/fstab | awk '{ print $2 }'`; do mount $mountpoint & done </pre>

/etc/network/if-down.d/umountsshfs

<pre> #!/bin/bash # Not for loopback! [ "$IFACE" != "lo" ] || exit 0 # umount all sshfs mounts umount -l `grep 'sshfs#' /etc/mtab | awk '{ print $2 }'` </pre>

Now make both files executable:

sudo chmod +x /etc/network/if-up.d/mountsshfs /etc/network/if-down.d/umountsshfs

That should be it! If your shares are configured as above, they should now automatically mount and unmount with your network. Try disconnecting and reconnecting.

No Comments

No comments yet.

RSS feed for comments on this post.

Leave a comment