In Linux, SSHFS or the SSH File System is used to mount and interact with directories and files that are located on a remote server. Let’s take a quick look at how to use this file system.
In large software organisations, it is very common to have separate development and build machines. Often, time is spent copying changes from the dev machine to the build machine. This becomes annoying when there are numerous files. Wouldn’t it be great if we could use a remote directory as a local one? It is very easy to do this in GNU/Linux and this article tells you how.
Overview
SSHFS is a fuse file system based on the SSH protocol and one of its big advantages is that it has to be installed on the client side only. No installation or configuration is required on the server side. The only requirement is that the openSSH server should be installed on the server side. In this article, we’ll discuss the usage and various features of SSHFS.
Installation
Installing SSHFS is pretty straightforward – it’s just like installing other packages. If you are using a Debian based GNU/Linux distro, such as Debian, Mint or Ubuntu, then execute the following commands:
$ sudo apt-get update $ sudo apt-get install sshfs
If you are using RPM based GNU/Linux, such as Red Hat or CentOS, then execute the following commands:
$ sudo yum update $ sudo yum install fuse-sshfs
Mounting a remote directory
The syntax of the SSHFS command is:
sshfs [user@]host:[directory] mountpoint [options]
To mount a remote directory in GNU/Linux, execute the following command:
$ sshfs jarvis@192.168.10.20:/home/jarvis/src /mnt
In the above command:
- jarvis is a user
- 192.168.10.20 is a remote host IP address
- /home/jarvis/src is a directory present on the remote host
- /mnt is a mount point
Configuring a passwordless SSH mount
In the above method, each time you mount a directory, it will ask for credentials. We can avoid this by configuring a passwordless SSH between these two machines. To do this, execute the following commands:
$ ssh-keygen -t rsa $ ssh-copy-id -i ~/.ssh/id_rsa.pub jarvis@192.168.10.20
In the above example:
- The first command creates RSA key pairs
- The second command copies the RSA public key to the remote host
Unmounting the directory
The unmount process is exactly like unmounting regular directories. To unmount a directory, execute the following command:
$ umount /mnt
…or:
$ fusermount -u mountpoint
Persistence directory mounting
If a directory is mounted manually, then the mount point will not be persistent across machine reboots. To create this persistence, we can configure auto-mount during boot time. To do this, a passwordless SSH has to be configured between two machines, as explained earlier.
The following entry must be added to the /etc/fstab file:
sshfs#jarvis@192.168.10.20:/home/jarvis/src /mnt fuse defaults 0 0
In the above command:
- sshfs# indicates that the mount must be performed using SSHFS
- jarvis is a user
- 192.168.10.20 is an IP of a remote host
- /home/jarvis/src is a directory from the remote host
- /mnt is a mount point
- fuse is a file system type
- defaults gives the options of the file system
- The last two parameters are related to backup and error checking
Miscellaneous options
This section discusses various SSHFS options that can be used to tune its default behaviour.
- -C: Enables compression
- -d: Enables debugging mode
- -f: Enables foreground mode
- -o cache=no: Disables caching mechanism
- -o cache_timeout=N: Sets cache timeout value in seconds
SSHFS is a very useful utility and it really makes life easier. Another advantage is that the behaviour of the remote mount point is identical to that of the local mount point. Users can create, delete, modify and perform other file system supported operations on this mount point.