Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

HOW TO Mount a Network Share using the Automount as non-root

So, this bothered me for some time, and I finally figured it out, so I decided to share it with everyone, in case other people have the same problem.


The Problem: Mount a Samba or Windows share on a mac to a specific mount point, have it autoremount when ever it is reachable, and have it automatically reconnect after a reboot, and have the share be accessible by non-root users.


Solution (almost): This should be simple, but there is a bug in Apples autofs which, after a reboot, automounts shares with root privileges only. Here is the solution, almost. This solution still requires you to run a script after login to remount the drives with normal user privileges, rather then root privileges.


So, here we go. First, let's create the automount shares. You can use your favorite method, but here is mine:


1. Open Terminal and run:


sudo nano /etc/auto_master


2. Add the following line to the bottom:


/-     auto_smb


3. Save, exit nano, and create a new file for your samba connections like so:


sudo nano /etc/auto_smb


4. Add the shares you would like to mount, one per line, in this format to the newlly created file:


/Users/username/mount     -fstype=smbfs,soft     ://user:password@server/share


In the above, the username is your mac username, basically path to your home directory. You can mount it any were inside your home directory, It's not necessary to create the actual mount point directory, automount should make it for you. Though, to be safe, you can do so. Just run 'mkdir ~/path/to/mount'. User and password after // is your credentials for the remote samba or windows share.


5. Finally, run automount to mount the new shares:


automount -vc


Once it's all done, you can switch to the directory were the mount points you have chosen are located. Run 'ls -la' to see the list of the mount points. You will likely see something like "Permission denied for 'mount point'" or something like this. You can run 'sudo ls -la' and you will likelly see that the shares are mounted with root privileges. This is a bug. (Thank you Apple, Now FIX THIS!) For now, however, here is a quick fix:


6. Unmount the shares and then remount it like so:


sudo umount /Users/username/mount
cd /Users/username/mount


Make sure you run the 'cd' command as a normal user. What happens is the share is unmounted, and then automounted once you try to change into it. It will remount with proper permissions.


Finally, so that you do not have to do this every time you reboot, create a bash script with commands in step 6, and run it at login. The annoying thing, you'll have to type your password in every time you run that script, to authorize sudo. I tried many other options ways to atomate it, but the trick is, you must run umnount as root, and you have to run the cd command as normal user.


If any one has a better method, please share, I searched and read every article on the subject, and this is the best method I have found so far, but please let me know if you have a better method.


- Bogdan

OS X Mountain Lion (10.8.3)

Posted on Mar 28, 2013 9:40 PM

Reply
Question marked as Best reply

Posted on Apr 14, 2013 11:24 AM

I had the same issue as you, and solved it slightly neater using AppleScript. The script looks like this (from my memory):


tell application "Finder"

mount volume "cifs://macmini@192.168.1.1/NAS"

end tell


I saved it as an application and set it to run as a login item for the users that needed it. Once you save the password to your keychain it works automatically on login/reboot without the issues of automount.

21 replies
Question marked as Best reply

Apr 14, 2013 11:24 AM in response to x0054

I had the same issue as you, and solved it slightly neater using AppleScript. The script looks like this (from my memory):


tell application "Finder"

mount volume "cifs://macmini@192.168.1.1/NAS"

end tell


I saved it as an application and set it to run as a login item for the users that needed it. Once you save the password to your keychain it works automatically on login/reboot without the issues of automount.

Mar 29, 2013 6:16 AM in response to x0054

Unfortunately, I don't have time to dig into this right now. I can tell you that OS X is designed to work with Active Directory. If you have a managed user, everything works the way it should. If you are trying to cobble together something on your own, then you are on your own. However, you can't label the difficulties you run into as "bugs" without extensive research.


If you can figure out all the parameters you need, you can use Directory Utility to do all the hard work for you. Lacking that, start with Apple's AutoFS documentation and employ lots of trial and error.


I would like to figure out better for myself how all of this works. I have a professional interest in it. I am a bit busy until April 15th. If you bump this thread on or after that time, I'll help you track it down.

Mar 22, 2017 8:03 PM in response to x0054

I have struggled with the same problem for quite some time, and seem to have solved it on a mac where only one user is ever logged-in at any given time.

I use the same method, but add the nosuid option and also only add soft after it mounts properly twice.

my mounts are not under /Volumes but under /Users/me/anotherdir.

Finally, I did not store the password in plain text, but in keychain.

This has been working flawlessly for months under both sierra and El Capitan.

The only drawback is that there is no Spotlight indexing.


On my mac mini server, where I have multiple users logged-in at any given time, it fails on a very regular basis, but I have not yet tried any kind of scripts to fix it.

Mar 29, 2013 11:49 PM in response to etresoft

Actually, I did about 2 days worth of research on this issue, and read a bunch of posts on the subject, and the AutoFS docs. Take a look at https://discussions.apple.com/thread/3221944?start=0&tstart=0, a lot of people have a problem with this.


Active Directory is great, but in my case I am trying to connect to a simple NAS drive. I think a bunch of people are in the same situation. In any case, my solution is hacky, but it works for now. But of course, I like elegant solutions, and mine is anything but elegant. If you know a better one, I would love to here about it, and I am sure a bunch of other people would too.


Basically the goal would be to connect a Mac to an NAS via Samba, have it mounted in the same location, anytime the NAS is within reach, and have it mounted with user level, rather then root level privileges. My method archives that, but I have to run a script after a reboot, which is not that big of a deal. However, the bigger problem is that the passwords for the shares are stored in cleartext, which I do not like.


I'll reread the AutoFS docs, but if any one has any sugestions, I am all ears.


Thank you guys.

Aug 15, 2013 7:37 AM in response to Revion

Does this AppleScript method ensure the mount is available after the computer wakes from sleep too?


That was the main reason I was looking into using automount as it guarantees the mount is always available when I want it, but hit the same problem as stated above in that the permissions aren't guaranteed to always be set correctly.

Mar 9, 2014 6:33 AM in response to x0054

I added two flags to your process, and since then my setup has been working even after reboot.


In step 2 I added the nosuid flag, which probably isn't necessary since it seems to be a default flag for the maps anyway


/-                      auto_smb      -nosuid


In step 4 I added the noowners flag. I think this is the one who did the trick; not really sure why, though...


/Users/username/mount    -fstype=smbfs,noowners,soft     ://user:password@server/share

Mar 20, 2014 9:45 AM in response to x0054

I've adapted these instructions to try and automount an afp share (changing -fstype to afp) and editing /etc/auto_afp instead of /etc/auto_smb


When I run automount -vc as standard user, I get the error

automount: Couldn't open /dev/autofs_control: Permission denied


If I run it under sudo, it claims to have updated my afp mount, but still it doesnt appear as mounted in Finder.


Has anyone got this working for afp?

I'm frustrated this whole process isnt just trivial, we've been using shares for 10 years now

OSX really annoys me lately :/

Mar 29, 2014 8:06 AM in response to callehedberg

After a little more extensive testing it turns out that my solution was very intermittent. Thus, I had to resort to the bash script solution proposed by x0054. I have however implemented the script as a LaunchDaemon, which enables the script to be run as root without having to authorize, and then cd into each user's mount point through sudo -u so that the mapping gets mounted by the correct user.


The problem with implementing it as a LaunchDaemon, is that the script is called before the automounting is completed, and thus no map exists to get remounted. I solved that problem by modifying the script containing a while-statement in the beginning:


while ! mount | grep "map -fstab on /Network/Servers"
do
        sleep 2
done

if ! mount | grep "mount .* mounted by user"
then
        sudo umount /Users/user/mount
        sudo -u user cd /Users/user/mount
fi


The if-statement is to ensure that the map is not already correctly mounted by the appropriate user, so that we don't do any unnecessary unmounting. This is useful if you need to run the script at a later time to remount maps that have become corrupt.


I have to wait for the "/Network/Servers/" mapping to be completed, since I am on a machine without any local accounts, only network accounts. This means that the script is not able to run the cd command as my network user until the machine gets tied to the Open Directory Server.


If you are not on an Open Directory Client, I suppose you should exchange the first line in the script with this one:


while ! mount | grep "map auto_smb on /Users/user/mount"


In which the script only waits for your specific mount to be mapped.


To call the script as a LaunchDaemon, you have to create a plist in /Library/LaunchDaemons. Mine looks like this:


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
          <key>Label</key>
          <string>com.organization.remount</string>
          <key>ProgramArguments</key>
          <array>
                    <string>/Users/user/bin/remount.sh</string>
          </array>
          <key>RunAtLoad</key>
          <true/>
</dict>
</plist>


The string com.organization.remount is also the name you will have to give the file, with the extension plist: "com.organization.remount.plist".


The string /Users/user/bin/remount.sh is the path to where you saved your script.


Finally I should add that the modifications proposed in my previous post seems superfluous. I have removed them, and everything still works.

Apr 18, 2014 3:44 AM in response to x0054

So after a couple of hours trying to get this working, the one thing that kept messing things up was having the "soft" flag on whilst making changes. No matter if I umounted, autoupdated, restarted, or deleted directories, it seemed to cause the connection to stay alive with the bad settings.


Basically, whilst you're making changes / getting this working, remove the "soft" flag. Once your mounts work, add it back in!

Jun 29, 2014 4:03 PM in response to callehedberg

Thank you very much callehedberg, your concept works well for me. But I'm not very familiar with scripting and having a few issues automating this...


1. my mount looks like this "Server.local:/music /Users/daniel/Shares/Music url automounted,url==afp://xxx:yyyy@Server.local/music 0 0

What should I put in line 1

while ! mount | grep "map -fstab on /Network/Servers"

I have tried while ! mount | grep "map auto_afp on /Users/daniel/Shares/Video"

2. when I try to run this as "bash remount.sh" I get the following errors

bash remount.sh

remount.sh: line 1: while ! mount : command not found

remount.sh: line 1: grep map auto_afp on /Users/daniel/Shares/Video : No such file or directory

remount.sh: line 2: do : command not found

remount.sh: line 3: : command not found

remount.sh: line 4: done : command not found

remount.sh: line 5: : command not found

remount.sh: line 6: unexpected EOF while looking for matching `"'

remount.sh: line 27: syntax error: unexpected end of file


Is this not a bash script?


3. Can this be implemented using a login hook?

Mac OS X: Creating a login hook


sudo defaults write com.apple.loginwindow LoginHook /path/to/script

Thanks for your help, I've burned many hours on this automount task today.

HOW TO Mount a Network Share using the Automount as non-root

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.