Mount a remote filesystem over SSH using SSHFS
While creating and debugging software, it is important to reduce the amount of friction between each iteration of making a change to the software and then testing that change. Over time, even small amounts of friction can lead to fatigue and decreased performance of a developer. Because of this, we should take every opportunity to make our workflow as smooth and comfortable as possible.
A common source of friction when developing software running on remote systems is the separation between your personal computer and the server. Your personal computer likely has an IDE configured just the way you like. The server, on the other hand, is likely configured to be easily available to everyone on your team.
You could copy files back and forth between systems using SFTP or some other file transfer protocol. This works for quick one-off changes, but for development requiring multiple iterations you likely want a more streamlined workflow.
If only there was a way to use the software installed on your personal system to edit files on a remote system, without copying the files back and forth…
There is! SSHFS is a tool for mounting and interacting with remote directories over SSH.
Notice
The SSHFS repository has been archived for around a year at the time of writing; the latest release was on May 26, 2022. The repository now has a note about being orphaned and inviting other developers to take over the project. While there are a few forks (most notably deadbeefsociety’s and stephenxxiu’s) with some traction, none is a definitive replacement yet.
One alternative suggested by the ArchWiki is using rclone’s mount feature.
For macOS users, it’s important to note that macFUSE is no longer open-source as of 2017.
Installation
This guide contains installation instructions for macOS and Linux systems only.
If you are using Windows, check out sshfs-win.
macOS instructions
Download the macFUSE and SSHFS packages from the osxfuse website and install them as an administrator.
Linux instructions
SSHFS is available from the package repositories of most distributions. This is usually the preferred way to install software on Linux.
On Debian and Ubuntu systems, the following command will install the latest version of SSHFS available:
sudo apt update && sudo apt install sshfs
Usage
After installation you may interact with SSHFS via the sshfs
command.
Before we mount a remote directory, let’s create a local directory that will serve as the local mount point:
mkdir /tmp/remote
Let’s say we want to mount the remote directory /var/www/html
of the remote
system at example.com
while connected as the remote user wildfly
. We want to mount this
remote directory at the local directory /tmp/remote
. To accomplish this, we
would execute the following command:
sshfs -o compression=no,cache=yes,cache_timeout=20,auto_cache,idmap=user wildfly@example.com:/var/www/html /tmp/remote
The following mount options are given using the -o
argument:
compression=no
- Disable compression. The performance impact of enabling compression is not worth the minimal bandwidth savings.cache=yes
- Enable caching. Files that are repeatedly accessed within a certain period of time will only be retrieved once.cache_timeout=20
- Set cache timeout, in seconds.auto_cache
- Enable caching based on file modification times.idmap=user
- Translate between UID of connecting user and remote user. This causes remote files to appear to be owned by you, even though they are actually owned by the remote user on the remote system.
To unmount on linux, run fusermount -u /tmp/remote
. On macOS, run umount /tmp/remote
.
Comments