Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aplnub

macrumors regular
Original poster
Nov 16, 2008
184
265
I need to mount a NAS server on 20+ MacMinis. I would like to do this upon bootup via a Bash Script. No matter what I try I can't get it to work. In the end, I have to share the screen and click on a Connect button or click on the network share in teh finder.

My son is using the processing power of all those Minis to explore a math problem and automating an NAS mounting is a must have and would speed up his progress.

Any help would be appreciated.

TIA
 
If you want the NAS to pop up every time you start your Mac, just mount it once by hand, then drag that folder over to Login Items inside System Settings > General.
 
  • Like
Reactions: aplnub
We have done this on each of the machines, however, when ssh Ing into the machine and trying to change directory to that folder it is not working.

We have both the share and the folder in logon items on start up.
 
We have done this on each of the machines, however, when ssh Ing into the machine and trying to change directory to that folder it is not working.

We have both the share and the folder in logon items on start up.
I think the method suggested by @VitoBotta will only mount the share when the user logs into the graphical user interface. It won't work if the user account is logged out, and then you log in with ssh.

You should probably try a bash script solution.

I haven't tried this in a long time, but here are some commands that may point you in the right direction. I don't have time right now to test thoroughly, so my apologies if it doesn't work right off...


smbutil view //username@machinename

mkdir ./local-mount-point
mount -t smbfs "//username@machinename/the/share/path" ./local-mount-point

If the pathname has a space, use something like "//user@mymac/Home%20Videos"
(Keep the quotes!)

I have notes that say:
"Always use 'mount' and not 'mount_smbfs', according to the man page."
and
"the user (on the remote file-serving Mac), must have a special setting, "Windows File Sharing", or else the password will always be rejected."

Go to System settings-->Sharing-->File Sharing (i button)-->select the desired folder-->hit the Options button and in the Windows File Sharing section, check the user account to allow the user's password to be "stored in a less secure manner." !

Perhaps that's not needed anymore, but in the past it was required for the Terminal commands to work, and I think it still is.

Resources:
man smbutil
man mount
man mount_smbfs
 

Attachments

  • Screenshot 2025-07-18 at 5.08.12 PM.png
    Screenshot 2025-07-18 at 5.08.12 PM.png
    159.7 KB · Views: 9
I have an Applescript that automated the mounting of server shares for me in the distant past. I have no idea if it works not or not in current versions of MacOS, but if you want I can post it. All you'd need to do is enter the credentials and NAS folder locations, compile in Script Editor and then save an app from there. The app could then be placed in the login items.

Again, no idea if it would work now.
 
Here is a bash script that might work for you. The script checks for network availability, creates the mount point if it doesn't exist, and mounts the NAS share. It logs actions to a file for troubleshooting. To run this script automatically on boot, you can integrate it with the provided launchd plist file.

Steps to use:
  1. Customize the script: Replace placeholders (NAS_SERVER, SHARE_NAME, USERNAME, PASSWORD, MOUNT_POINT) with your actual NAS details.
  2. Deploy the script: Save it to a location like /usr/local/bin/mount_nas.sh on each MacMini.
  3. Set permissions: Make the script executable (chmod +x /usr/local/bin/mount_nas.sh).
  4. Configure launchd: Use the provided plist file to run the script on boot.
  5. Test the script: Run it manually first to ensure it works (sudo /usr/local/bin/mount_nas.sh).
  6. Deploy to all MacMinis: Use a management tool (e.g., Jamf, Munki, or SSH) to distribute the script and plist file to all 20+ MacMinis.
Detailed deployment instructions:
  • Save the Script:
    • Save the Bash script as /usr/local/bin/mount_nas.sh.
    • Set permissions: sudo chmod +x /usr/local/bin/mount_nas.sh.
    • Set ownership: sudo chown root:wheel /usr/local/bin/mount_nas.sh.
  • Save the Plist File:
    • Save the plist file as /Library/LaunchDaemons/com.example.mountnas.plist.
    • Set permissions: sudo chmod 644 /Library/LaunchDaemons/com.example.mountnas.plist.
    • Set ownership: sudo chown root:wheel /Library/LaunchDaemons/com(continues).example.mountnas.plist.
  • Load the Plist:
    • Load the launchd job: sudo launchctl load /Library/LaunchDaemons/com.example.mountnas.plist.
    • Verify it’s loaded: sudo launchctl list | grep com.example.mountnas.
  • Test the Script:
    • Run the script manually: sudo /usr/local/bin/mount_nas.sh.
    • Check the log file: cat /var/log/mo
Bash:
#!/bin/bash

# Script to mount NAS on macOS at boot
# Save as /usr/local/bin/mount_nas.sh and make executable (chmod +x)

# Configuration
NAS_SERVER="nas.example.com"  # Replace with your NAS server IP or hostname
SHARE_NAME="shared_folder"    # Replace with your NAS share name
MOUNT_POINT="/Volumes/NAS"    # Local mount point
USERNAME="your_username"      # NAS username
PASSWORD="your_password"      # NAS password (consider using Keychain for security)
LOG_FILE="/var/log/mount_nas.log"
MAX_RETRIES=3
RETRY_DELAY=10

# Ensure log file exists
touch "$LOG_FILE"
echo "[$(date)] Starting NAS mount script" >> "$LOG_FILE"

# Function to check network connectivity
check_network() {
    ping -c 1 "$NAS_SERVER" > /dev/null 2>&1
    return $?
}

# Wait for network to be available
attempt=1
while [ $attempt -le $MAX_RETRIES ]; do
    if check_network; then
        echo "[$(date)] Network is available" >> "$LOG_FILE"
        break
    else
        echo "[$(date)] Network not available, attempt $attempt/$MAX_RETRIES" >> "$LOG_FILE"
        if [ $attempt -eq $MAX_RETRIES ]; then
            echo "[$(date)] Failed to connect to network after $MAX_RETRIES attempts" >> "$LOG_FILE"
            exit 1
        fi
        sleep $RETRY_DELAY
        ((attempt++))
    fi
done

# Create mount point if it doesn't exist
if [ ! -d "$MOUNT_POINT" ]; then
    mkdir -p "$MOUNT_POINT"
    if [ $? -ne 0 ]; then
        echo "[$(date)] Failed to create mount point $MOUNT_POINT" >> "$LOG_FILE"
        exit 1
    fi
    echo "[$(date)] Created mount point $MOUNT_POINT" >> "$LOG_FILE"
fi

# Check if already mounted
if mount | grep -q "$MOUNT_POINT"; then
    echo "[$(date)] NAS already mounted at $MOUNT_POINT" >> "$LOG_FILE"
    exit 0
fi

# Mount the NAS
mount -t smbfs "//${USERNAME}:${PASSWORD}@${NAS_SERVER}/${SHARE_NAME}" "$MOUNT_POINT"
if [ $? -eq 0 ]; then
    echo "[$(date)] Successfully mounted NAS at $MOUNT_POINT" >> "$LOG_FILE"
else
    echo "[$(date)] Failed to mount NAS at $MOUNT_POINT" >> "$LOG_FILE"
    exit 1
fi

exit 0

Here is the launchd plist you can use:

Code:
<?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.mountnas</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/mount_nas.sh</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <false/>
    <key>StandardOutPath</key>
    <string>/var/log/mount_nas.log</string>
    <key>StandardErrorPath</key>
    <string>/var/log/mount_nas.log</string>
</dict>
</plist>
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.