How to Use SSHFS to Mount Remote Directories Over SSH?

SSHFS is a file system that enables users to mount a remote directory over an SSH connection. It is a convenient way to access remote files using your local file system as if they were located on your local machine.

This guide will demonstrate various methods to mount remote directories over SSH. The content of this article is as follows:

Here’s how to mount a remote directory on a Linux machine.

Prerequisite: Install the SSHFS

To use SSHFS, you will need to have an SSH client, and the SSHFS client installed on your local machine. On different linux distributions, users can install SSHFS according to the package manager via the below commands:

$ sudo apt install sshfs            # Debian or Ubuntu
$ sudo yum install sshfs            # CentOS and Red Hat

Using SSHFS Mount Directory from Local to Remote Machine

To mount the directory from the local to remote machine over the SSH, the step by step procedure is provided below:

Step 1: Create a Local Directory

To create a local directory that will serve as the mount point (logically linked to another file system) for the remote directory. For instance, a directory is created named “office” as below:

$ mkdir ~/office

Step 2: Mount Remote Directories over SSH

Use the “sshfs” command to mount the remote directory “rfolder” on the local mount point “office”. To do so, specify the username and hostname of the remote machine as below:

$ sshfs roger@ubuntu:rfolder ~/office

It requires the authentication of the remote machine.

Step 3: Verify Mount Directory

To verify the mounted directory and content, use the “cd” command can utilize by specifying the directory:

$ cd office

The output confirms the “files.txt” has been transferred from the remote to the local machine.

Using SSHFS Mount Directory From Remote to Local Machine

To mount the directory from the remote machine on the local, specify the username and hostname of the local machine. The “testmount” directory is located in the home directory of the remote machine having some content inside it:

$ sshfs itslinuxfoss@ubuntu:office ~/testmount

The above code requires the authentication of the local machine.   

Note: To access the remote files, you can simply navigate to the local mount point using the “~” sign.

Verify in Local Machine

To verify the mounted directory with content, utilize the “cd” command by specifying the name of the directory in the local machine:

$ cd office

The output confirms that the “testmount” directory has been mounted with the “office” directory.

That is all from the mounting directories.

Bonus Tip: Unmount Remote Directory

To unmount the remote directory, use the “fusermount” command with the -u option, followed by the local mount point “testmount”:

$ fusermount -u /home/itslinuxfoss/testmount

The successful execution of the above script confirms the specific directory has been unmounted.

Note: You will need SSH access to the remote machine and the appropriate permissions to access the remote directory.

Conclusion

The “sshfs” command can mount the remote directory by specifying the local directory (mount point). This command requires the “username” and “hostname” of the remote machine. Similarly, users can mount a directory from a remote to the local machine over SSH. This article has provided all possible ways for mounting the remote directory using SSHFS.