This guide aims at demonstrating steps to Installing sshfs and using it to mount a remote directory on your local system over SSH. We’ll kick off by explaining what SSHFS is, how to install it, and finally how to use it to mount a remote directory on the local system.
What’s SSHFS?
SSHFS is a FUSE-based filesystem used for mounting remote directories on a local system over SSH connection. Helper tools for operating sshfs are available, the main one being sftpman which ease mounting and unmounting task for heavy users of SSHFS.
Installing sshfs on Ubuntu
SSHFS is Linux based software and it needs to be installed on your local computer before you can do the mounting. On Ubuntu and Debian based systems it can be installed using apt-get
package manager.
sudo apt update && sudo apt install sshfs
There is no configuration needed, after installation you’re set to start using sshfs.
Installing sshfs on Arch
The installation of SSHFS on Arch is done using either pacman package manager or AUR helpers like yay and pacaur.
sudo pacman -S sshfs
Installing sshfs on Fedora and CentOS
On Red Hat based systems, the sshfs package is not available on default repositories. You have to install epel first then install sshfs package from it.
sudo yum -y install epel-release
sudo yum -y install sshfs
For Fedora
sudo dnf -y install sshfs
Mounting remote file system over ssh with sshfs
For you to be able to mount a directory on a remote server locally, the SSH user needs to be able to access it. Once confirmed that the user can access the remote system, mounting of a filesystem is done using below syntax:
sshfs [user@]host:[dir] mountpoint [options]
Some commonly used options are:
-p PORT equivalent to ‘-o port=PORT’ –> Specify ssh port number for remote system
-C equivalent to ‘-o compression=yes’ –> Enable compression
-F ssh_configfile specifies alternative ssh configuration file –> Use different ssh configuration file than the default. -o used to specify mounting options.
So let’s consider a server with ssh port 2022, and remote location to mount being /data/backups. To mount this with compression turned on, we’ll use:
mkdir /home/jmutai/backups
sshfs outboundmx-02:/data/backups -p 2022 -C /home/jmutai/backups
Confirm if successfully mounted:
$ df -hT | grep fuse.sshfs
outboundmx-02:/data/backups fuse.sshfs 17G 5.7G 12G 33% /home/jmutai/backups
Now you can start working with the files on your machine as if it were a remote server you’re physically connected to. If you navigate to /home/jmutai/backups directory you can create a file or a directory locally and it will appear on the remote server. Any files you copy to mount directory will be uploaded automatically to the remote server as well.
cd /home/jmutai/backups
touch testfile{1..3}.txt
ls testfile1.txt testfile2.txt testfile3.txt
Check to see if files were uploaded to the remote server.
$ ssh outboundmx-02 -p 2022 "ls /data/backups"
testfile1.txt testfile2.txt testfile3.txt
Permanently Mounting the Remote File System
SSHFS allows you to set the permanent mount point to the remote file system which persists through system reboots ( local/remote). To set up permanent mount point, you’ll need to modify /etc/fstab file to automatically mount the file system each time the system is booted. The syntax is:
USERNAME@HOSTNAME_OR_IP:/REMOTE/DIRECTORY /LOCAL/MOUNTPOINT fuse.sshfs defaults,_netdev 0 0
So in our example, this will have a line like:
jmutai@outboundmx-02:/data/backups /home/jmutai/backups use.sshfs defaults,_netdev 0 0
Summary of the relevant options:
allow_other – Allow other users than the mounter (i.e. root) to access the share.
default_permissions – Allow kernel to check permissions, i.e. use the actual permissions on the remote filesystem. This allows prohibiting access to everybody otherwise granted by allow_other.
uid, gid – set reported ownership of files to given values; uid is the numeric user ID of your user, gid is the numeric group ID of your user.
To test this setting, let’s first unmount /home/jmutai/backups then mount through /etc/fstab file.
sudo umount /home/jmutai/backups
sudo mount -a
Unmounting
To unmount the remote system:
fusermount3 -u mountpoint
or:
sudo umount mountpoint
Conclusion
Locally mounted file system behaves similar to remote storage and you can do all file system operations on it, including file creation, deletion, edit, move, copy, compress, decompress e.t.c This is helpful when working on a project that needs regular updating/lots of copy to the remote server, it’s more productive than having to run scp/sftp every time you need to perform data sync.
Other SSH related tutorials available on this blog are: