Start/Stop/Status of Filesharing (2025)

Is this the best way to start/stop/status filesharing for macOS ?

-- AppleScript to enable File Sharing on macOS

do shell script "sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.smbd.plist" with administrator privileges
do shell script "sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist" with administrator privileges

-- AppleScript to disable File Sharing on macOS

do shell script "sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbd.plist" with administrator privileges
do shell script "sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist" with administrator privileges
1 Like

You should not use both ā€˜sudo’ and ā€˜with administrator privileges’ in one command.
Either one will do.

thanks for the response.

So the issue I’m getting is it seems like the toggle turns off then turns back on however, the service doesn’t seem to.

scenario1 - failed
check the file sharing window
file sharing toggle is on
close file sharing window
run unload commands
check the file sharing window
file sharing toggle is on
attempt to connect to the mac specific share
connection failed

scenario 2 - success
check the file sharing window
file sharing toggle is on
click the file sharing toggle off – manually
click the file sharing toggle on – manually
attempt to connect to the mac specific share
connection successful

so it seems the commands above aren’t doing what the manual clicking does. So how do I script it so it does do what the manual clicking does?

I have never used those commands, so can’t tell how they might go wrong. Sorry.

I’m not very confident in English, so I might be misunderstanding something (apologies if that’s the case!).

I’m using macOS 15, so com.apple.AppleFileServer is no longer configurable
this is just about SMB file sharing.
Just wanted to share a quick note about enabling/disabling file sharing (SMB) on macOS.
Behavior can vary a bit depending on the OS version.

In earlier versions of macOS
I used to disable file sharing like this:

/usr/bin/sudo /bin/launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbd.plist
/usr/bin/sudo /bin/launchctl disable system/com.apple.smbd

Starting with macOS 14, I now do it this way:

/usr/bin/sudo /bin/launchctl bootout system/com.apple.smbd
/usr/bin/sudo /bin/launchctl disable system/com.apple.smbd

And when I want to start file sharing again on macOS 14 or later, I use:

/usr/bin/sudo /bin/launchctl enable system/com.apple.smbd
/usr/bin/sudo /bin/launchctl bootstrap system /System/Library/LaunchDaemons/com.apple.smbd.plist

I’m not entirely sure if this method is the ā€œcorrectā€ one, but it works for me on macOS 14 and later. If mentions their macOS version, there might be someone who can offer a more optimized solution for their setup.
Hope this helps!

Interesting… I figured the sudo was for the shell and the administrator privileges was for AppleScript.

Just a heads-up for anyone still running a file server for multiple users. Apple’s current SMB implementation respects no server permissions. It’s the Wild West. Have a robust backup strategy.

Yes, I totally agree.
For a beginner like me, it’s all a bit too complex… sigh

Between
com.apple.smb.server.plist,
nsmb.conf,
launchctl,

and also:

/usr/libexec/smb-migrate-preferences
/usr/libexec/smb-sync-preferences

dscl, user/group configurations,
and directory ACLs…

It’s definitely a challenge to get everything set the way you intend.

ā€œBestā€ is outside my purview and I see that this concerns Monterey and I don’t currently have a system running that OS but I expect this code to function. If GUI scripting is acceptable then you can try the following.

tell application "System Settings"
	activate
	delay 3
	reveal pane "Sharing"
	delay 1
end tell
set FileSharingCheckboxReference to getFileSharingCheckboxReference()
Checkbox_Status(FileSharingCheckboxReference)
-->false
Checkbox_Activate(FileSharingCheckboxReference)
-->true
Checkbox_Status(FileSharingCheckboxReference)
-->true
Checkbox_Deactivate(FileSharingCheckboxReference)
-->true
Checkbox_Status(FileSharingCheckboxReference)
-->false



on getFileSharingCheckboxReference()
	tell application "System Events" to tell application process "System Settings" to return checkbox 1 of group 1 of scroll area 1 of group 1 of group 2 of splitter group 1 of group 1 of window "Sharing"
end getFileSharingCheckboxReference

on Checkbox_Status(CheckboxReference)
	tell application "System Events"
		tell CheckboxReference
			return (value as boolean)
		end tell
	end tell
end Checkbox_Status

on Checkbox_Activate(CheckboxReference)
	tell application "System Events"
		tell CheckboxReference
			if value is 0 then
				perform action "AXPress"
				return true
			end if
		end tell
	end tell
end Checkbox_Activate

on Checkbox_Deactivate(CheckboxReference)
	tell application "System Events"
		tell CheckboxReference
			if value is 1 then
				perform action "AXPress"
				return true
			end if
		end tell
	end tell
end Checkbox_Deactivate

thanks you for this but as I’ve found that when the screen saver locks the screen this doesn’t run. still testing the other posts to see if it works successfully.

do shell script ā€œsudo launchctl bootout system /System/Library/LaunchDaemons/com.apple.smbd.plist; sudo launchctl bootstrap system /System/Library/LaunchDaemons/com.apple.smbd.plistā€ with administrator privileges

this seems to be working… but still testing.