How to use SSHFS to Mount Remote Directories over SSH

How To Use SSHFS To Mount Remote Directories Over SSH

This tutorial will show you how to install the SSHFS client on Linux, macOS, and Windows and how to mount a remote directory.

SSHFS (SSH Filesystem) is a filesystem client based on FUSE for mounting remote directories over an SSH connection. SSHFS is using the SFTP protocol, which is a subsystem of SSH and it is enabled by default on most SSH servers.

When compared to other network file system protocols such as NFS and Samba the advantage of SSHFS is that it does not require any additional configuration on the server side. To use SSHFS you only need SSH access to the remote server.

Because SSHFS uses SFTP , all transmitted data between the server and the client must be encrypted and decrypted. This results with a slightly degraded performance compared to NFS, and higher CPU usage on the client and server.

 

Installing SSHFS

SSHFS packages are available for all major operating systems and the installation is pretty straightforward.

 

Installing SSHFS on Ubuntu and Debian

SSHFS is available from the default Ubuntu and Debian repositories. Update the packages index and install the sshfs client by typing:

sudo apt updatesudo apt install sshfs

 

Installing SSHFS on CentOS

On CentOS and other Red Hat derivatives run the following command to install sshfs:

sudo yum install sshfs

 

Installing SSHFS on macOS

macOS users can install the SSHFS client by downloading the FUSE and SSHFS packages from the osxfuse site or via Homebrew:

brew cask install osxfusebrew install sshfs

 

Installing SSHFS on Windows

Windows users need to install two packages, WinFsp and SSHFS-Win.

  • WinFsp .

  • SSHFS-Win .

 

Mounting a Remote File System

The following instructions are applicable for all Linux distributions and macOS.

To mount a remote directory the SSH user needs to be able to access it. The SSHFS mount command takes the following form:

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

The sshfs command will read the SSH Config File and use per host settings. If the remote directory is not specified, it defaults to the remote user home directory.

For example, to mount the home directory of a user named “nexonhost” on a remote host with IP address of “192.168.121.121”, first create a directory that will serve as a mount point, it can be any location you want:

mkdir ~/nexonhostremote

Then use the sshfs command to mount the remote directory:

sshfs nexonhost@89..45.113.112:/home/nexonhost /home/nexonhost/nexonhostremote

You will be prompted to enter the user password. To avoid typing the password each time you mount the remote directory generate SSH keys and setup Passwordless SSH Login .

Now you can interact with the directories and files located on a remote server in the same way as you do with the local files. For example, you can edit, delete, rename or create new files and directories.

If you want to permanently mount the remote directory you need to edit the local machine’s /etc/fstab file an add a new mount entry. This way when your system boot up it will automatically mount the remote directory.

To mount a remote directory over SSHFS from /etc/fstab, use fuse.sshfs as the filesystem type.

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

 

When creating a persistent mount make sure you can connect the remote host using the SSH key-based authentication.

 

Mounting a Remote File System on Windows

Windows users can use the Windows Explorer to map a network drive to the remote directory on the SSH server.

Open Windows Explorer, right-click on “This PC” and select “Map network drive”. Choose a drive to mount at and in the “Folder” field enter the remote user, server, and path in the following format:

\\sshfs\user@host[\PATH]

 

At the time of writing this article SSHFS-Win doesn’t support key-based authentication so the remote ssh server needs to be configured to accept password-based authentication.

For more detailed information check the SSHFS-Win manual.

 

Unmounting a Remote File System

To detach a mounted file system, use either the umount or fusermount command followed by the directory where it has been mounted (mount point):

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

 

Conclusion

In this guide, you have learned how to use SSHFS to mount a remote directory over SSH. This can be useful when you want to interact with the remote files using your local machine applications.

For a complete list of the sshfs options, type man sshfs in your terminal.

You may also want to restrict user access to their home directory by setup up an SFTP Chroot Jail environment and change the default SSH port to add an extra layer of security to your server.