Keep Network Drives Mounted on Mac OS X using Autofs

I have a Apple Mac Mini that I use as a HTPC using Plex. Connected to the Mac are two USB external drives, one called Media for movies, TV shows, etc… and the other one called PersonalMedia for photos and camcorder/home videos. I recently bought a Synology NAS, and copied the contents of the PersonalMedia into the Synology using RAID 1 redundancy (2x 2TB WD Red). With the NAS mounted using Finder and adding it to the “Login Item” for automatic mounting, I was all set, or so I thought…

The Synology NAS is set to spin down the disks after 20 minutes of inactivity and wake on LAN when requested (WOL), which is great as it’ll preserve the life of the disks and of course use less power consumption when not in use. The problem is that the Mac will only attempt to connect to the NAS on power up from cold start/restart or from sleep. So if I’m using the Mac, but not accessing the PersonalMedia share, then after 20 minutes the NAS will go to sleep and the network drive will disconnect, and the only way to reconnect is using the Finder option, Connect to Server [CMD+K] or performing a restart.

This is more of an issue with Plex, as the disconnected share is not accessible when you try to access the Plex library for that share, Plex returns an error – understandibly as the drive is no longer available.

autofs is a utility that will mount network volumes on your OS X system, just like you would do through the Finder. However, unlike drives mounted manually through the Finder, or even by a “Login Item” at login time, autofs mounted volumes will automatically connect and reconnect as needed whenever the network volumes are available, and quietly disconnect when they’re not. The same benefits apply to devices that may live permanently on your network, but regularly go to sleep and need to reliably re-mount volumes on wake.

Using autofs means that whilst my Mac is awake, it will continously attempt to mount the PersonalMedia share on the NAS – whether it’s being used or not – effectivily disabling the NAS’s ability to sleep. Then 20 minutes after the Mac has gone to sleep, so will the NAS (assuming no other resource is accessing the NAS) – Win Win!

The first column /mnt/NAS specifies where you would like your mounted volume to appear. You can replace this with a path of your choice.

On Mac OS X 10.9 and later, do not use /Volumes as your mount point since everything that’s not a physical volume (hard disk), including your mount point directory, will be removed whenever the machine is rebooted.

The second column is the name of a file that we will create in Step 5. It can also be named anything you want.

Step 4 – Save auto_master

When done, press Control+X to exit.

Press Y when prompted to save the changes

Press Enter to accept the suggested path

Step 5 – Create auto_nas

We need to create the file that autofs will refer to for details about your individual mounts. With the Terminal window still open, create the auto_nas file

$ sudo nano /etc/auto_nas

Type your user password when prompted.

Step 6 – Edit auto_nas

This will bring up a text editor containing the new empty auto_nas file.

In this example, PersonalMedia is the name of the directory that will appear under your mount point specified in Step 3. -fstype=afp tells autofs to use the AFP share protocol, similarly -fstype=smbfs tells autofs to use the SMB share protocol. Replace username:password values with your own. Finally, replace the IP address or hostname with that of your NAS, and the path with the name of your share.

The hostname of the remote device must be in the local /etc/hosts file or otherwise defined by DNS. On my local system I simply include it in the /etc/hosts file.

Step 7 – Save auto_nas

When done, press Control+X to exit.

Press Y when prompted to save the changes

Press Enter to accept the suggested path

Step 8 – [OPTIONAL] Security

Since the auto_nas file potentially contains password information, you may want to make sure only the root user can read it. Enter the following command in the Terminal window

$ sudo chmod 600 /etc/auto_nas

Step 9 – Create mount directory

If the directory you specified in Step 3. (auto_master) above doesn’t exist for your mount to appear, you’ll need to create one, either using Finder or Terminal. In my case I needed to create the directory structure /mnt/NAS.

Step 10 – Execute autofs to mount the shares

With the Terminal window still open, enter the following command to mount the new shares

Hi Michael, I haven’t noticed any issues, to be honest. As Plex Server is running on the Mac Mini, the library is already up and running so there so by the time I start Plex HT and navigate to the media I want to watch, and then click to play, the drive is already mounted and ready to go.

Thanks for this detailed overview, I’m having this exact problem since a recent upgrade (either to El Capitan or DSM 5.2) and it’s driving me crazy !

I’m following all the steps and i get a mounted share appear in the location i asked… but the file size is zero and the contents of the NAS aren’t visable.

The frustrating thing is I’ve had it working once, but then stupidly tried to change the location of where i wanted the mounted drive to appear, it broke again. Now no matter what I try i just can’t get it to mount and show me the files.. Help !

(I can acces my NAS so I know its on the network, i just want it to stay there

Hi John, Which protocol are you using for the autofs? I was initially using the AFP protocol, which worked fine, but recently when I tried Plex, it kept failing saying media not available. I checked all settings and ran the automount cmd several times, which never generated an error, but still couldn’t navigate to the location – similar to yourself I guess. I ended up removing the AFP protocol and used SMB instead, exactly the same steps, process, and names, but different protocol. When I then ran automount everything seemed to work fine, and has been running since (+4 weeks).

Hi John, either the Synology IP address or its Server name. For example my Server name is called NAS and its IP address is 192.168.0.50. If using the Server name just make sure that your Mac can resolve to the Synology, if not you’ll need to add an entry to your hosts file. Easiest way to do this is open up Terminal and enter Ping <server-name>, if you get a response all good, else add it to the hosts file.

Great article and exactly my issue with my Plex server running on a 95% stationary MacBook, but my NAS (afp) is actually an external USB Lacie drive connected to an Apple Time Capsule but the sleep behaviour is the same and there are not even any settings on the TC to change this. I can’t get this to work though and I feel it may be related to my share using the Time Capsule device password to authenticate, I can’t seem to pass this on as just :@ without doesn’t work. I can’t change it back to user level authentication as I can’t see any files wont he drive after I do.

Small update, I managed to get around the password issue, I use user accounts on the Time Capsule now and log in with the Admin account and device password so I can see all files. I also managed to mount the drive correctly via Terminal:

Try removing the entry from auto_masters, then run the automount cmd, reboot the system and try and add the entry back in. If that doesn’t work, try creating a new file, e.g. auto_lacie and create the necessary entry points to see if that works, finally if all fails, and if viable, try using the smbfs protocol.

I can’t past the first step, I tried to call the Auto_master file but keep getting error that command is not found… I’m a newbie to this mac stuff.
So you meant to tell me.. that ancient, old fashioned Windoze mount drive or share is waaayy superior to anything in Mac OS X? That’s utterly amazing, to have to jump through these hoops to mount a share? It’s the only thing stopping me from using a mac mini as Plex server (what I bought it for – but what a waste).
Windows sucks in many departments, but its a whole lot better with managing networking it seems.
I’m a little stuck now, my Win 7 VM is too slow for Plex, and NAS’s are just too expensive and even slower for Plex… so I guess a dedicated Windows box for Plex server is the next best thing…grudgingly but hey Cupertino can’t seem to get networking basics right.

Hey all. This all sounds great. However, I am new to all this and I do not write code. I just bought a QNAP TS-251 and successfully transferred all my media but I am having the issue you reference above. I am fearful that if I attempt this recommended step I may screw something up. So, can you provide exact, step by step directions? In step 4 or 5 you reference not naming it “volume/” so I am not clear on what that means exactly and if there is a step there that is inferred that I do not know. Thanks for any help!!

I don’t know whether this is a 10.11 thing but I’m finding that after following these instructions for my Sinology NAS it doesn’t initially mount until I try and access the folder, is that correct? In Finder it appears as a regular folder until I click on it, at which point it “wakes up” and turns into a network share.

So this made it useless, for example, for Acronis True Image backup because when this software loaded it just said “Destination not available”.

I thought from the description above that it is meant to setup the mount fully right away?

The server mounts beautifully, but clicking on the server icon in /Users/me gives the error “The folder “Test” can’t be opened because you don’t have permission to see its contents.”

After working the problem through, I see that this is a permissions problem. The mount point has the owner and group and permissions:
drwx——@ 1 root wheel 364 31 Dec 10:01 Test
(Actually, it’s interesting — if I empty out my auto_afp file and reboot, this returns to a regular directory with owner/group/permissions drwx——+ 18 me staff 612 31 Dec 10:01 Test)

So the problem here is that autofs is mounting the share with root privilege, and my user cannot actually use the share. From my reading on the web, this appears to be a relatively new problem — perhaps El Capitan related?

For comparison, when I do the following (as a user, not as a system user or using ‘sudo’):
Create a new folder from the finder at /Users/me called ‘Test3′
then from the terminal enter:
mount -o nosuid -t afp afp://serveuser:servepass@server.local/Music /Users/me/Test3

then the server mounts beautifully (albeit with the finder name “Music”, which I would prefer to change), and has the following user/group/permissions:
drwx——@ 1 me staff 364 31 Dec 10:21 Test3
and I am able to see and manipulate the contents.

So in summary, the problem is: How can I get autofs to mount a network AFP share and map it into a user directory so that the user can access it and manipulate its contents? Historically, I think this is exactly how autofs is supposed to work, but it seems that the ownership of the mapped folder by ‘root wheel’ prevents it now from being of any real use.

One more matter, while I’m at it: I have fallen back to the simple goal outlined above, but my longer-term goal is to map an external music folder to EACH user. auto_afp should look like this:

I know that I can make this mount in each users’ login items, but that’s not actually good enough to achieve my long-term needs. Long term, I need this folder mounted at boot by autofs to make it available for cloud backup.

I would really appreciate any help or insight you can offer! Thanks for reading.
Mark

Using an indirect map doesn’t help either. This functionality seems to be fundamentally broken in OSX going back to at least Yosemite… I’ve dug through 3 years worth of posts of people having this same issue and as far as I can tell, there’s simply no way to share a mount point across users… what an huge, infuriating, unbelievable fail.

One option is to enable root, and logon as root. Once you do that it all starts working. Crap option, I know, but my use case is a media server on an isolated network. Only option until I can see until apple gets it head out of its arse.

after a long time I found your solution for my NAS … everything works like a charm … But now I realised that something is not working as it could … Do you have any solution for the following:

autofs only works like a charm on machines that are always connected to a network because of the delay issue if for example you leave your home network and restart your MacBook …

Let’s say you take your MacBook to a friend and a new network (not known) and try to open/save files from the Finder … you will get stuck into a big delay and not responding applications and the MacBook keeps trying to mount the NAS drives continuously …

Nice!
Is there ANY way to have this feature check if i am in my local network or on a remote network (like “if ‘localNAS-IP’ not reachable THEN auto-mnt ‘nasname.dyndns.com’) so I can have the same mount, no matter where i am (as long as i am online, of course)

That feature i miss in every mount tool (like expandrive, autoMount, Mountdrive, etc)…

I have a Plex server on a Mac mini and the drive disconnecting drives me nuts. I have it set to connect when the Mac mini boots up but if the NAS reboots or gets unplugged from the network if never remounts unless I remote desktop to it and do it manually. Pain! So I really want to get this working.

I am struggling to understand the first part where I edit the auto master. With the file path. I literally just want it to mount one particular share on the NAS and I want it to appear as mounted in the Finder sidebar as well as appear on the Desktop like it usually would. Can you tell me what path I would use for that.

Hi Rajesh,
Will this work for an external hard drive attached to an Airport Extreme Base Station? I am looking for a solution which will allow the network drive to automatically mount and eject from my Macbook Pro as I come and go from my home. Thanks, great info!

Hello…
I have tried, it work! GREAT JOB!
i tried with local network (afp/smb) it working fine. But when i try to connect with far connection (mount SMB via VPN in other side latency 300-400ms) it can’t get any information.

Awesome article, thanks for posting. I pretty much have the same setup, just moved my Plex from my MBP to a dedicated Mac Mini on 10.11 and switch my growing media library from a USB drive to a Synology 1515+ .. accessing the media away from home was an issue with the files not being available.

Awesome article and it works perfectly.
I have two questions.
1. I have Plex on a Mac Mini and my movies on a Synology NAS. What is the best protocol for connecting those, AFP of SMB and why?
2. Can you setup automount to only work on a specified network? I would like my MacBook to connect to the drives when I am at home but don’t want any issues on other networks.

Thank you for the post, it looks like this will be the solution for me !
BUT I can’t get it to work ?
I’ve tried AFP and SMB variants, it creates the mnt/NAS/PersonalMedia dir but no drives are visible ?

I’m having a big time issue. I am using an Airport Time Capsule and it simply won’t mount. In Finder Connect to Server, I can go to cifs://username:password@cbtc/Data/iTunes (iTunes Library in that folder) and it will pull it up right away. So I do all that in the auto_nas file, and I run automount -vc but it simply shows it as blank. What am I doing wrong?

Thank you this was massively helpful. I was having a very similar problem with a Drobo 5n. It kept in mounting and having to manually reconnect to watch stuff on Plex was driving me insane. This solved the problem. Thank again.

Can you explain how we create this directory?
“If the directory you specified in Step 3. (auto_master) above doesn’t exist for your mount to appear, you’ll need to create one, either using Finder or Terminal. In my case I needed to create the directory structure /mnt/NAS.”

Hi Rajesh! That article has a great value! Thank a lot for that and the perfect documentation. I had similar troubles to mount my NAS connected with at my fritz.box. Especially when running backup scripts by jenkins to save documents. My script for mounting the network drive does not work properly at all. Additionally I do not find out how to define another mount point instead of the given by device:

on mountFB()
set the_server to “smb://user:pwd@fritz.box/FB7490/Intenso-USB3-0device-01″
try
tell application “Finder”
mount volume the_server
end tell
end try
end mountFB
mountFB()

I haven’t tried it yet, but perhaps sshfs might do the trick for you if it’s an option to open ssh on your firewall. In this setup you don’t necessarily need VPN. I might try it myself when I find the time to do it.

Thanks Rajesh. Nice write up!
I’m in same situation, except I’m using an unRAID server for my media NAS.
Just thought I’d point out you still have one small typo: In step 6 you’ve missed the ‘smb’ before the ‘:’
ie. It should read: smb://username:password@nas/PersonalMedia
Although obvious to IT people, this may trip-up some of your visitors setting up smb automount’s.

Hi Rajesh,
You can ignore my comment above regarding the ‘smb:’ protocol prefix, as it indeed isn’t required. Mind you, I’ve still included the full smb:// protocol prefixed link, just to be explicit.
I am however having the same issue which others have also noted elsewhere. This is with automount’d shares occasionally becoming inaccessible, due to their ownership being taken over by root with exclusive access. This appears to require a ‘sudo umount’ of the share, to allow the current user to regain ownership, and access to the mounted share. Unfortunately, I haven’t found a “nice” solution to this as yet.

This has worked perfectly for me for sometime now. But I recently upgraded to Sierra and now my mounts randomly unmount and will not remount until I reboot. Is anyone else experiencing this problem? Does anyone have a solution?

Hi Derek. What you describe sounds like the same problem I described above (my Nov 14 reply). In my case the mounted shares (which you cannot see / access) had been taken over exclusively by root. eg. if you open terminal and do a ‘sudo ls -all’ over your mount folder (eg. /mnt/NAS in Rajesh’s example), you’d see the missing shares still mounted, but now owned by root with exclusive rights (ie. 700). In my case you could avoid a full reboot by sudo unmount of the affected share (ie. ‘sudo umount /mnt/NAS/SharedFolderName’). In my case this then allowed the current user to immediately re-take ownership (until it randomly happened again to this, or another share mount). But I haven’t found a ‘nice’ solution to this yet. Let me know if you are seeing identical issue & if you find the solution. Cheers.

Great article and it’s working well for me. I’m also having some shares drop randomly from finder. They appear to still be there when listing folder contents in terminal and the sudo umount solution works to bring them back.
It would be great if we could devise an automatic way to bring these shares back, or at least a 1 click solution. An executable script for example.
thanks again.

I think SIP may break /mnt/NAS permissions. I’m going to try this and see if it works:
mkdir -p /Users/Shared/mnt/NAS
chmod 777 /Users/Shared/mnt/NAS
Or if above does not work, try again but put “sudo” in front of above 2 commands.

I also experienced the problem of the share disappearing. Inspection with the terminal reveals the share is still there and accessible, but the ownership changes to root/wheel making it inaccessible to the user via Finder or other applications.

Thanks for the post, this has helped me automount my Synology NAS.
However now I have the problem that if the NAS is not reachable in the network (like when I’m at the office), my “Save as” dialog in most Mac application starts hanging with the dreaded “beachball” for a very long time.
Is there a way to set a timeout for trying to mount the network share?

Anyone know what’s the quickest way to disable the changes made by the code outlined here? I’m not a programmer, but I followed everything to the tee and got this to work. Only problem now is that every time macOS boots up, I cannot launch finder for the first 2-3 minutes because this script is busy looking for the drives on my network. I don’t always want to access Finder on boot up so this is not a huge deal, but when I do, that wait is super annoying. Would appreciate some help in disabling this please.

Except that this app that costs AUD$14.99 requires an additional $10.99 in app purchase to allow it to mount shares outside of the sandbox folder: /Users/xxxxx/Library/Containers/nz.co.pixeleyes.AutoMounter/Data/Mounts/xxxx

It is ridiculous that Automounter by Pixeleyes does not mount in /Volumes

Thanks for this entry! I just got to the point, where I wanted to offload some data from my Macbook onto a USB drive, shared by the WiFi router, and you made it a painless process. You certainly made my day!

Problem in enhance system security, macOS Sierra 10.12. You can disable the enhance system security.
1) Update to macOS Sierra 10.12.2.
2) Open the Terminal app, which is in the Utilities folder of your Applications folder.
3) Enter the following command, then enter your administrator name and password when prompted:
sudo defaults write /Library/Preferences/com.apple.NetworkAuthorization AllowUnknownServers -bool YES