1

I want to use sshfs to mount a directory from the remote system automatically when I make the first ssh connection and to unmount the directory when I finally disconnect.

I currently use ssh's ControlMaster facility so that I only have to enter the password the first time I connect to the remote. I do often have several ssh connections running simultaneously.

I guess I can have a script run from ssh when it connects (I seem to remember there's a way to do this) and the script can check for the existence of the mount before trying to do it.

However I'm not sure how to clear down the mount when the last ssh (the master one) exits.

0

The option for running a command when connecting is LocalCommand, which must be enabled by setting PermitLocalCommand to yes, e.g.

ssh -oPermitLocalCommand=yes -oLocalCommand="sshfs server:dir /mnt &" myhost

Note the &, without it the actual connection will not open until sshfs exits. This only runs on the master (so it would be safe to add this to ~/.ssh/config).

There is no equivalent for running a command on exit, but as long as you use sshfs on the same host as the master connection, it will use that connection. When your shell in the master session exits, the session will not close, until any other connection is closed or the master is terminated. So if you type Ctrl-C after exiting your shell, the sshfs process will be terminated too and unmount the filesystem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.