SSHFS is a filesystem client used to mount and interact with files and directories on a remote server. SSH protocol is used to encrypt data exchange between the two hosts.
FTP (File Transfer Protocol) is a networking protocol used to transfer files between two computers over a network, SFTP on the other hand in addition to performing a similar function to FTP, it incorporates a security layer used to encrypt the data being sent and received between the two machines over the internet.
Now, how do the two tools work together? Well, SSHFS is just a file system in the user space, for it to perform the function of mounting a remote file system, it utilizes SFTP.
Installing SSHFS in Linux
SSHFS is a Linux-based software tool that is available to install from the standard repositories in most distributions using the default package manager.
$ sudo apt install sshfs [On Debian, Ubuntu and Mint] $ sudo yum install fuse-sshfs [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a net-fs/sshfs [On Gentoo Linux] $ sudo pacman -S sshfs [On Arch Linux] $ sudo zypper install sshfs [On OpenSUSE]
Mounting the Remote Linux File System
For this to work, the remote server needs to be reachable therefore perform some ping requests. We should also make sure that we have the server credentials e.g password.
SSH also needs to be configured on both the server and client machines.
The syntax for the command is:
$ sshfs [user@]host:[dir] mountpoint [options]
From the syntax we have:
- user – This specifies the username on the server. When connected via SSH, we are prompted for a password for the remote user account with the specified username.
- host – We specify the hostname of the remote server, we could specify a domain name e.g www.example.com which is easier to recall or an IP address.
- dir – This specifies the path of the remote directory.
- mountpoint: This specifies the location in which we want our file system to be mounted, Note that this is in our local system. Normally file systems are mounted in the /mnt directory.
The image below shows the IP address and file system of the remote server we want to mount to our local system.
On our local system, let’s create a directory preferably in the /mnt directory where we will mount the remote file system:
$ sudo mkdir /mnt/remoteFS
Now to mount the remote Linux file system that is located at 192.168.100.62 at /home/user directory, we write:
$ sudo sshfs -o allow_other [email protected]:/home/user /mnt/remoteFS/
When we execute the above command we are prompted for a password for the remote system user account which we have specified within the command.
After the password is accepted the remote file system is mounted on our local system on the selected mount point /mnt/remoteFS. Note that we have used the
-o allow_other option to allow other users to read and write to the mounted file system.
To verify that the remote Linux file system is mounted, run:
$ sudo ls /mnt/remoteFS
From the above image, we list the remote file system which is now mounted on our local machine. You can also check its mount point in our local system by using.
$ df -Th
Caution: DON’T DELETE THE FILE SYSTEM!! – We should not delete the mounted file system as it will also be deleted on the remote server and we don’t want this.
Permanently Mounting the Remote Linux File System
The mounting we have seen thus far is only temporary, to mount the remote file system permanently, we edit the fstab file located in the /etc directory as follows:
$ sudo vim /etc/fstab
After the file is open, paste the following line, remember to use details specific to your setup.
sshfs#[email protected]:/home/user/ /mnt/remoteFS fuse.sshfs defaults 0 0
To mount the file execute the following command:
$ sudo mount -a
Now even if we are disconnected from the remote machine, the file system will remain mounted in the specified directory we had mounted it on previously.
Unmounting the Remote Linux File System
Suppose we are done with this filesystem we can unmount it using the umount command as follows:
$ sudo umount /mnt/remoteFS
We have learned how to mount a remote file system to our local machine and perform this over SSH protocol for security purposes. We have also seen how to mount the file system on our local system permanently by editing the /etc/fstab file.