So you’ve got a bunch of NFS shares on your network, and you’re tired of manually mounting them every time you need access? Let’s fix that with autofs
, a handy little tool that automatically mounts network shares on-demand and unmounts them after a period of inactivity.
In this quick guide, we’ll walk through setting up an automount point for an NFS share using autofs
on a Red Hat-based system (RHEL, AlmaLinux, Rocky Linux, etc.). The setup allows us to cleanly access remote directories under /net
, and it’s especially useful in multi-server environments.
Step-by-Step Procedure
1. Install autofs
Let’s make sure autofs is installed:
# dnf -y install autofs
2. Configure the master map
This file tells autofs where to look for mount definitions:
# cat << EOF > /etc/auto.master
/net /etc/auto.nfs --timeout=60
EOF
This means: “Hey autofs, anything accessed under /net
should consult /etc/auto.nfs
to figure out what to mount, and if it’s idle for 60 seconds, unmount it.”
3. Set up global config (optional but recommended)
We’re customizing some default autofs behavior here:
# cat << EOF > /etc/autofs.conf
[ autofs ]
master_map_name = auto.master
timeout = 300
browse_mode = no
mount_nfs_default_protocol = 4
[ amd ]
dismount_interval = 300
EOF
mount_nfs_default_protocol = 4
: forces NFSv4timeout = 300
: unmount after 5 minutes idlebrowse_mode = no
: hides mount points until accessed
4. Define your NFS mount(s)
Now let’s create the actual mount definition:
# cat << EOF > /etc/auto.nfs
remote -fstype=nfs nfs-server.company.net:/shared-dir
EOF
This sets up a mapping so that /net/remote
automatically mounts the remote NFS directory from nfs-server.company.net
.
5. Fix SELinux (if you’re enforcing)
If you’re running SELinux in enforcing mode, automount might be blocked. This command will generate and load a custom policy allowing it:
# ausearch -c 'automount' --raw | audit2allow -M my-automount && semodule -i my-automount.pp
If you’re not using SELinux, you can skip this step.
6. Enable and start the autofs service
# systemctl enable autofs
# systemctl start autofs
Autofs runs as a daemon and listens for path accesses that trigger mounts.
7. Test it!
Let’s access the directory:
# ls /net/remote
The first time you access it, it may take a second or two as autofs mounts the share in the background. Once mounted, it behaves like any other local directory.
Wrapping Up
Using autofs
is a great way to simplify access to NFS shares, especially in enterprise environments where systems are frequently rebooted or managed by multiple users. It saves system resources by only mounting what’s needed, when it’s needed.