Easy Guide to Using AutoFS to Connect to Windows CIFS Shares

AutoFS is a powerful tool that allows for mount-on-demand functionality in Linux, reducing the chances of any negative effects when rebooting Windows running file services. In this article, we will focus on how to correctly connect AutoFS to a Windows share, without covering advanced features such as dynamic maps or special cases with different mappings.

Why Use AutoFS?

Unlike regular static mounts defined in the /etc/fstab file, AutoFS mounts the target directory only when it is accessed. A predefined directory needs to be set, and when the AutoFS service is enabled, it takes ownership of this directory and hides any existing files or directories in it. These files or directories are not deleted, but only hidden, and become visible again when the service is stopped.

When a client points to a target directory (“key”) in this predefined directory, AutoFS checks its configuration file (usually /etc/auto.misc) for an entry with that name. If an entry exists, it attempts to mount it using the specified mount command, which can include scripts, regular expressions, and more for flexible handling of maps. If the mount succeeds, the mount point becomes visible and remains mounted as long as it is in use. If the mount command fails or there is no key in the map file, an IO error will result, and the calling application will receive an IO error, similar to when a directory does not exist.

This flexibility makes AutoFS a useful option for less-stable network drives, such as Windows shares.

How to Set it Up?

To demonstrate the process, we will use the /misc directory as an example of a predefined directory, assuming it is already configured in the auto.master file. However, it’s important to note again that if a directory is defined in auto.master, any existing files or directories in that directory will not be visible when the AutoFS service is active, although the data is not lost.

  1. Make sure /misc is managed by AutoFS by checking the entry in the /etc/auto.master file:
    /misc /etc/auto.misc
    This means that the directory /misc is handled by the configuration file /etc/auto.misc.
  2. Add a line in /etc/auto.misc for the Windows share you want to connect to (a single line):
    backup -fstype=cifs,rw,uid=1000,gid=1000,credentials=/etc/cifs.creds ://fileserver.myfqn.local/network-backup
    This line has three fields: key, parameters, and location.
    • The key is “backup”, meaning that if we attempt to access /misc/backup, the mount command will trigger, attempting to mount the specified location (//fileserver.myfqdn.local/network-backup) to /misc/backup automatically.
    • The parameters field includes the filesystem type and additional mount options. In this example, we are mapping the share to be owned by UID 1000 to avoid issues with user ID mapping to a Windows share. For more mount parameters, refer to the man page for the mount.cifs command.
    • The credentials file is important, as it allows us to specify a credentials file for mount.cifs to authenticate with the Windows share. The file format should include the username, password, and domain. In this example, the file is /etc/cifs.creds, and its permissions should be set to 600 to protect it from being world-readable. Since AutoFS runs as root, there are no access issues.
      The file format is as follows:
  3. Once all the configurations are defined correctly, enable the AutoFS service using systemctl restart autofs or systemctl enable --now autofs, and then attempt to access /misc/backup. If everything is set up correctly, the mount command should succeed, and you will be able to see the contents of this destination. If not – check the system logs for any error message which might point you at the reason it is not working.

Good luck, and drop me a comment if it works for you, and even if it doesn’t đŸ™‚

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.