How to auto mount using sshfs?
I am using the following command to mount a ssh ubuntu directory to my ubuntu pc.
sshfs [email protected]:/dir/dir /home/username/mount/xxx
My question is, can I create a script for this in my desktop where I can make a double click and run this script when ever I need to mount the drive without manually typing the command always.
You could create a launcher and add it to your launcher bar by drag&dropping the .desktop
-file there:
#!/usr/bin/env xdg-open
[Desktop Entry]
Version=1.0
Type=Application
Terminal=false
Icon[en_US]=nautilus
Name[en_US]=Connect to xy
Exec=shfs [email protected]:/dir/dir /home/username/mount/xxx
#OR: to mount and than open in nautilus (note the '/dir' where ':dir' used to be)
#Exec=nautilus sftp://[email protected]/dir/dir
Comment[en_US]=Connect to xy via ssh
Name=Connect to xy
Comment=Connect to xy via ssh
Icon=nautilus
Suggestion - even less work:
If you want even less work (=autoconnect) and a graphical user interface, you might want to check out Gigolo . It has the capability of auto-mounting a bookmark, whenever the bookmarked filesystem is present. You might want to check that out.
sudo apt-get install gigolo # or use the install link above
Run gigolo
. There is an option in the preferences that puts it into autostart and another to activate the tray icon. Check both. Then add your bookmark.
Here is a screenshot:
Shell way
Another solution would be to put the following line in your crontab (edit /etc/crontab
with sudo privileges):
@reboot sshfs [email protected]:/dir/dir /home/username/mount/xxx
But since Ubuntu's password manager is not present when the command is run you need to use a password-less private/public key pair to authenticate with the ssh server in question (or a similar method of authentication). This would mount it on every reboot.
Yet another solution would be to edit your /etc/fstab
(providing your Ubuntu-Version provides that option).
I am adding an fstab
method, since no one talks about it in this page. If you don't want hacks and use the builtin advanced mounting features, you need to use /etc/fstab
and never look back.
user@host:/remote/folder /mount/point fuse.sshfs noauto,x-systemd.automount,_netdev,IdentityFile=/home/name/.ssh/id_rsa, allow_other,reconnect 0 0
-
noauto
will stop the no-brainer actions like forcibly mounting whatsoever at booting regardless if the network is up or not. -
x-systemd.automount
is the smart daemon that knows when to mount. - The
_netdev
tag will also identify that it uses network devices, thus it will wait until the network is up.
This forum thread shows a method of creating an automounting SSHFS which seems to me exactly what you would like to do.