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.
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.
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._netdev
tag will also identify that it uses network devices, thus it will wait until the network is up. You could create a launcher and add it to your launcher bar by drag&dropping the
.desktop
-file there: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.
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):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).This forum thread shows a method of creating an automounting SSHFS which seems to me exactly what you would like to do.
You can simply type this to a shell script, and you can create a launcher for it at the desktop.
For example
mountssh.sh
:make sure to
chmod +x mountssh.sh
and then clicking it will executeAlternatively, you can mount it via gvfs, by right clicking at the desktop, and creating a launcher with URL parameter:
ssh://[email protected]/dir/dir
. By default it mounts to~/.gvfs/...
. If you want stick with the/home/username/mount/xxx
, you can create symlink from the gvfs one to this.You could even take it a step further and have autofs take care of the mounting for you. Since autofs doesn't work particularly well with SSH public key authentication (unless you want to create a passwordless key pair for the superuser), there are tools that allow you to use the user's SSH keys, ssh-agent and keychain:
I mount a folder the exact same way, what i did was create a custom launcher that points to a
.sh
file that contains the command. Just make sure the file has execution permission and you're good to go.I just click on the launcher:
I tried to use cron to automatically mount ssh directory, but it causes an error saying
Network is unreachable
. It is because the cron job execution is too early to establish ip connections. After I inserted sleep before thesshfs
command, it successfully mount the ssh directory.So I made this script to fulfill my requirement.
Since none of the answers worked for me and I managed to figure this out, I figured I should share it with the world. My solution also autoreconnects when the mount disconnects and works when the machine boots up without problems. Just add this to your
/etc/fstab
file (one-liner):Some notes:
You NEED SSH keys for this apparently. You can copy your SSH keys using
ssh-copy-id
or manually - which one you prefer. The automounting only works with SSH keys in other words. You can troubleshoot the SSH connection with editing the fstab file and then callingmount -av
.allow_other
allows when the root has mounted the automount that not only the root can access it.IdentityFile
is the necessary pointer to the SSH key as evidenced in Arch Wiki, but apparently not described what syntax it uses. Just don't forget to replace the placeholders for uid and gid ;) I think you can comma separate the IDs, but I haven't tried it.This was super useful for my Nextcloud instance, since I wanted to remotely mount an external storage box and
follow_symlinks
allows the required functionality for supporting symlinks in the mount, which is required for Nextcloud when you start using external storage and similar stuff.