How to Automatically Mount Remote Shares on Demand in Linux Using AutoFS

Hardcoding remote Network File System (NFS) or Server Message Block (SMB/CIFS) mount points directly into /etc/fstab is common practice, but it carries operational drawbacks. If a network fileserver is offline or experiences latency during client boot, the system may hang or drop into emergency rescue mode. Furthermore, maintaining permanent network connections consumes system sockets and server bandwidth even when no applications are actively accessing shared storage.

The Linux automounter utility (AutoFS) solves this by managing mount points on demand through kernel-level interception. When a user, script, or service accesses a designated target directory, AutoFS immediately mounts the remote export. Once file operations cease and an inactivity timeout elapses, AutoFS unmounts the resource cleanly, preserving stability and bandwidth.

Installing the AutoFS Package on Debian, Ubuntu, and RHEL

AutoFS is available across all major Linux distributions via their default package repositories.

  1. Launch your terminal and update your local package index:
    sudo apt update
    (On Fedora, AlmaLinux, or RHEL, run: sudo dnf check-update)
  2. Install AutoFS and necessary network filesystem support packages:
    sudo apt install autofs nfs-common cifs-utils -y
    (On RHEL/CentOS systems, execute: sudo dnf install autofs nfs-utils cifs-utils -y)
  3. Verify that the AutoFS service is present and inactive before configuring maps:
    systemctl status autofs

Configuring the AutoFS Master Map File

AutoFS uses a master configuration file located at /etc/auto.master to define top-level mount directories and associate them with subordinate map files.

  1. Open the master map file with a root text editor:
    sudo nano /etc/auto.master
  2. Append a new mapping directive at the bottom of the file. For example, to manage dynamic mount points within a base directory named /mnt/shares:
    /mnt/shares /etc/auto.nfs --timeout=60 --ghost
    • /mnt/shares: The parent directory where automatic sub-mounts will reside.
    • /etc/auto.nfs: The secondary mapping configuration file detailing individual remote shares.
    • --timeout=60: Instructs AutoFS to automatically unmount inactive shares after 60 seconds of idle time.
    • --ghost: Creates visible empty directory placeholders inside /mnt/shares so users can see available shares in graphical file managers without triggering unnecessary network calls prematurely.
  3. Save the file and exit the editor (Ctrl+O, then Ctrl+X in nano).

Creating Secondary Map Files for NFS and SMB Exports

Next, define the remote file server addresses and export paths inside your custom map file.

  1. Create and open the secondary map file:
    sudo nano /etc/auto.nfs
  2. To define an NFS share from a network storage host (for instance, IP 192.168.1.50 exporting /srv/nfs/backups):
    backups -fstype=nfs4,rw,soft,intr 192.168.1.50:/srv/nfs/backups
  3. To mount an SMB/CIFS Windows or Samba share using stored credentials:
    media -fstype=cifs,credentials=/etc/auto.smb.credentials,uid=1000,gid=1000 ://192.168.1.50/shared_media
  4. If using SMB credentials, create /etc/auto.smb.credentials, add your username=... and password=..., and restrict file access permissions strictly to root:
    sudo chmod 600 /etc/auto.smb.credentials
  5. Save your secondary map file and exit.

Starting and Verifying the AutoFS Service

Activate the automounter daemon to start monitoring filesystem calls.

  1. Enable and start the AutoFS systemd service:
    sudo systemctl enable --now autofs
  2. Check the operational status:
    systemctl status autofs
    Ensure the output indicates active (running).
  3. Test the on-demand trigger by accessing the mount point:
    ls -la /mnt/shares/backups
    AutoFS immediately queries the network server, mounts the remote directory transparently, and outputs the directory contents.
  4. Verify active mounts in the Linux kernel table:
    df -hT | grep nfs
    The NFS export appears in the list. Wait 60 seconds without running any commands inside that path, then re-check df -hT to confirm that AutoFS has cleanly unmounted the share.

Troubleshooting AutoFS Connection Failures

  • Permission denied on mount attempt: Verify that the export on the remote NFS server permits access to your client IP address in /etc/exports (for example, ensure rw,sync,no_subtree_check permissions are configured).
  • Firewall blocking RPC and NFS ports: Ensure TCP/UDP port 2049 (NFS) or ports 139 and 445 (SMB) are open across all network firewalls and local UFW/iptables rules.
  • Debugging map syntax errors: Stop the service and run AutoFS interactively in verbose foreground mode to trace parsing errors:
    sudo systemctl stop autofs
    sudo automount -f -v

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.