Using the Mac OS X automounter

A quick tip on how to use the Mac OS X automount command to automatically mount a remote filesystem. You will need to be comfortable with the OS X command-line for this but once you know how it is fairly simple.

Last updated: Feb 5, 2020

Why automount may be useful

I have a number of remote filesystems that are stored on Network Attached Storage (NAS) devices connected to my local network wireless router. For example, I use a share on a NAS device to host all of my media for use with the Plex Media Center. Anytime I run Plex on a computer I need to mount that share in order to be able to play the media. Likewise I have a number of cron scripts that run nightly to sync local filesystems on my Mac with a share on the NAS device.

(As an aside I should mention that remote syncing is not intended as a replacement for backups or a version control repository for your Xcode projects).

Now it would be a pain if I had to manually mount those remote filesystem shares everytime I wanted to run Plex or some other application or script. One possible option is to have them permanently mounted by adding them as login items to my OS X account (mount the share and then drag the disk icon from your desktop into the Login Items tab of your account settings in the System Preferences application). However these are filesystems that I do not generally want to see on the desktop and which sometimes are not even reachable.

A better approach in this case is to use the automount service that is part of Mac OS X. A few very simple configuration steps will allow the automount service to automatically manage access to a remote filesystem mounting it only when it is accessed and unmounting it later when it is no longer being used.

Create a local mount point

To get started you need to create a local directory that will be managed by the automount service. All remote filesystems will appear under this directory which acts as a trigger to the automount service. I keep these remote filesystems under the directory /mnt/Resources. The Resources directory is created and managed by the automount service but you will need to create the /mnt directory if it does not exist. You will need to use sudo to create the directory.

Note that starting with Catalina the root file system is now read-only. I have moved the mount point to the user writable filesystem /System/Volumes/Data:

$ sudo mkdir /System/Volumes/Data/mnt

Modify the master map

The automount process reads the file /etc/auto_master to retrieve a list of directories that it should control. I’ve placed my resource maps in a separate map file (/etc/auto_resources). The one line modification to add /System/Volumes/Data/mnt/Resources to the auto_master file is as follows:

## Automounter master map
/System/Volumes/Data/mnt/Resources auto_resources
+auto_master            # Use directory service
/net                    -hosts    -nobrowse,hidefromfinder,nosuid
/home                   auto_home -nobrowse,hidefromfinder
/Network/Servers        -fstab
/-                      -static

So any time an attempt is made to access a directory under /System/Volumes/Data/mnt/Resources the automount process will check the auto_resources map file to determine if it needs to mount a remote filesystem.

Creating the resources map

Let’s assume we have a remote filesystem /media on a NAS device named nas001. Also we will assume the filesystem is formatted as a Microsoft Windows (SMB) filesystem as it is also accessed by Windows devices (true in my case). To automatically mount that filesystem anytime we attempt to access it under /System/Volumes/Data/mnt/Resources/media we need to add a map to the /etc/auto_resources file as follows:

media   -fstype=smbfs ://nas001/media

Notes:

  • This assumes that anonymous access to the filesystem is allowed otherwise you need to include a username and password (e.g. ://username:password@nas001/media).
  • The hostname of the remote device must be in the local /etc/hosts file or otherwise defined by DNS. On my local systems I simply include it in the /etc/hosts file.
  • You can add other remote filesystems to the same map file and they will also be mounted under /System/Volumes/Data/mnt/Resources. Add each new filesystem map on a separate line.

Activate the changes

Any time you modify the map files you need to inform the automount service in order for them to become active:

$ sudo automount -vc

Optional configuration changes

A number of options can be changed in the /etc/autofs.conf configuration file. The only option that you are likely to ever want to change is the timeout period. This is time that the automount service will wait before unmounting a filesystem that has not been accessed. This is set to 1 hour (3600 seconds) in the /etc/autofs.conf that ships with OS X:

AUTOMOUNT_TIMEOUT=3600

Finder integration

It should be noted that filesystems that are automounted in this way will not show up automatically in the sidebar of the OS X Finder application. Nor will they automatically appear as disk icons on the desktop. If that is important you may want to simply add the share as a login item and have it mounted whenever you login.

For filesystems that are accessed by scripts or applications I prefer having them automatically mounted and unmounted in the background on demand. You can of course always go to the directory in the Finder using the Go > Go To Folder… command and entering /mnt/Resources.