Sunday, September 27, 2009

Mounting SSHFS drives in Ubuntu

If you have a remote file server that you usually access via SSH, it is extremely simple to mount a directory on your 'local' filesystem (many applications will not allow you to 'browse' to network drives when looking for files to open).

You can simply add the following to /etc/fstab (remember to mkdir -p /path/to/dir and chown it to yourself):
# sshfs (see http://ubuntuforums.org/showthread.php?t=270806)
# The lines below will NOT mount the directories automatically - use: mount /path/to/dir (no need to sudo)
# To mount an arbitrary directory use: sshfs myserver:/remote/dir /path/to/dir
sshfs#marco@myserver:/media/data/shared/media /media/multimedia fuse users,auto 0 0
executing mount /media/multimedia will make all files on myserver located in /media/data/shared/media visible (assuming user `marco` on myserver has the right permission).

To avoid having to type a password each time you mount that directory, I recommend to create a public/private key-pair (there are a number of good tutorials about how to do this, you can see http://pkeck.myweb.uga.edu/ssh/) which you will need anyway for the next step.

Mounting automatically when starting Gnome

Unlike all the other mountpoints defined in /etc/fstab, and despite the `auto` option, the sshfs ones will not mount at boot (I suspect that that is because /etc/fstab are mounted as root, who is most definitely not allowed to ssh into my server - nor into yours, right??? - and in any event, even if it were, would result in a pretty useless situation as I would have either no access to the directories, or I would have to make them all world-readable, which is a BAD idea at the best of times....)

Using .bashrc is really not an option, as it requires me to launch a terminal session, and then leave it open for the duration of my Gnome session (closing it will automatically umount my drives).

I use Gnome (at home, and KDE at work - still can't quite decide which one is better, and I laugh at all those embroiled in this religious war.... they're both excellent windows managers and leave Windows in a trail of shame and uselessness) so I'd like to have the drives mounted when I start the session, without having to launch a terminall session.

The easy option is to create a simple shell file in my ~/bin directory:
#!/bin/sh
# Mounts external drives on sshfs (not mounted automatically from /etc/fstab)

mount /media/multimedia
mount /media/www

and add it to the session startup scripts for the Gnome session: System > Preferences > Startup Applications

If you now log out and log back in (no need to reboot) you will find your sshfs mountpoints proudly showing in the local directory.

You may also want to consider changing the 'auto' option back to 'noauto' should speed up the boot process.