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

Now that we have the prerequisites covered, let's proceed with the steps to mount a remote Linux filesystem or directory using 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

Now that SSH key-based authentication is set up, let's proceed with mounting the remote filesystem or directory using SSHFS.

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
  • You can now perform various operations on the mounted files, such as copying, moving, editing, or deleting them, as if they were local files.

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

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

By following these steps, you can easily mount and access a remote Linux filesystem or directory using SSHFS. It provides a convenient way to work with remote files as if they were local, enhancing your productivity and flexibility.

Conclusion

In conclusion, mounting a remote Linux filesystem or directory using SSHFS offers a secure and convenient way to access and manage files on remote servers. With SSHFS, you can seamlessly integrate the remote filesystem with your local system, enabling easy file operations using familiar tools and commands.

Updated on: 09-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements