The joy of sshfs

[Tweet : ADN : nvALT]

I know a lot of web developers still use FTP clients to work on remote servers. I prefer to have a local dev install and use scp, rsync and git to push changes whenever it’s feasible. When I can’t do that, though, I use sshfs.

sshfs (SSH Filesystem) allows me to mount a remote server over SSH on my local filesystem, very similar to the way ExpanDrive does it (using FUSE). In addition to web design work, this can be great for system administration and other applications where you have to work on a remote server where you don’t a full dev environment set up. You get to use your own tools on your local machine while working directly with the files on the remote, all with the security of SSH.

If this sounds interesting, here’s the easiest way to get started. Install homebrew (if you haven’t already), then use it to install fuse4x and sshfs:

brew install fuse4x sshfs

Side note:

You can grab a binary installer for Fuse4X and manually build sshfs if you don’t want to deal with Homebrew, but for ease of install/uninstall, you really can’t beat brew.

There’s also a new project called Fuse for OS X that is supplanting Fuse4X, but the above combination is perfectly stable for now and is — to the best of my knowledge — the path of least resistance for getting sshfs up and running quickly.

Make a folder called “mounts” in your home folder (or whatever/wherever you want to mount the filesystems, but you’ll need to modify the following scripts). Add rmount and rumount below to your ~/.bash_profile to be able to quickly mount and unmount remote directories locally:

# Remote Mount (sshfs)
# creates mount folder and mounts the remote filesystem
rmount() {
	local host folder mname
	[[ ${1%:} == ${host%%:*} ]] && folder='' || folder=${1##*:}
	if [[ $2 ]]; then
		[[ "$mname" == "" ]] && mname=${host%%:*}
	if [[ $(grep -i "host ${host%%:*}" ~/.ssh/config) != '' ]]; then
		mkdir -p ~/mounts/$mname > /dev/null
		sshfs $host$folder ~/mounts/$mname -oauto_cache,reconnect,defer_permissions,negative_vncache,volname=$mname,noappledouble && echo "mounted ~/mounts/$mname"
		echo "No entry found for ${host%%:*}"
		return 1

# Remote Umount, unmounts and deletes local folder (experimental, watch you step)
rumount() {
	if [[ $1 == "-a" ]]; then
		ls -1 ~/mounts/|while read dir
			[[ $(mount | grep "mounts/$dir") ]] && umount ~/mounts/$dir
			[[ $(ls ~/mounts/$dir) ]] || rm -rf ~/mounts/$dir
		[[ $(mount | grep "mounts/$1") ]] && umount ~/mounts/$1
		[[ $(ls ~/mounts/$1) ]] || rm -rf ~/mounts/$1

These functions work with any host set up in ~/.ssh/config (key pair access preferred, especially for scripting mounts). Say you have a section in your ~/.ssh/config:

host mm
  User monster

and you have a key pair set up for password-less entry. Now you can just type rmount mm to mount the entire filesystem in ~/mounts/mm (the script will auto-generate the folder name based on the host or directory name). Typing rmount mm:/Users/monster would mount only the Home folder of a co-located Mac mini in ~/mounts/monster. Running rumount monster un-mounts the remote folder and removes the local mount point.

Why do this? Because, now you get all of the benefits of working on your own system. It’s as secure as SSH/SFTP but with access to your local environment and applications, and there’s no need to download each file you want to work on and wait for it to sync back up. Yes, it’s a bit slower (a bit determined by bandwidth, of course) than your local filesystem, but the direct tunnel keeps things moving nicely1.

Some of the things I love about sshfs vs. SSH or an FTP client:

  • subl/mate (no rmate or local listener required)
  • qlmanage -p (Quick Look from the command line)
  • quick file copy/move between filesystems (no scp, rsync or reverse tunneling needed)
  • pbcopy and pbpaste (clipboard interchangeability)
  • Finder access, because…

There, did I sell you2?

By the way, ExpanDrive for Mac just updated for the first time in quite a while. If you want all of this goodness in a GUI instead of the Terminal, check it out. Happy sshfs-ing.

  1. There are a few things you can do to speed up any SSH connection, including turning on compression and setting up “master sessions.” The post 9 Awesome SSH Tricks is a great place to start.

  2. Please note that I will not be compensated in any way if you start using sshfs. I don’t think anyone will. I am not invested in your future, I just want the best for you. Because you’re special. Stop it.