Skip to content

Instantly share code, notes, and snippets.

@proprietary
Created April 12, 2018 13:25
Show Gist options
  • Save proprietary/96f6f08758fb98da8467880904191f64 to your computer and use it in GitHub Desktop.
Save proprietary/96f6f08758fb98da8467880904191f64 to your computer and use it in GitHub Desktop.
systemd automount sshfs: how to automatically mount a remote sshfs filesystem with systemd
# Change the relevant {{ PARTS OF THIS FILE }} for your remote address etc.
# Make sure this unit file is named similarly to your mountpoint; e.g., for /mnt/mymountpoint name this file mnt-mymountpoint.mount
# On Ubuntu:
# $ sudo cp mnt-mymountpoint.mount /lib/systemd/system/
# $ sudo systemctl enable mnt-mymountpoint.mount
# $ sudo systemctl start mnt-mymountpoint.mount
# On Fedora:
# $ sudo cp mnt-mymountpoint.mount /etc/systemd/system
# $ sudo systemctl enable mnt-mymountpoint.mount
# $ sudo systemctl start mnt-mymountpoint.mount
[Unit]
Description=Mount my remote filesystem over sshfs with fuse
[Install]
WantedBy=multi-user.target
[Mount]
What={{ USER }}@{{ HOST }}:{{ REMOTE DIR }}
Where={{ MOUNTPOINT like /mnt/mymountdir }}
Type=fuse.sshfs
# I recommend using your SSH key (no password authentication) with the following options so that you don't have to mount every time you boot
Options=_netdev,allow_other,IdentityFile=/home/{{ MY LOCAL USER WITH SSH KEY IN ITS HOME DIRECTORY }}/.ssh/id_rsa,reconnect,x-systemd.automount,uid=1000,gid=1000
# Change to your uid and gid as well according to the output of `cat /etc/group`
@mcejp
Copy link

mcejp commented Sep 4, 2021

I couldn't get this to work, but this guide did it for me: https://blog.tomecek.net/post/automount-with-systemd/

@mopore
Copy link

mopore commented Jan 10, 2022

Zelly's file worked perfectly for me 👍

@phockett
Copy link

Also worked nicely for me on Ubuntu 20.04LTS. I added some After commands to ensure my network stack was up first, although this may not be necessary - but I'll add it here in case useful for anyone else (here tailscaled == Tailscale daemon).

[Unit]
After=network.target
After=tailscaled.service

@ki9us
Copy link

ki9us commented Nov 3, 2023

I believe the name of this file must match the mountpoint, with the slashes replaced by spaces.

For example, to mount at Where=/mnt/mymountdir, the file must be named mnt-mymountdir.mount.

@ki9us
Copy link

ki9us commented Nov 3, 2023

Also, Type=sshfs worked for me.

@Martzy303
Copy link

Any idea how to make it show in the file explorer as a device?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment