Mounting remote filesystems using SSH

Posted by Steve on Wed 13 Oct 2004 at 16:49

OpenSSH can be used for many things, from connecting to remote hosts to transferring files securely. Thanks to a new kernel module shfs it is possible use it to mount remote filesystems securely.

Using SSH to mount a remote filessytem means that you can edit files on a remote machine by using local utilities which you might not necessarily have available remotely.

If you're using a KDE Desktop system you can directly browse remote filesystems by using the sftp protocol, as explained in this simple introduction.

This is great if the applications you are using are KDE based, but it doesnt help if are using GNOME or if you wish to edit files using a console based application.

Enter shfs, the ssh-file-system. shfs is a Linux kernel module which allow you to mount a remote filesystem using SSH as the transport.

To install is very simple on a Debian Unstable system.

First of all download the package source code and a couple of helper programs:

root@undecided:~# apt-get install shfs-source shfs-utils module-assistant 

(The module is downloaded as source code because it must be rebuilt to match the kernel you are currently running, just like the NVidia kernel module.)

Then use the module assistant to build the kernel module to match your local system. This works if you're running either a v2.4.x kernel or a newer v2.6.x one.

root@undecided:~# module-assistant build shfs

This will show you a blue screen with a progress bar, and status updates as it builds. Once this is done you can install it:

root@undecided:~# module-assistant install shfs
(Reading database ... 87702 files and directories currently installed.)
Preparing to replace shfs-module-2.4.27-1-k7 0.35-1+2.4.27-2 (using .../shfs-module-2.4.27-1-k7_0.35-1+2.4.27-2_i386.deb) ...
Unpacking replacement shfs-module-2.4.27-1-k7 ...
Setting up shfs-module-2.4.27-1-k7 (0.35-1+2.4.27-2) ...

You might see an error message like the one below after running the install step. It's harmless and can be ignored:

depmod: *** Unresolved symbols in /lib/modules/2.4.26-1-686/shfs/shfs.o

Then comes the moment of truth, we test it.

Create a mount point and attempt to mount a filesystem:

root@undecided:~# mkdir /mnt/remote
root@undecided:~# shfsmount skx@sun:/var/www /mnt/remote
Password: 
root@undecided:~# cd /mnt/remote/
root@undecided:/mnt/remote# ls
Effective-C++  analog     fwanalog    isoqlog     phpsysinfo  tor
Reference      calamaris  index.html  jeff_wayne  stats       uptimes.html

The filesystem is mounted and we can edit the files which are present there!

To unmount the filesystem run:

root@undecided:/mnt/remote# cd ..
root@undecided:/mnt# umount /mnt/remote 

If you wish to be able to run this after you've rebooted you will need to make sure the module is added to the list of modules to load when your machine boots.

To do this add shfs to the bottom of the file /etc/modules:

root@undecided:/mnt# echo 'shfs' >> /etc/modules

This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2004 Steve - please ask for permission to republish or translate.