Learning Center
Linux

How To Use SSHFS to Mount Remote File Systems Over SSH

24 Dec 2024
Awais Khan
Awais Khan

SSHFS is a Linux tool for mounting remote folders over SSH. It allows users to manage network-shared files just like local ones. This tool is secure and efficient, providing seamless management of network shared folders across different environments.

Required Setup
Copy link

Before you start, ensure you have:

  • Root or sudo permissions.
  • An external server with SSH enabled.
  • An SSH service working on your local machine.

And if you’re looking for a reliable, high-performance, and budget-friendly solution for your workflows, Hostman has you covered with Linux VPS Hosting options, including Debian VPS, Ubuntu VPS, and VPS CentOS.

Using SSHFS
Copy link

Step 1: Install SSHFS
Copy link

First, install SSHFS on your local system through the package manager. This tool installation on the other system is not needed.

On Ubuntu/Debian:

sudo apt install sshfs

Image1

On CentOS/RHEL:

sudo yum install sshfs

Step 2: Create a Connection Point
Copy link

Set up a folder in your home or any desired location. This will act as the connection point for the network shared directory.

sudo mkdir remote_dir

Image3

Step 3: Attach a Directory
Copy link

Attach the linked folder to the local computer for seamless access. Use the below-given command to perform remote filesystem mounting:

sudo sshfs -o [options] user@host:/remote_path /local_mount

Substitute user with your real remote server’s username, host with the IP address or hostname of the server, and /remote_path with the directory path you want to connect. The [options] include:

  • allow_other: Grants access to other local machine users for accessing the mounted folder.
  • reconnect: Automatically re-establishes the connection in case it drops.
  • IdentityFile=/loc/of/private_key: Specify the location where SSH private key is stored.
  • idmap=user: Aligns the ID of remote user to the local user ID.
  • default_permissions: Applies the remote file system's default permissions.

To connect the linux home folder from 192.X.X.X to /home/ubuntu/remote_dir, utilize:

sudo sshfs -o allow_other,default_permissions linux@192.X.X.X:/home/linux/ /home/ubuntu/remote_dir/

To employ an SSH key found at /home/ubuntu/.ssh/id_rsa, use:

sudo sshfs -o allow_other,default_permissions,IdentityFile=/home/ubuntu/.ssh/id_rsa linux@192.X.X.X:/home/linux/ /home/ubuntu/remote_dir/

Type 'yes' to accept the server’s fingerprint and add it to known hosts.

Image2

Enter the password for authentication. Use the key if set up.

Image5

After verification, the folder will be linked to the local path.

Image4

Step 4: Verification
Copy link

Create a new folder or file in the attached directory and verify its existence on the external server.

Image7

If the folder or file appears in the external server's directory, the operation is successful. This ensures changes in your local directory are mirrored on the external system.

Image6

If you experience the "Permission denied" error when trying to create or modify an existing file, follow these instructions to resolve it:

  1. Run the ls -l command to view the current permission of files or directory.

  2. Execute the chmod command to modify the permissions.

sudo chmod 644 /path/to/file_or_directory
  1. If the file or directory is owned by another person, run the chown command to change the ownership.

sudo chown your_username  /path/to/file_or_directory

Step 5: Unmounting
Copy link

Once finished, simply unmount the folder:

sudo umount /remote_directory

Image9

Additional Options
Copy link

Below are some additional things you can also do: 

Auto-Mounting at Boot
Copy link

To automatically connect remote filesystem at startup, utilize these steps:

Step 1: Edit fstab

Access the /etc/fstab file with elevated privileges:

sudo nano /etc/fstab

Step 2: Add Entry

Append an entry to the end of the file:

user@remote_host:/remote/directory /local/mount/point fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,allow_other,reconnect 0 0

Example:

linux@192.X.X.X:/home/linux/ /home/ubuntu/remote_dir fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,allow_other,reconnect 0 0

Where:

  • noauto: Automatically stops the mount from happening at boot.
  • x-systemd.automount: Uses systemd to dynamically connect the filesystem upon access.
  • _netdev: Indicates that network access is required for the process.
  • users: Grant non-root users the ability to mount and unmount.
  • idmap=user: Associates external user with local one.
  • allow_other: Permits another person from retrieving the connected directory.
  • reconnect: Ensures automatic reconnection in case connection drops.

Step 3: Create a Connection Point

Make sure the local mount point directory exists, and if not, create it:

sudo mkdir -p /home/ubuntu/remote_dir

Step 4: Testing

Test the connectivity:

sudo mount -a

This command initiates the connection of all filesystems listed in /etc/fstab. If no errors arise, the process is successful.

Utilizing SSHFS Without SFTP
Copy link

SSHFS usually utilizes SFTP for transferring. To bypass this, run:

sshfs -o sftp_server=/usr/lib/openssh/sftp-server user@host:/remote_directory ~/remote_mount

Configuration File
Copy link

To save commonly used options, create a .sshfs_config file in your home location. This will allow you to store and easily apply your preferred settings.

nano ~/.sshfs_config

Add your options and connect via the configuration file.

sshfs -F ~/.sshfs_config username@remote_host:/remote/directory ~/remote_mount

Resolving Typical Problems
Copy link

Below are some common problems and solutions.

Connectivity Problems
Copy link

To ensure seamless connectivity, make certain that SSH service is configured in the correct way on both your local and external systems. Also, check that the service port is open and that your firewall settings allow access, which is crucial for maintaining an uninterrupted connection.

Performance Issues
Copy link

For better performance, use the -o direct_io and -o cache=yes options.

sshfs -o direct_io -o cache=yes user@host:/remote_directory ~/remote_mount

Connection Reset by Peer
Copy link

  • Cause: The external SSH server may be down, or there could be network instability.
  • Solution: Verify that the SSH server is operational on the external machine. Ensure a stable network connection for consistent communication.

Permission Denied
Copy link

  • Cause: The user lacks the required permissions to access the network-shared folder.
  • Solution: Confirm that you have the correct permissions. Proper access rights are essential for successful connection.

Running SSHFS on Windows
Copy link

To utilize SSHFS for Windows, follow these instructions:

  1. Download and set up SSHFS-Win from this location.

  1. Right-click on This PC and go with the option Map network drive from the context menu:

Image8

  1. Choose an available drive letter from the menu. In the Folder field, input the command as follows:

\\sshfs\user@host\remote_path

Click Finish to complete the mapping process.

Image12

  1. Enter your credentials and provide the required username and password (or SSH key, if configured).

Once connected, access the directory via Windows Explorer.

Image10

Here are the additional options for the sshfs command based on different used cases:

  • sshfs: Integrates the remote home directory locally.
  • sshfs.r: Links to the remote server's root directory.
  • sshfs.k: Uses [local-user]/.ssh/id_rsa to map the remote home directory.
  • sshfs.kr: Utilizes a locally stored SSH key to access the root directory.

When finished, right-click the network drive and choose Disconnect to detach the directory.

Image11

Conclusion
Copy link

SSHFS provides an efficient and secure method for mounting remote file systems through SSH. This guide helps you set up and use this tool to improve file management on Linux systems. Whether performing the SSHFS mount as root, avoiding SFTP, or utilizing configuration files, this tool offers flexibility and control for various scenarios.