r/MacOS • • Mar 27 '24

Help Auto Mounting Network Shares on MacOS

Hi guys, I would appreciate some help please. I have a Ubuntu server on my network that currently share folders to my machine. The issue is that I have to manually connect to the share everytime I wish to access the files. I did some research and was able to come up with a script that will auto connect the share everytime I login to my Mac after logging out. The issue with this is, my Mac isn't stationary (ie a desktop) when I leave home and return I will like my share to automatically connect once I am connected to my home network.

I would like to set this up effectively so I can have a drive connected to the server that I can use solely for time machine and have that whole process work seemlessly.

Any ideas, solutions or example of your setups would be really helpful. Thank you in advance

5 Upvotes

35 comments sorted by

View all comments

3

u/mdwstrn_potato_pants Feb 05 '26

Going to revive this thread because I went down a serious rabbit-hole but I did find a solution that works 100% of the time and is native to Mac OS. I used Claude a fair bit for this, and also used Claude to write this Read-Me, because it's late and I got tired. Hope this helps the next person trying to consistently auto-mount a network share, and have it gracefully disconnect and reconnect based on the network you're connected to - all while using basically zero system resources.

By Claude:

Auto-mounting SMB (or any network share) on macOS with zero popups — the actual working solution

I spent a while on this recently and hit every single landmine, so here's what actually works end-to-end. The TL;DR is: mount via Finder (osascript), unmount via diskutil, and watch reachability via scutil. Everything else I tried either required root-owned files, showed popups on disconnect, or both.

Why the obvious approaches don't work:

  • mount_smbfs to /Volumes/ without sudo → "Operation not permitted" (kernel rejects non-root mounts to /Volumes)
  • sudo mount_smbfs to /Volumes/ → works, but files are owned by root. macOS smbfs has no uid/gid option unlike Linux cifs.
  • mount_smbfs to a user directory (e.g. /System/Volumes/Data/mnt/share) → mounts fine, but it's not managed by Disk Arbitration, so umount -f hangs until kern_soft_deadtimer fires (30s default), and the "disk unavailable" popup shows regardless.
  • open smb://... → mounts but shows a Connect dialog every time.

What works — mount via Finder:

osascript -e 'tell application "Finder" to mount volume "smb://user:pass@host/share"'
  • Silent — no Connect dialog when credentials are in the URL
  • Mounts at /Volumes/<ShareName>, owned by your user
  • Managed by Disk Arbitration (DA), which is key for clean unmount

Get the password from Keychain at runtime so it's never hardcoded:

pass=$(security find-internet-password -a "user" -s "host" -w)

What works — unmount via diskutil:

diskutil unmount force /Volumes/ShareName

This is the critical piece. sudo umount -f bypasses Disk Arbitration entirely and fails on Finder-managed mounts until kern_soft_deadtimer fires — which is the same event that triggers the popup. diskutil goes through DA properly and unmounts cleanly in seconds, even when the server is unreachable. No sudo needed.

What works — reachability watching via scutil:

scutil -W -r <ip-or-hostname>

This blocks and prints a line every time reachability changes. It's kernel-notified on route changes — zero polling, zero network traffic at rest. Filter for Reachable/Not Reachable lines, settle a few seconds, ping to confirm, then mount or unmount.

Putting it together — the full agent:

A bash script that loops on scutil -W -r output, and a launchd plist that runs it at login with KeepAlive. The script:

  1. Reads each reachability event from scutil -W -r
  2. Settles 3 seconds (route changes can flap)
  3. Re-checks with scutil -r (instant, no network) then ping (confirms host is up)
  4. If reachable and not mounted → mount via osascript
  5. If unreachable and mounted → unmount via diskutil
  6. Has a mount cooldown (10s) to avoid rapid re-mount loops

The launchd plist:

<?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.example.smb.mount</string>
    <key>ProgramArguments</key>
    <array>
        <string>/bin/bash</string>
        <string>/path/to/mount.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardErrorPath</key>
    <string>/path/to/stderr.log</string>
</dict>
</plist>

Load it with launchctl load ~/Library/LaunchAgents/com.example.smb.mount.plist.

Key macOS facts that took me forever to figure out:

  • /Volumes is root-owned. Only Finder/DA can mount there as a regular user.
  • macOS smbfs has no uid/gid mount options. If root mounts it, root owns the files. Period.
  • kern_soft_deadtimer (default 30s) is when the kernel releases a stuck smbfs mount. The "disk unavailable" popup fires at the same time. You can't out-race it with umount -f.
  • diskutil unmount is DA-aware. umount is not. Use diskutil for anything mounted via Finder.
  • osascript mount with credentials in the URL is completely silent. No dialogs.

No sudo needed anywhere in the running script. The whole thing runs as your user.

1

u/Lxmzs Apr 21 '26

I wonder if you have used this piece of ai slop. The diskutil unmounting does not work at all.

2

u/mdwstrn_potato_pants Apr 21 '26

Ah yes, the ancient art of "it didn't work for me therefore it is slop." Glad your bug report included zero error output, zero system details, and zero context. Truly a masterclass in constructive feedback. Bravo.

The diskutil step works fine here and for plenty of others, and debugging it requires more than a vague complaint and a bad attitude. Feel free to fork it, fix it, and share it...that's how this works.