In development, you often edit files and upload them to the server (ex: hosting) a couple of times. This can be a headache to do the same thing again and again. Luckily there is an alternative way to mount remote files and directories into your system in an encrypted way.
What is SSHFS?
SSHFS (Secure shell file system) is a filesystem client that uses SFTP protocol to mount remote files and directories to the local system over an ssh connection.
Comparing other protocols that help us perform the same task, like Samba and NFS, requires additional configuration, while SSHFS advantage is that it doesn’t require any external configuration to be done before establishing the connection.
There will be slightly degraded performance because SSHFS uses SFTP connection to transmit all data between server and client, which requires encrypting and decrypt data before sending and receiving, which take few more second and system usage.
SSHFS is mostly available in all major Linux distribution, and installation is pretty straightforward.
$ sudo apt install sshfs
$ sudo yum install sshfs
Windows 7/8 and 10
Installation on Windows based is pretty straightforward. Just download the package from below link and install as regular windows software.
Mounting remote directory:
Mounting remote files and directories on the local system are pretty simple. Just make sure that you have proper permission and authentication on the remote server, which you want to mount on a local system via SSH.
sshfs [email protected]_host:remote_directory mountpoint
username: Here, you have to specify the same username, which requires authenticating you on a remote server via ssh.
remote_host: Now, you have to specify the remote host IP or domain name from where you want to mount all the files and directories.
remote_directory: Once you specify all authentication information such as username and remote_host, you have to specify the remote directory you want to mount on your local system.
mountpoint: Finally, you have to specify where you want to mount remote files and directories in your local system. Generally, it is recommended to mount at /mnt/.
For example, I want to mount my remote home directory at /mnt/trendoceans in my local system. Then I have to first create a directory before mounting, and the syntax for that looks something like below.
$ sudo mkdir /mnt/trendoceans $ sshfs [email protected]:/home/trendoceans /mnt/trendoceans
Once you execute the above command to mount, it will ask you to enter a remote ssh password or nothing if you were using the public key to authenticate.
Mount remote directory from server with specific port:
Suppose your ssh server using a different port than the default 22. Then you can easily specify those ports with sshfs as a parameter like shown below.
$ sshfs -p port_number [email protected]_host:remote_directory mountpoint
Unmount remote directory:
To unmount or detach the remote directory on your local system. You can use umount and specify the location of the mounted directory in your system.
$ sudo umount /mnt/trendoceans
Caution: Never try to delete mounted files or directories; otherwise, they will be directly reflected in your remote server.
Permanently mount remote directory:
Suppose you want to mount the remote directory to your local system permanently. Then you have to edit /etc/fstab. This way, it will be auto mount whenever you boot up your system.
To mount a remote directory, first, open your fstab using your favourite text editor.
$ sudo nano /etc/fstab
Once it opens, add below the line by replacing your remote server authentication and directory to your local system. Here we were using fuse.sshfs as filesystem type.
[email protected]_host:remote_directory mountpoint fuse.sshfs defaults 0 0
Before using this method, make sure you have authentication to the remote server via a public key.
Innovative tech mind with 10 years of experience working as a computer programmer, web developer, and security researcher. Capable of working with a variety of technology and software solutions, and managing databases.