How to Mount Remote Linux Filesystem or Directory Using SSHFS Over SSH?

Mounting a remote filesystem or directory on your Linux system can provide convenient access to files and data stored on a remote server. One popular and secure method to achieve this is by using SSHFS (SSH Filesystem). SSHFS allows you to mount a remote directory on your local machine, giving you the ability to interact with the remote files as if they were stored locally.

In this tutorial, we will explore how to mount a remote Linux filesystem or directory using SSHFS over SSH. This approach ensures that the data transmission between your local machine and the remote server is encrypted and secure.

By the end of this guide, you will have a clear understanding of the necessary steps to set up SSHFS, configure SSH key-based authentication, mount a remote filesystem or directory, and perform basic operations on the mounted files.

Prerequisites

Before we begin, make sure you have the following prerequisites in place

  • Linux System Ensure that you have a Linux-based operating system installed on your local machine. SSHFS is widely supported on various Linux distributions.

  • SSH Access Ensure that you have SSH access to the remote server you want to mount. You should have the necessary credentials (username and password or SSH key) to connect to the remote server.

  • SSHFS Package Verify if SSHFS is installed on your local machine. If not, you can install it using the package manager specific to your Linux distribution. For example, on Ubuntu or Debian-based systems, you can install SSHFS by running the command

sudo apt-get install sshfs

For other distributions:

# CentOS/RHEL/Fedora
sudo yum install sshfs

# Or on newer versions
sudo dnf install sshfs

# Arch Linux
sudo pacman -S sshfs

Step 1: Generate SSH Key Pair (Optional but Recommended)

To enhance security and convenience, it's recommended to use SSH key-based authentication instead of password authentication. If you haven't set up SSH key authentication, you can generate a key pair using the following steps

  • Open a terminal on your local machine.

  • Generate an SSH key pair by running the command

ssh-keygen -t rsa -b 4096
  • Follow the on-screen prompts to provide a location for the key pair and optionally set a passphrase. Press Enter to accept the default values.

  • Once the key pair is generated, you can view the public key by running the command

cat ~/.ssh/id_rsa.pub
  • Copy the entire contents of the public key.

Step 2: Configure SSH Key-Based Authentication on the Remote Server

To configure SSH key-based authentication on the remote server, follow these steps:

  • Connect to the remote server using SSH. If you are using password authentication, enter your password when prompted.

  • Create an SSH directory (if not already present) by running the command

mkdir -p ~/.ssh
  • Open the authorized_keys file using a text editor. If the file doesn't exist, create it by running the command

touch ~/.ssh/authorized_keys
  • Paste the contents of the public key (copied in Step 1) into the authorized_keys file.

  • Save the changes and exit the text editor.

  • Set the appropriate permissions for the SSH directory and authorized_keys file by running the commands:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Mounting Remote Linux Filesystem Using SSHFS

To mount a remote Linux filesystem or directory using SSHFS, follow these steps

Step 1: Create a Mount Point

  • Open a terminal on your local machine.

  • Create a directory that will serve as the mount point for the remote filesystem. For example, you can create a directory named "remote-mount" in your home directory

mkdir ~/remote-mount

Step 2: Mount the Remote Filesystem

  • Run the following command to mount the remote filesystem using SSHFS

sshfs username@remote-server:/path/to/remote-directory ~/remote-mount

Replace "username" with your remote server username, "remote-server" with the hostname or IP address of the remote server, and "/path/to/remote-directory" with the actual path to the directory you want to mount on the remote server.

For example, if your username is "john", the remote server's IP address is "192.168.1.100", and you want to mount the "/data" directory on the remote server, the command would be:

sshfs john@192.168.1.100:/data ~/remote-mount
  • Enter your password or passphrase when prompted.

  • If the connection is successful, the remote filesystem will be mounted to the specified mount point.

Step 3: Access the Mounted Files

  • Once the remote filesystem is mounted, you can access the files and directories within it using your file manager or command-line tools.

  • To navigate to the mounted directory using the command line, you can use the cd command. For example

cd ~/remote-mount
ls -la
  • You can now perform various operations on the mounted files, such as copying, moving, editing, or deleting them, as if they were local files.

Advanced SSHFS Options

SSHFS supports various options to customize the mounting behavior:

# Mount with specific port
sshfs -p 2222 username@remote-server:/path ~/remote-mount

# Mount with compression enabled
sshfs -o compression=yes username@remote-server:/path ~/remote-mount

# Mount with reconnection on connection loss
sshfs -o reconnect username@remote-server:/path ~/remote-mount

# Mount with custom SSH key
sshfs -o IdentityFile=/path/to/private/key username@remote-server:/path ~/remote-mount

Unmounting the Remote Filesystem

To unmount the remote filesystem, use the fusermount command followed by the mount point directory. In our example, the command would be

fusermount -u ~/remote-mount

Alternatively, you can use:

umount ~/remote-mount

Ensure that you are not currently accessing any files or directories within the mounted filesystem before unmounting.

Troubleshooting Common Issues

Here are some common issues and their solutions:

  • Permission denied Ensure SSH key permissions are correct (600 for private key, 644 for public key).

  • Connection refused Verify the remote server's SSH service is running and accessible.

  • Mount point busy Use fusermount -u to force unmount before remounting.

  • Slow performance Consider using compression or checking network latency.

Conclusion

SSHFS provides a secure and convenient way to mount remote Linux filesystems over SSH. It offers seamless integration with your local system, enabling you to work with remote files using familiar tools and commands. The encrypted connection ensures data security during transmission, making it ideal for accessing files on remote servers.

Updated on: 2026-03-17T09:01:39+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements