How to Use SSHFS on Linux?


In this tutorial, we will delve into the intricate details of utilizing SSHFS on Linux. SSHFS, an acronym for SSH File System, seamlessly mounts a remote file system over a secure SSH connection. By blending the power of the Secure Shell (SSH) protocol with the convenience of a file system, SSHFS enables users to access and manipulate files on a remote server as if they were locally stored. This tutorial will guide you through the process of setting up SSHFS, meticulously outlining each step involved, including installation, configuration, mounting, and advanced usage scenarios. By the end of this tutorial, you will possess a profound understanding of SSHFS and its incredible potential for facilitating efficient file management tasks on Linux.

Installing SSHFS and Configuring SSH Access

Let's initiate the installation process of SSHFS on our Linux system. In order to achieve this, launch a terminal and execute the following command −

sudo apt-get install sshfs

This command promptly installs the SSHFS package alongside its necessary dependencies, ensuring a seamless setup. Once the installation completes, we must configure SSH access to the remote server, reinforcing the security of our system. To accomplish this, open the SSH configuration file located at `/etc/ssh/sshd_config` using a text editor 

sudo nano /etc/ssh/sshd_config

Within this file, identify the line commencing with `PermitRootLogin` and alter its value to `no`, effectively disabling root login. By implementing this crucial security measure, we fortify our system by preventing direct root access over SSH. Subsequently, save the file and exit the text editor.

To ensure that our SSH configuration changes take effect, we need to restart the SSH service. Execute the following command to initiate the SSH service restart 

sudo service ssh restart

With SSHFS successfully installed and SSH access appropriately configured, we are now fully prepared to proceed with mounting a remote file system.

Mounting a Remote File System

In this section, we will delve into the intricacies of mounting a remote file system using SSHFS. To successfully accomplish this task, we require certain essential information: the IP address or hostname of the remote server, your username on the remote server, and the specific directory on the remote server that we intend to mount.

Before proceeding further, let's create a directory on our local machine where we can seamlessly mount the remote file system. For illustrative purposes, let's assume we create a directory named `remote` within our home directory 

mkdir ~/remote

With our local directory ready, we can now proceed to mount the remote file system by employing SSHFS. Execute the following command −

sshfs username@remote_server:/path/to/remote_directory ~/remote

In the above command, kindly replace `username` with your username on the remote server, `remote_server` with the IP address or hostname of the remote server, and `/path/to/remote_directory` with the actual directory path you intend to mount.

Upon executing the command, you will be prompted to enter the password for the remote server. Once authenticated successfully, the remote file system will be seamlessly mounted onto the specified local directory.

To validate the successful mount, you can utilize the `df` command, which conveniently displays information about currently mounted file systems. Execute the following command 

df -h ~/remote

By executing this command, you should be able to witness the mounted remote file system listed within the output.

Browsing and Manipulating Remote Files

In this section, we will explore the myriad possibilities offered by SSHFS for effortlessly browsing and manipulating files within the mounted remote file system. Utilizing standard Linux commands, we can conveniently perform an array of operations to efficiently manage our files.

To obtain a comprehensive listing of the contents within a remote directory, employ the `ls` command, as shown in the following example 

ls ~/remote

Upon executing this command, a detailed display of files and directories present on the remote file system will be presented.

To seamlessly copy files from our local machine to the remote file system, we can effortlessly utilize the `cp` command. For instance, to copy a file named `example.txt` from our current directory to the remote file system, execute the following command 

cp example.txt ~/remote

Upon executing this command, the specified file will be efficiently copied to the remote file system, allowing for seamless file management.

To facilitate convenient editing of files located on the remote server using our preferred text editor, we can simply open the file as if it were stored locally. For instance, to edit a file named `config.ini` on the remote file system using the `nano` editor, execute the following command 

nano ~/remote/config.ini

Upon executing this command, the `nano` editor will launch, providing an intuitive and familiar environment to make the necessary changes. Once the desired alterations have been made, proceed to save the file and exit the text editor. This process ensures that the changes are seamlessly reflected within the remote file system.

Advanced Usage Scenarios

Within this section, we will explore some advanced usage scenarios to further enhance our understanding and utilization of SSHFS.

To effortlessly unmount the remote file system, we can employ the `fusermount` command followed by the specific mount point. For instance, to unmount the `remote` directory we previously created, execute the following command 

fusermount -u ~/remote

By executing this command, the remote file system will be promptly unmounted, rendering the directory inaccessible until remounted.

For users frequently interacting with a specific remote file system, we can automate the mounting process by adding an entry within the `/etc/fstab` file. To facilitate this, open the file using a text editor 

sudo nano /etc/fstab

Within this file, append the following line to the end −

username@remote_server:/path/to/remote_directory ~/remote fuse.sshfs defaults 0 0

Ensure to replace `username`, `remote_server`, and `/path/to/remote_directory` with the appropriate values corresponding to your specific setup. Following this, save the file and exit the text editor. Subsequently, the remote file system will be automatically mounted to the specified directory during system startup, streamlining and simplifying your workflow.

Conclusion

In conclusion, this in-depth tutorial has thoroughly covered the fundamentals of utilizing SSHFS on Linux. By meticulously following the step-by-step instructions, which encompassed installing SSHFS, configuring SSH access, mounting remote file systems, and performing various file operations, readers have gained profound insights into the seamless integration of remote file management into their Linux environment. SSHFS effectively combines the power of SSH with the convenience of a file system, enabling users to work with remote files as effortlessly as if they were local. Armed with the knowledge acquired from this tutorial, readers are now equipped to employ SSHFS to streamline their file management tasks on Linux, enhancing productivity and efficiency in the process.

Updated on: 27-Jul-2023

118 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements