Secure Shell FileSystem (SSHFS) is a file system for Linux systems that uses the rewritten implementation of the FUSE module, developed by Miklos Szeredi, who also wrote SSHFS, allowing you to mount a file system from an SSH server using that same protocol. On Windows, you can use SSHFS-Win by Bill Zissimopoulos to mount the network drive.
In this article, we will briefly explain how to use this file system, mounting it manually and automatically with fstab.
Installing and mounting a file system
On the client machine, install the following packages:
# apt install sshfs fuseNext, check that FUSE is loaded in the kernel:
# grep -i fuse /lib/modules/$(uname -r)/modules.builtinThe following should appear:
kernel/fs/fuse/fuse.koIf it does not appear, enable it:
# modprobe fuseNext, create the location where you will mount the file system:
# mkdir /root/sshfsThen simply specify the remote directory and the destination directory:
# sshfs -o allow_other root@dirección_IP:/root /root/sshfsYou will be prompted for the remote SSH server's password. Once you enter it, the directory will be mounted (example):
root@clientesshfs:~# ls -la /root/sshfs/
total 56
drwx------ 1 root root 4096 Jan 4 14:33 .
drwx------ 8 root root 4096 Jan 4 17:59 ..
drwxr-xr-x 1 root root 4096 Jan 4 14:33 .ansible
-rw------- 1 root root 5 Jan 4 17:40 .bash_history
-rw-r--r-- 1 root root 3106 Dec 5 2019 .bashrc
drwx------ 1 root root 4096 Jun 15 2020 .cache
drwx------ 1 root root 4096 Jun 15 2020 .config
-rw-r--r-- 1 root root 161 Dec 5 2019 .profile
drwx------ 1 root root 4096 Jan 4 14:33 .ssh
-rw------- 1 root root 14008 Aug 26 16:11 .viminfo
-rw------- 1 root root 110 Dec 21 10:22 .XauthorityHow to mount the file system automatically
The most practical and secure way to mount the SSHFS file system automatically when the operating system starts is to use SSH keys for this purpose. You will therefore need to create a key on the client machine and copy/transfer it to the remote server.
We explain the process below, but you can find more information in Creating keys with ssh-keygen.
First, create the SSH key with ssh-keygen:
# ssh-keygen -t rsa -b 4096Once it has been created, you can use the utility included with ssh-keygen itself to transfer it:
# ssh-copy-id root@direcciónIPYou will be prompted for the remote server's password to store the key, and a message should appear indicating that it has been added:
Number of key(s) added: 1Once this is done, check that you can log in without being prompted for the remote server user's password. If you can, you will be able to mount the file system through fstab using the key.
To do this, edit the fstab file:
# vi /etc/fstabAnd add the following line at the end (modify it if necessary):
root@direcciónIP:/root /root/sshfs fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=/root/.ssh/id_rsa,allow_other,reconnect 0 0Once this is done, the file system will mount automatically when the operating system starts.
We hope this tutorial has helped you 🙂. Remember, if you have any questions about this or anything else related to your servers on Clouding, feel free to write to soporte@clouding.io We're here for whatever you need!