Securely mount remote unix directory using SSHFS

sshfs

There are times when your work environment is setup such that your dev environment is setup on another box and you have to edit code on that code a quick an easy solution is to login to the box, over ssh and update the code over vi. However most people wouldn’t want to leave the luxury of leaving their favorite text editor and working on the unfriendly text based editor.*

So on a mac, you can mount a remote file system as a drive using a free software called Macfusion which is awesome.

On linux if you just want to edit a couple of files, you have the option accessing files over sft provided by nautilus.

Places -> Connect To Server and then choose sftp for Service Type.

It gets tricky when you want to be able to load the entire directory inside a code editor and work as if you were working locally. Using macfusion allows you to do that it mounts the files as a drive. Doing it on Linux requires a little more work.

Get the sshfs and fuse code packages and do the regular configure, make and sudo make install on it.
The following steps are for the redhat enterprise settings but can be adopted for your distro.

$ wget http://superb-west.dl.sourceforge.net/sourceforge/fuse/fuse-2.6.5.tar.gz
$ tar -zxvf fuse-2.6.5.tar.gz
$ cd fuse-2.6.5
$ ./configure
$ make
$ sudo make install

Once compiled and installed the dynamic linker needs to be run so that the new libraries are added to the search path.

$ sudo vi /etc/ld.so.conf.d/fuse.conf

add /usr/local/lib to the file and then run the linker.

$ ldconfig

With that done, its time to compile and install sshfs on your box.

$ wget http://easynews.dl.sourceforge.net/sourceforge/fuse/sshfs-fuse-1.7.tar.gz
$ tar -zxvf sshfs-fuse-1.7.tar.gz
$ cd sshfs-fuse-1.7
$ ./configure
$ make
$ make install

Now to mount the directory this is what you would need to do.

$ mkdir ~/localdev
$ sshfs -o port=<place_your_port_here_if_non_standard> rpandit@devbox.dom ~/localdev

The user mounting should obviously have write permission on the directory being used. Now you can browse the directory like your local directory and any changes done to files are automatically synced to the remote ssh file system.

Now the only other thing remaining is un-mouting the file, which can be done with one of the two commands.

$ fusermount -u ~/localdev

or

$ umount -u ~/localdev

Further Reading:

* Having called it unfriendly, its a really nice tool to use and if I wasnt lazy enough to read the manuals and remember the shortcut keys, I would have preferred using vi.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.