How To Mount Remote File Systems Over SSH using SSHFS

SSHFS - Head Image

SSHFS (SSH Filesystem) is a powerful tool that allows users to mount remote file systems over SSH, providing a secure and convenient way to access remote files as if they were local. This guide will walk you through the process of using SSHFS to mount remote file systems, offering IT savvy customers and system administrators a practical solution for managing remote files efficiently.

What is SSHFS?

SSHFS leverages the SSH protocol, which is widely used for secure remote access, to create a seamless connection between local and remote file systems. By mounting a remote directory on your local machine, you can interact with remote files using your familiar local tools and applications, streamlining your workflow and enhancing productivity.  

Benefits of Using SSHFS

Using SSHFS offers several benefits:  

  • Security: It uses SSH for secure communication, ensuring that your data is encrypted during transfer. 
  • Convenience: Access remote files as if they were on your local machine, making it easier to work with remote data. 
  • Flexibility: It can be used on various operating systems, including Linux, macOS, and Windows. 

Prerequisites of SSHFS

Before you can start using SSHFS to mount remote file systems over SSH, there are a few prerequisites that need to be met. Ensuring these requirements are in place will help you avoid common issues and streamline the setup process.  

System Requirements of SSHFS

To get started, you need:  

  • A Linux, macOS, or Windows machine: SSHFS is compatible with all major operating systems. Each system has its own method for installing SSHFS, which will be covered in the next section. 
  • FUSE (Filesystem in Userspace): SSHFS relies on FUSE to function. Most Linux distributions come with FUSE pre-installed. For macOS and Windows, you will need to install FUSE separately. 

SSH Access to the Remote Server

You must have SSH access to the remote server you intend to mount. This includes:  

  • SSH Server Running on the Remote Machine: Ensure that the SSH server is installed and running on the remote machine. Most Linux servers have SSH installed by default. 
  • User Permissions: The user account you use to connect via SSH must have the necessary permissions to access the directories you want to mount. 
  • SSH Keys: For a more secure and convenient connection, it is recommended to set up SSH key-based authentication. This eliminates the need to enter a password each time you connect. 

For those managing multiple servers or needing reliable remote access, Contabo offers a range of VPS and Dedicated Server solutions that can be easily integrated. With Contabo’s robust infrastructure, you can ensure high performance and secure access to your remote file systems. 

With these prerequisites in place, you are ready to install SSHFS on your system. The next section will guide you through the installation process for various operating systems.

How to install SSHFS (Linux, Windows, macOS)

To use SSHFS, you need to install it on your local machine. The installation process varies depending on your operating system. Here’s how to install SSHFS on some popular operating systems: 

Installing SSHFS on Linux

For Linux systems, open a terminal to execute these commands. You’ll need to use `sudo`, which allows you to run commands with superuser privileges. When using `sudo`, you’ll be prompted to enter your password. 

Ubuntu and Debian

On Ubuntu, Debian, and their derivatives, install SSHFS using the apt package manager

sudo apt update
sudo apt install sshfs

Fedora and CentOS

For Fedora, CentOS, and other Red Hat-based distributions, use the dnf package manager: 

sudo dnf install fuse-sshfs

Arch Linux

Arch Linux users can install SSHFS using the pacman package manager: 

sudo pacman -S sshfs

Installing SSHFS on macOS

For macOS users, open Terminal and use Homebrew to install SSHFS: 

brew install sshfs

Note that on macOS, you’ll also need to install FUSE for macOS, which is a dependency for SSHFS. 

Installing SSHFS on Windows

For Windows users, you’ll need to use PowerShell to install SSHFS. Right-click on the PowerShell icon and select “Run as administrator” to ensure you have the necessary privileges. 

  1. Install WinFsp: WinFsp provides the FUSE functionality required by SSHFS: 
winget install -e --id "WinFsp.WinFsp"
  1. Install SSHFS-Win: SSHFS-Win is a minimal port of SSHFS to Windows: 
winget install -e --id "SSHFS-Win.SSHFS-Win" 

Verifying the Installation 

After installation, you can verify that SSHFS is correctly installed by checking its version. In your terminal (Linux/macOS) or PowerShell (Windows), run: 

sshfs --version

This command should display the version number of SSHFS installed on your system. 

With SSHFS now installed on your local machine, you’re ready to start mounting remote directories. In the next section, we’ll cover the basic usage of SSHFS to mount a remote filesystem. 

Basic Usage of SSHFS

Once you install SSHFS on your system, you can start using it to mount remote directories. This section will cover the basic usage of SSHFS, including the command syntax, mounting a remote directory, and unmounting it. 

Syntax of the SSHFS Command

The basic syntax of the SSHFS command is as follows: 

sshfs [user@]hostname:[remote_directory] [local_mount_point] [options] 
  • [user@]hostname: The SSH user and hostname of the remote machine. If you omit the username, the system uses the current local username.
  • [remote_directory]: The directory on the remote machine that you want to mount. 
  • [local_mount_point]: Mount the remote directory on the local directory.
  • [options]: Additional options to customize the behavior of SSHFS. 

Mounting a Remote Directory

To mount a remote directory, follow these steps: 

  • Create a Local Mount Point: First, create a directory on your local machine to mount the remote directory.For example: 
mkdir ~/remote_mount 
  • Mount the Remote Directory: Use the SSHFS command to mount the remote directory to the local mount point. 
    For example, to mount the /var/www directory from a remote server example.com as user username to the local directory ~/remote_mount, run: 
 sshfs [email protected]:/var/www ~/remote_mount 

If you have set up SSH key-based authentication, you won’t need to enter a password. Otherwise, the system will prompt you to enter the SSH password.

  • Verify the Mount: Verify that you have mounted the remote directory by listing the contents of the local mount point.
ls ~/remote_mount 

You should see the contents of the remote /var/www directory. 

Unmounting the Remote Directory 

After finishing work with the remote directory, you can use the fusermount command on Linux or macOS to unmount it. On Windows, use the umount command to unmount the directory.

Unmounting On Linux and macOS 

fusermount -u ~/remote_mount 

Windows Unmounting

umount ~/remote_mount 

Unmounting the remote directory will disconnect the SSHFS session and free up system resources. 

Advanced SSHFS Options 

While the basic usage of SSHFS is straightforward, there are several advanced options that can enhance its functionality and performance. This section will cover setting permissions, configuring connection options, and using SSHFS with different ports. 

Setting Permissions

By default, the mounts remote directories with the permissions of the remote system. However, you can modify these permissions using mount options: 

  • Allow Other Users: To allow other users on your local system to access the mounted directory, use the -o allow_other option: 
sshfs user@remote:/path/to/remote ~/local_mount -o allow_other 
  • Set UID and GID: To set specific user and group IDs for the mounted files, use the uid and gid options: 
sshfs user@remote:/path/to/remote ~/local_mount -o uid=1000,gid=1000 

Configuring Connection Options 

SSHFS allows you to fine-tune your connection for better performance or security: 

  • Compression: Enable compression to potentially improve performance over slow connections: 
sshfs user@remote:/path/to/remote ~/local_mount -o compression=yes 
  • Keep Alive: To prevent the connection from timing out, use the ServerAliveInterval option: 
sshfs user@remote:/path/to/remote ~/local_mount -o ServerAliveInterval=15 
  • Caching: Adjust caching behavior for better performance: 
sshfs user@remote:/path/to/remote ~/local_mount -o cache=yes,cache_timeout=115200 

Using SSHFS with Different Ports 

If your SSH server is running on a non-standard port, you can specify it using the -p option: 

sshfs -p 2222 user@remote:/path/to/remote ~/local_mount 

This command connects to the SSH server running on port 2222. 

Combining Options 

You can combine multiple options in a single command. For example: 

sshfs user@remote:/path/to/remote ~/local_mount -o allow_other,compression=yes,ServerAliveInterval=15,port=2222 

This command mounts the remote directory, allows other users to access it, enables compression, sets a keep-alive interval, and connects to a non-standard SSH port. 

Conclusion 

SSHFS (SSH Filesystem) provides a robust and secure tool that allows you to mount remote file systems over SSH, enabling seamless access to remote files as if you stored them locally. This guide has walked you through the essential steps for setting up and using SSHFS, from installation to troubleshooting.  

SSHFS Keypoints 

  • Installation: You can install SSHFS across various operating systems, including Linux, macOS, and Windows, ensuring broad compatibility.
  • Basic Usage: With simple commands, you can mount and unmount remote directories, making remote data easily accessible. 
  • Advanced Options: Customize your SSHFS setup with options for permissions, connection settings, and port configurations to enhance functionality and performance. 
  • Troubleshooting: Address common issues such as connection problems, permission errors, and performance bottlenecks to ensure smooth operation. 

By integrating SSHFS into your workflow, you can securely and efficiently manage remote files, enhancing productivity and streamlining tasks. Whether you’re a system administrator or an IT-savvy user, SSHFS offers a flexible solution for remote file access. 

Scroll to Top