I have a server I connect to using ssh in pantheon-files, how do I make the network file system remain mounted after reboot?
2 Answers
The easy way to mount a filesystem during boot is to list it in /etc/fstab
. However, SSHFS is a FUSE filesystem, so mounting it from a system configuration file is tricky: you wouldn't have permission to access it under your account.
While it's possible to get this done, it's easier to do the mounting from your account.
You can use cron to schedule a task at boot time to mount that filesystem. Run the command crontab -e
to edit the list of scheduled tasks for your account, and add a line:
@reboot sshfs remotehost.example.com:/remote/path /path/to/mount/point
Note that the sshfs
mount command won't have access to any interactive environment, so it won't be able to ask you for a password or for a key passphrase. If you need that, you'll need to arrange to run the command when you log in, not when the system starts.
If the network connection isn't available at boot time, you may want to do the mounting when the network becomes available. To do that, add a file to the if-up.d
directory called john-mount-sshfs
and containing the following code (replace john
by your username, and replace the host name and paths):
#!/bin/sh
su -c john <<'EOF'
if ping -c 1 remotehost.example.com >/dev/null 2>/dev/null; then
sshfs remotehost.example.com:/remote/path /path/to/mount/point
fi
EOF
If you need to supply credentials to the sshfs
command, then autosshfs is likely to be more convenient. It mounts the directory on first access, so you can supply the credentials at that time.
-
What you've prescribed here would work, with a lot of caveats about where to semi-securely store the credentials. Then, and now, I think using the login keyring to store the remote credentials along with the drive-mounting and starting-a-program-on-login capabilities of the graphical desktop environment would probably suffice for the OPs purposes.– Kevin ECommented Jun 25, 2023 at 22:26
-
Then again, what you've prescribed will still work in ten years, and who knows what will come after
gio mount
in the meantime.;)
– Kevin ECommented Jun 25, 2023 at 22:31
Depending on your workflow, you may be able to get by with just a sidebar bookmark:
- connect to the remote system the first time with Files
- click-and-drag the mounted network volume from "Network" up to "Bookmarks"
- then, next time you log in, click on that bookmark to reconnect the remote volume.
The downside is it won't be accessible from other applications (e.g., LibreOffice) until after you open Files for the first time and click the bookmark.
If the remote volume must be mounted at login, an alternative is to create an autostart desktop entry that uses gvfs-mount
(for Freya) or gio mount
in newer elementary OS releases. Hypothetically, this method could work for other kinds of volumes, too, provided your user has appropriate permissions to mount them.
Since you refer to connecting to the SSH server with Pantheon Files, I assume you mean "mount on login" rather than "mount on boot" (which implies that the network filesystem would be mounted independent of the graphical desktop).
If you're OK with mounting on login, either:
- connect to the server with the Files app the first time and allow it to store the password in your login keyring
- —or— use
ssh-copy-id
to copy your SSH public key to the remote system such that you canssh thehostname
without having to give a password- if you don't have a
~/.ssh/id_SOMETHING.pub
already, usessh-keygen
to create one; be sure to specify a password for the private key (which the desktop environment should offer to store for you the first time you use it)
- if you don't have a
Then add gvfs-mount sftp://username@hostname/path/to/dir
(in newer releases, gio mount
) to your autostart config. You can either do this with System Settings → Applications → Startup, or else create a Desktop Entry file in ~/.config/autostart
under your user account.
Here is the created file:
# ~/.config/autostart/custom-command0.desktop
# You can rename this file and/or edit 'Name' below to make it more descriptive
[Desktop Entry]
Name[en_US]=Custom Command
Comment[en_US]=gvfs-mount sftp://username@hostname/path
Exec=gvfs-mount sftp://username@hostname/path
Icon=application-default-icon
X-GNOME-Autostart-enabled=true
Type=Application
If you create an appropriate ~/.ssh/config
entry for hostname
, you can specify the username, key file, or any other necessary SSH options, without needing to hard-code those into the autostart desktop entry.
Hypothetically, you could place the .desktop
file in /etc/xdg/autostart
instead (as root) and have it run on login for all users. You'd have to figure out how to solve the problem of specifying the remote username, though, if it differs from the local one. I don't think Desktop Entry files understand environment variables.
Editorial
Re-mounting a network volume when you log in seems like something that Files itself should support, but the only issue I'm aware of where this was brought up was closed as WONTFIX. I guess that's OK, because this is a pain on all three major operating systems, Windows being the possible exception, since the "Map Network Drive" dialog has a "Reconnect at sign-in" checkbox that works most of the time.
One serious downside of using gvfs-mount
/ gio mount
is the weird filesystem path where network volumes get mounted. On Ubuntu-derived systems, it's something like:
/run/user/YOURUID/gvfs/proto:host=hostname,user=username/path
You could make symlinks in ~/net
or something, but that's a pain to maintain if you connect to a lot of different hosts.
Then there's the fact that non-GTK apps don't always understand GVFS/GIO mounts or show them as removable devices, forcing you to browse to that weird path. Drag-and-drop from the file manager to these apps often doesn't work. There's probably no clear fix for that.
As an aside, I think this is something OS X got right, and maybe NeXTSTEP before it: all mounted volumes including network volumes are under /Volumes
, with simple names. But I just don't see how that could work in a multi-user environment on Linux.
-
I've verified that this works in elementary OS 6.1 Jólnir (using
gio mount
instead ofgvfs-mount
), so this answer should be good for modern elementary desktops, too.– Kevin ECommented Jun 25, 2023 at 22:09