How to mount remote directories in Linux using SSHFS

 

In the world of system administration and DevOps, secure and efficient file access is crucial. Whether you’re managing multiple servers, working with remote systems, or simply need a reliable way to access files on a different machine, SSHFS (SSH Filesystem) is an invaluable tool. It allows you to mount a remote filesystem over SSH, making it accessible as if it were part of your local file system. This not only simplifies file management but also ensures that your data remains secure through encrypted SSH connections.

In this blog post, we’ll delve into what SSHFS is, why it’s useful, and provide a detailed step-by-step guide to mounting remote directories using SSHFS.

What is SSHFS?

SSHFS is a user-space file system that allows you to mount and interact with directories and files on a remote server using the SSH (Secure Shell) protocol. This means you can work with files on a remote server as if they were on your local machine, leveraging the security features of SSH for secure communication.

Why Use SSHFS?

SSHFS offers several benefits:

Security: SSHFS uses SSH to communicate, which means all data is encrypted. This ensures that your file transfers and access are secure.

Ease of Use: Once mounted, the remote directory behaves like any other directory on your local machine. You can use standard file operations like copy, move, edit, and delete.

Cross-Platform Compatibility: SSHFS works on Linux, macOS, and even Windows (using third-party tools), making it a versatile option for various environments.

No Additional Server Software Needed: As long as the remote server has SSH access enabled, you don’t need to install any additional software on the server side.

Android using Linux terminal.  Generated with Leonardo.ai

 

If you need a VPS for this tutorial, you can find them here:
AMD EPYC™ VPS Servers
Intel Xeon® Gold VPS Servers
Powerful ARM Ampere® Altra® VPS Servers.

Step-by-Step Guide to Mounting a Remote Directory Using SSHFS

Now that you understand the basics, let’s walk through the process of mounting a remote directory using SSHFS.

Step 1: Install SSHFS

The first step is to install SSHFS on your local machine. The installation process varies depending on your operating system.

For Ubuntu/Debian:

Open a terminal and run:

Open a terminal and run:

sudo apt-get install sshfs

For Fedora/CentOS/RHEL:

Use the following command:

sudo dnf install sshfs

For macOS:

If you’re using macOS, you can install SSHFS using Homebrew:

brew install sshfs

For Windows:

Windows users can install SSHFS using tools like WinFsp and SSHFS-Win. Here’s how:

  1. Download and install WinFsp.
  2. Download and install SSHFS-Win.

Step 2: Create a Mount Point

A mount point is simply a directory where the remote filesystem will be mounted. This directory can be located anywhere in your local filesystem.

Create a mount point with the following command:

Create a mount point with the following command:

mkdir ~/remote_directory

You can replace ~/remote_directory with any directory path of your choice.

Step 3: Mount the Remote Directory

Now that you have SSHFS installed and a mount point ready, you can mount the remote directory.

The basic syntax for mounting a remote directory is:

sshfs username@remote_host:/path/to/remote/directory ~/remote_directory
  • username: Your SSH username on the remote server.
  • remote_host: The hostname or IP address of the remote server.
  • /path/to/remote/directory: The directory on the remote server you want to mount.
  • ~/remote_directory: The mount point on your local machine.

For example, if your username is john, your remote host is example.com, and you want to mount the /home/john/projects directory from the remote server to a local directory called ~/remote_projects, you would use the following command:

sshfs john@example.com:/home/john/projects ~/remote_projects

Step 4: Working with Mounted Remote Directory

Once mounted, the remote directory behaves just like any other directory on your local machine. You can navigate to it using cd, list files using ls, and open files with your preferred text editor or file manager. Any changes you make will be reflected on the remote server.

Step 5: Unmounting the Remote Directory

When you’re done working with the remote files, it’s important to unmount the directory to safely disconnect the SSHFS session.

To unmount the directory, use the fusermount command on Linux:

fusermount -u ~/remote_directory

On macOS, you can use the umount command:

umount ~/remote_directory

For Windows, you can use the standard “Disconnect Network Drive” option in File Explorer or use the net use command in the command prompt.

Step 6: Automating the Mounting Process (Optional)

If you frequently access the same remote directories, you might want to automate the mounting process. This can be done by adding the sshfs command to your .bashrc or .zshrc file, or by creating a script that runs on startup.

Here’s an example script that you could place in /usr/local/bin/mount_remote:

#!/bin/bash
sshfs username@remote_host:/path/to/remote/directory ~/remote_directory

Make sure to make the script executable:

chmod +x /usr/local/bin/mount_remote

Now, you can simply run mount_remote to mount your remote directory.

Conclusion

SSHFS is a powerful tool that can simplify the way you interact with remote filesystems. By following the steps outlined in this guide, you can securely and easily mount remote directories and work with them as if they were part of your local system. Whether you’re managing multiple servers, collaborating on projects, or just need to access files remotely, SSHFS provides a secure and efficient solution.

With this knowledge, you’re now equipped to make remote file access a seamless part of your workflow. Happy mounting!