How to use SSHFS to Mount Remote Directories over SSH

Introduction

Before we begin talking about how to use SSHFS to Mount Remote Directories over SSH, let's briefly understand – What is SSHFS ?

SSHFS, short for SSH File System, is a secure and convenient way to access and manage remote files. By using the SSH protocol for authentication and encryption, SSHFS allows you to mount a remote server's file system on your local machine.

This enables you to effortlessly browse and manipulate files as if they were on your own computer. With its simplicity and security, SSHFS is an invaluable tool for remote file management and collaboration.

In this tutorial, you will use SSHFS to Mount Remote Directories over SSH. We will also address a few FAQs on how to use SSHFS to Mount Remote Directories over SSH.

Advantages of SSHFS

  1. Secure: SSHFS utilizes the SSH protocol for secure authentication and encryption, ensuring the safety of your remote file transfers.
  2. Convenience: Mounting remote file systems locally allows for easy access and management of files as if they were on your own machine.
  3. Transparency: SSHFS seamlessly integrates remote files into your local file system, providing a transparent and familiar user experience.
  4. Efficiency: With SSHFS, you can efficiently transfer and manipulate files without the need for additional tools or complicated setups.
  5. Collaboration: SSHFS enables effortless sharing and collaboration by allowing multiple users to access and work on the same remote files simultaneously.

Installing SSHFS

Packages for SSHFS are available for all major operating systems, and installation is simple.

Installing SSHFS on Ubuntu and Debian

SSHFS can be found in the Ubuntu and Debian repositories by default. Type the below command to update the packages index and install the sshfs client:

sudo apt update
sudo apt install sshfs

Installing SSHFS on CentOS

To install sshfs on CentOS and other Red Hat derivatives, perform the following command:

sudo yum install sshfs

Installing SSHFS on macOS

Download the FUSE and SSHFS packages from the osxfuse site or use Homebrew to install the SSHFS client on macOS:

Installing SSHFS on Windows

WinFsp and SSHFS-Win are two packages that Windows users must install.

Mounting a Remote File System

The steps below are relevant to all Linux and macOS distributions.

The SSH user must be able to access the remote directory in order to mount it. The mount command for SSHFS looks like this:

sshfs [user@]host:[remote_directory] mountpoint [options]

The sshfs command reads the SSH Config File and applies the settings per host. If no remote directory is supplied, the remote user's home directory is used.

To mount the home directory of a user named "vegastack" on a remote computer with the IP address "192.168.121.121," for example, first create a directory that will act as a mount point, which can be anywhere:

mkdir ~/vegastackremote

Then mount the remote directory with the sshfs command:

sshfs linuxize@192.168.121.121:/home/vegastack /home/vegastack/vegastackremote

You will be asked for your user password. Create SSH keys and set up Passwordless SSH Login to avoid inputting the password every time you mount a remote directory.

You can now interact with distant server directories and files in the same manner that you can with local files. You can edit, delete, rename, and create new files and directories, for example.

If you wish to mount the remote directory permanently, update the /etc/fstab file on the local machine and add a new mount entry. As a result, when your system boots up, the remote directory will be mounted automatically.

Use fuse.sshfs as the filesystem type in /etc/fstab to mount a remote directory through SSHFS.

user@host:/remote/dir  /local/mountpoint  fuse.sshfs  defaults  0  0

Make sure you can connect to the remote computer using SSH key-based authentication before creating a persistent mount.

Mounting a Remote File System on Windows

Windows users can map a network disk to the remote directory on the SSH server using Windows Explorer.

Right-click "This PC" in Windows Explorer and choose "Map network drive." Choose a drive to mount to, then enter the remote user, server, and path in the "Folder" field in the following format:

\\sshfs\user@host[\PATH]

Because SSHFS-Win does not offer key-based authentication at the time of writing, the remote ssh server must be set to accept password-based authentication.

Check out the SSHFS-Win documentation for further information.

Unmounting a Remote File System

Use the umount or fusermount commands, followed by the directory where the file system was mounted (mount point), to remove it.

fusermount -u /local/mountpoint
umount /local/mountpoint

FAQs to use SSHFS to Mount Remote Directories over SSH

How do I unmount a remote directory mounted through SSHFS? 

To unmount a remote directory, simply use the "fusermount -u" command followed by the path of the directory you want to unmount. For example, "fusermount -u /mnt/remote".

How does SSHFS work? 

SSHFS works by establishing an encrypted SSH connection to the remote server and mounting the remote directory on your local machine as if it were a local file system.

Which operating systems support SSHFS?

 SSHFS is compatible with various operating systems, including Linux, macOS, and Windows (with the help of third-party software like WinFSP or Dokan).

How secure is SSHFS? 

SSHFS ensures security through the SSH protocol, which provides strong authentication and encryption for your remote file transfers.

Can multiple users access the same remote directory simultaneously? 

Yes, SSHFS supports concurrent access, allowing multiple users to access and work on the same remote directory simultaneously.

What are the advantages of using SSHFS over other file transfer methods?

 SSHFS offers convenience, transparency, security, efficient file transfers, and simplified collaboration, making it a popular choice for remote file management.

Do I need administrative privileges to mount a remote directory using SSHFS? 

Generally, administrative privileges are required for mounting remote directories using SSHFS, although it may vary depending on your system's configuration.

Conclusion

You learnt how to use SSHFS to mount a remote directory through SSH in this tutorial. This is useful if you wish to utilize your local machine applications to communicate with remote files.

In your terminal, type man sshfs for a complete list of sshfs options.

You may also wish to set up an SFTP Chroot Jail environment and alter the default SSH port to give an extra degree of security to your server by restricting user access to their home directory.

If you have any queries, please leave a comment below and we’ll be happy to respond to them.S