By drupalmaster |

This article deals with a Linux-based cryptographic protocol file system known as sshfs. SSHFS stands for Secure Shell File System and is based on another well-known protocol called FUSE. So what is FUSE? FUSE is a userspace filesystem framework. It consists of a kernel module (fuse.ko), a userspace library (libfuse.*) and a mount utility (fusermount). FUSE allows the use of sshfs, which is a secure network file system using the sftp (Secure File Transfer) Protocol.

So, what are the uses of sshfs. In Linux, sshfs is used to mount a directory on a remote server to a directory on the local desktop. In this article, I'm going to show you how to mount a remote server directory to your local desktop to a directory you specify using sshfs.

Installing sshfs

The first thing we need to do is to determine if sshfs is installed and running on our local desktop Linux-based OS. I'm running AV Linux MX Edition for the purposes of this article and I will be connecting to a remote server, which is actually a Debian 10 Linux distro running on a Raspberry Pi Model 3B+. You can achieve this mount process on just about any remote server running Linux. 

Let's check to see if sshfs is installed. To accomplish this, open the Linux terminal on your desktop system and type the following command:

command -v sshfs

The stdout from running this command on my system looked like this:

command -v sshfs

Here, you can see that sshfs is already installed at /usr/bin/sshfs. If, on the other hand, there is nothing returned as stdout, then you will need to install it. If you're running an Ubuntu- or Debian-based Linux distro--like AV Linux MX Edition--then you can use the command:

$ sudo apt install sshfs

If you are running Fedora or Alpine Linux, then the command will look like this:

$ sudo dnf install fuse-sshfs

Using sshfs to Mount a Remote Directory

Now that we have sshfs installed, let's put it to use and see how it allows us to mount a remote server directory onto a folder/directory on our local system. A nice thing about sshfs is that you don't have to have root or elevated privileges on the remote server to make this work. Here's how. 

But, first, before we can use sshfs, we need to make sure we can ssh into the remote server. To check this out, simply run the ssh command connecting to the remote server using the user account that you wish to connect to and the IP address or FQDN (Fully-Qualified Domain Name) of the server you're connecting to. If you have access to the remote server, you can obtain the IP address of that server by running:

$ ip a

You will be able to obtain the IP address rather easily. In my case, the IP address to my Raspberry Pi 3B+ is:

ip a

192.168.1.90. this is a fixed IP address that I assigned to that SBC (Single-Board Computer). The /24 in that address refers to the CIDR (Classless Inter-Domain Routing) notation that tells us that this IP address is on a Class C network. If you can't console into the remote server to obtain its IP address, then contact the network administrator for that server to obtain the information.

Next, run the ssh command to establish an ssh session on the remote server:

ssh into remote server

Here, I have used the user account of pi on that server to ssh into it at IP address 192.168.1.90. I was then prompted for a password for the user and, when entered correctly, I was allowed to connect an ssh session. 

Now, let's create a directory that we wish to mount locally on our desktop PC. To do this, I'm going to create a directory called filesDir, which, when created, will be empty.

creating files directory on remote server

Now that we have created the directory on the remote server which we wish to mount locally on our PC, let's return to the PC and create that directory we want to mount to. I'm going to call this folder remoteFilesDir

creating remote files directory on local system

After creating the remote server directory where files will be stored and the local directory we wish to mount the remote directory to on our local PC, let's run the sshfs command that will allow us to accomplish this. The command to mount the filesDir directory on the remote server to the remoteFilesDir directory on the local PC is the following, which should be executed from the local PC Linux terminal:

Note, when I ran the command to mount remoteFilesDir to the local mount folder of filesDir, I used the pi user account at the IP address of the remote server followed by a colon (:) and the absolute path to the mount directory on the local PC. This was followed by a space, then the absolute path to the remote directory being mounted. After hitting the Enter key to execute the sshfs command, I was prompted for the pi user's password. Entering that password and hitting the Enter key returned me to a prompt with no intervening stdout. This is a good sign that the command executed properly.

We can verify that the mount of the remote directory on the local directory was successful, we run the following command:

verifying mount

As we can clearly see, running the mount command produced stdout that shows a mount of the remote server directory onto the local PC directory was successful.

Testing Remote Directory Access In The File System

Now, finally, to test that we have access to the remote server directory and its contents from our local mounted directory in the file system on the PC, let's return to the remote server and add an empty file called testFile.md to the remote directory of filesDir. Using the touch command while in that remote directory, I have created the testFile.md file now residing in the filesDir directory.

touching the directory

 Returning to the local PC, let's change directory into the remoteFilesDir directory and look at its contents:

contents of remote files directory

As shown above, the empty testFile.md file residing on the remote server is visible and accessible from the mounted directory on the local PC. 

Unmounting the Local PC-Mounted Directory

Now that we have access to the contents of the remote server directory we created earlier and which we mounted to the local PC's directory, how do we unmount the local PC directory? We could simply use the umount command:

$ sudo umount remoteFilesDir

But, there is a similar special command associated with SSHFS that will also let us accomplish the same thing. This is a dedicated command when running sshfs on your local system and you should use this command instead of umount to accomplish the unmounting of the local directory. That dedicated command is:

$ fusermount -u remoteFilesDir

However, when running this command in my distro of Linux, I received an error message that the local directory was not found in the /etc/mtab file on the system. This is a bug. So, we must resort to using the umount command anyway. Running:

$ umount remoteFilesDir

then following that up with the mount command to see if this directory is still mounted, we see from the stdout of that command that the remote server directory is no long mounted to our local directory:

unmounted local directory

If you have questions regarding this article, please send a comment by emailing me at dancalloway@hey.com. I'll respond directly to you as soon as possible.