Klaus Zimmermann's Corner

Enabling SFTP mounts on Alpine Linux and FreeBSD

Another day, another thing learned. In many "out-of-the-box-preconfigured" Linux distros, it's possible to mount remote locations directly into your filesystem so that you can access them directly through your graphical file manager via a URI such as sftp://yourmachine:/mountpoint, as though it is an ejectable drive. You can see this behavior on Ubuntu, Fedora, Debian and even Artix.

As I venture across different OSes, though, I find that many times this behavior is not consistently reproduced, with most of the times cryptic error messages such as "Operation not permitted" being displayed instead. And even though it took me quite a while, today, I'm glad to say that I've figured a way to consistently be able to mount remote files with more minimalist distros, like Alpine or FreeBSD. It goes like this:

The sshfs package does exactly what it seems to: mounts remote filesystems via SSH. Step one is to install it. Here's Alpine:

# apk add sshfs

The command syntax is very similar to mount's so it goes like this (note how you run it as a normal user):

$ sshfs user@myserver:/my/remote/mountpoint /media/local_mountpoint

If this command fails due to a missing kernel module, do:

# modprobe fuse

Bonus points to make local_mountpoint owned by your user so you can truly run the command as a regular user without any need for root! And after that, have a look at your file manager: a mount for your SFTP directory should have appeared, which you can access and transfer files to and from at will. Victory!

And then challenge number two: FreeBSD. It has the same program packaged neatly to be used, but the same command as above will fail mysteriously. Here's why: FreeBSD's VFS tunables prevent mounting of filestystems by regular users by default, and the error messages can be a little misleading in this case. So here's how you actually fix it:

# sysctl vfs.usermount=1

No need to touch other sshfs config options. After that, voilĂ !

The upshot of this method is that since it works with the "barebones" of OS mounting mechanism, this might make it as universal as possible a solution, so long as the OS supports sshfs. Very, very useful to access my NAS and other shares I have across the interwebz!

Do you know another way to make this seamless integration of SFTP share mounting? Please let me know on Mastodon!