Automatic remount cifs/SMB after short network interruption
A Windowsserver provides a network directory called data via CIFS/SMB protocol. The data folder is mounted on a Linux client with password and user authentification.
Sometimes the drive gets disconnected, but is available again after a second. I do not know exactly why, but it seems to be because of the Windows server or a broken network component.
How can I make Linux reconnect automatic as soon as possible?
I'd recommend mounting it via autofs
. This is a service that will mount a directory on demand (for example if you cd
into it or ls
it) and unmount it automatically after a user defined timeout.
Install the
autofs
package for your distribution (by the way, remember to include your distro in your questions since an answer's details may depend on it).-
Add the following to /etc/auto.master
/media/[my_server] /etc/auto.[my_server]
Where
/media/[my_server]
is the mount point of the share. -
Create a file /etc/autofs/auto.[my_server] with this line:
[any_name] -fstype=cifs,[other_options] ://[remote_server]/[share_name]
For more information see here and here.
To add to the autofs answer, I recommend doing the the way it is described here:
https://andrewaadland.me/2017-06-18-autofs-nfs-and-archlinux-key-not-found-in-map-sources/
That is:
- Make the first field in
auto.master
is always/-
. - Use full mount name in
/etc/autofs/auto.server
.
So in my case, /etc/autofs/auto.master
contains:
/- /etc/autofs/auto.nas
And /etc/autofs/auto.nas
contains:
/home/rkitover/nas -fstype=cifs,credentials=/home/rkitover/.nascredentials,uid=1000,gid=1000,iocharset=utf8 ://nas/rkitover
This works for me!