I work pretty often on a server cluster. To do so I use (like mostly everybody else) ssh. When working with files or a remote environment sshfs
is a reliable tool to mount remote storage on your computer. On a laptop, the automount functonality on boot might not be optimal, since wifi is not always available.
Automounting sshfs
on demand is a pretty versatile solution. It only mounts the directory on the first access. To do so adjust your /etc/fstab
and add another row:
user@REMOTE:REMOTEPATH LOCALPATH fuse.sshfs noauto,x-systemd.automount,_netdev,users,idmap=user,IdentityFile=$HOME/.ssh/id_rsa,uid=1000,gid=1000,allow_other,reconnect,comment=x-gvfs-hide,compression=yes,port=xxx 0 0
The options are as follows:
- noauto does tell fstab not to mount this device on boot.
- x-systemd.automount does the magic.
- idmap and IdentityFile are used as login credentials. Note that in order to work, the root user on your device needs to connect at least once with the server. Moreover the server needs to be access in a non-password fashion.
- compression allows for less data traffic thus making the connection faster.
- x-gvfs-hide masks the directory in being invisible to gvfs ( if there are any gvfs problems that might help )
- port is the port of the ssh server.
- Identitiyfile is the previously generated ssh id file. First run
ssh-keygen
thenssh-copy-id USER@SERVER
to enable auto login onto the specific server.
Troubleshooting with GVFS (Xfce)
In my case, that did not prevent automount from being fired directly
after start leading to errors during mount. The bad guy here is gvfs, specifically thunar-volman
and gvfs-trash
. Both of them seem to access the sshfs mounted directory on start.
Another possible solution is to turn the switch AutoMount=true
to AutoMount=false
in the file
/usr/share/gvfs/mounts/trash.mount
Another alternative: Autofs
While sshfs
is powerful indeed, I experienced quite some problems regarding the auto dismount function. Especially when I previously opened any sshfs
mounted folder in sublime and then disconnected, the remote sshfs will hang. This is pretty frustrating, since even restarts will not work (but a restart of sshfs will do sudo systemctl restart sshfs
).
Another alternative is autofs, which provided much more functionality compared to sshfs
, e.g. ftp, nfs, samba support.
For a simple config, follow the following instructions:
- Install autofs. In arch its just
sudo pacman -S autofs
- Autofs generates couple of files in
/etc/autofs/
. At first, open the file/etc/autofs/auto.master
with e.g., vimsudo vim /etc/autofs/auto.master
. I personally commented the lines/net -hosts
and/misc/ /etc/autofs/auto.misc
since I dont use any misc formats or mounting points. - At the end of the opened
auto.master
file, add a line:/- /etc/autofs/auto.ssh uid=1000,gid=1000,--timeout=10,--ghost
. The parameters mean:/-
Mount absolute path (given in theauto.ssh
config). If/-
is replaced with e.g.,/ssh/
, all mounts in theauto.ssh
config will be relatively mounted to/ssh/
. I personally have two mounts which need to be placed in the root dir, thus need to use/-
.uid=1000,gid=1000
Mount as root--timeout
Time until the mount will be dismounted (if a disconnect occured)--ghost
Keep a “dummy” folder as the mount folder, even if the directory is not mounted. This is very helpful for sublime.
- Open the file
/etc/autofs/auto.ssh
, where you can configure your ssh mounts. The following syntax is used in this file:/MOUNTPOINT -fstype=fuse,rw,allow_other,reconnect,no_check_root,compression=yes,port=YOURPORT,IdentityFile=/home/YOURUSER/.ssh/id_rsa :sshfs\#REMOTEUSER@SERVER\:/REMOTEDIR
You can add multiple ssh mounts in this file. Note that in order to work, your user needs to have password-less login enabled (ssh-copy-id
), as well as the necessity that the root user at least logged into the server once ( to obtain the RSA fingerprint ) - Enable/Start autofs
sudo systemctl start autofs
,sudo systemctl enable autofs
and enjoy! The mounts will be at the paths specified inauto.ssh