Mount Remote Linux Filesystem or Directory Using SSHFS Over SSH
How to Mount Remote Linux Filesystem or Directory Using SSHFS Over SSH
I find myself doing this often, so the purpose of writing this article is to provide a step-by-step guide on how to mount a remote Linux file system using SSHFS client over SSH. This is not part of my RHCSA release as it is an added application.
The ability to mount a drive located on a remote computer is extremely helpful. For example, I have a laptop that I carry about the house when I am deep in work with a desktop that is stationary in my office. The desktop has a 4TB external hard drive attached to it.
Keeping articles, scripts, and personal information on the external drive, I find myself referring to it for reference, research, etc. SSH is a quick way to jump to that computer and read something I have stored as a text file, but what if it is a .pdf that I need to reference? SCP works but it is daunting to remember the directories exactly if I want to pull it to my working laptop.
SSHF comes into play here as I can mount the remote drive to my local drive and search it as if I had it directly plugged into my laptop.
Before we go further into installation let’s understand about SSHFS and how it works.
What Is SSHFS?
SSHFS stands for (Secure SHell FileSystem) client that enable us to mount remote filesystem and interact with remote directories and files on a local machine using SSH File Transfer Protocol (SFTP).
SFTP is a secure file transfer protocol that provides file access, file transfer, and file management feature over Secure Shell protocol. Because SSH uses encryption while transferring files over the network from one computer to another computer and SSHFS comes with a built-in FUSE (Filesystem in Userspace) kernel module that allows any non-privileged users to create their file system without modifying kernel code.
Any Linux machine is able to use this feature. I have also set up a Raspberry Pi to be a network share.
Install SSHFS Client in Linux Systems.
By default SSHFS packages do not exist on all major Linux distributions, you need to enable the epel repository under your Linux systems to install SSHFS with the help of Yum command with their dependencies.
On RHEL or CentOS
$ sudo yum install sshfs
On Fedora 22+ releases
$ sudo dnf install sshfs
On Debian/Ubuntu based systems
$ sudo apt-get install sshfs
Creating SSHFS Mount Directory.
Once the SSHFS package installed, you will create a mount point directory where you will mount your remote file system. In this example, I have created a mount directory under
/mnt/4TB 4TB because it is my 4 Terabyte External HardDrive. Some distros you find your mounts under
/media/ or in the case of RHEL8 you find it in
/run/media But I use
/mnt/ since it is universal for all distros. You can go one step farther and create a hardlink to that mount point.
Remember to replace my 4TB with what you are naming yours.
$ sudo mkdir /mnt/4TB
Mounting The Remote Filesystem with SSHFS
Once you have created your mount point directory, run the following command as a root user to mount the remote file system under
/mnt/4TB. In your case the mount directory would be anything.
The following command will mount a remote directory called
/run/media/spohnz/4TB (Its a RHEL8 desktop that's why it is in
/run/media but you can mount any directory.) under
/mnt/4TB in the local system. Don’t forget to replace x.x.x.x with your IP Address and mount point.
$ sudo sshfs -o allow_other spohnz/run/media/spohnz/4TB /mnt/4TB.x.x.x:
If your Linux server is configured with SSH key based authorization, then you will need to specify the path to your public keys.
$ sudo sshfs -o allow_other,IdentityFile=~/.ssh/id_rsa spohnz .x.x.x:/run/media/spohnz/4TB /mnt/4TB
Verifying That The Remote Filesystem is Mounted.
If you have run the above command successfully without any errors, you will see the list of remote files and directories mounted under
/mnt/4TB or your version of it.
$ cd /mnt/4TB
If you need to be root to view then you may want to chown the directory.
Mounting The Remote Filesystem Permanently.
To mount the remote filesystem permanently, you need to edit the file called
/etc/fstab. To do, open the file with your favorite editor.
$ sudo vim /etc/fstab
Go to the bottom of the file and add the following line to it and save the file and exit. The below entry mount remote server file system with default settings.
firstname.lastname@example.org:/run/media/spohnz/4TB /mnt/4TB fuse.sshfs defaults 0 0
Make sure you’ve SSH Passwordless Login in place between servers to automount filesystem during system reboots.
If your server is configured with SSH key based authorization, then add this line:
sshfs#spohnz/run/media/spohnz/4TB /mnt/4TB fuse.sshfs IdentityFile=~/.ssh/id_rsa defaults 0 0.x.x.x:
Next, you need to update the fstab file to reflect the changes.
$ sudo mount -a
Now the remote drive will always be available when you reboot. There are alternatives that may make sense to you other than mounting at boot. For me personally, I run a bash script to mount what I need when I need it. But there you have it. Hope it is useful.