MacOS – Ensuring file sharing stays enabled MacOS

command linemacossmb

I have a large set of mac mini's in the field with smb/afp shares enabled. For reasons completely unknown to me the file sharing sometimes gets disabled for some nodes. I could not find a single thing in the logs, but for now I'm trying to write a quick workaround until we can find the issue.

Currently I'm writing a script that simply periodically checks if file sharing is enabled, and otherwise forcing it on again.
A lot can be done with the systemsetup binary, but it can't do anything for filesharing.

Can anyone tell me either what commandline binary is used to enable file sharing, or which services specifically I need to launch with launchctl?

Best Answer

Proper tools in macOS client are sharing and launchctl which have to be run as root:

Check man sharing how to use it.

To start/stop or load AFP or SMB use:

sudo launchctl [subcommand] -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist

or

sudo launchctl [subcommand] -w /System/Library/LaunchDaemons/com.apple.smbd.plist

The proper tool to deal with macOS server related tasks is serveradmin which has to be run as root:

sudo serveradmin -h
Usage: serveradmin [-dhvx] [list | start | stop | status | fullstatus | settings | command] [<service_key> [ = <value> ]]

  -h, --help     display this message
  -v, --version  display version info
  -d, --debug    print command
  -x, --xml      print output as XML plist
Examples:
serveradmin list
    --Lists all services
serveradmin start afp
    --Starts afp server
serveradmin stop ftp
    --Stops ftp server
serveradmin status web
    --Returns current status of the web server
serveradmin fullstatus web
    --Returns more complete status of the web server
serveradmin settings afp
    --Returns all afp configuration parameters
serveradmin settings afp:guestAccess
    --Returns afp guestAccess attribute
serveradmin settings afp:guestAccess = yes
    --Sets afp guestAccess to true
serveradmin settings
    --Takes settings commands like above from stdin
serveradmin command afp:command = getConnectedUsers
    --Used to perform service specific commands
serveradmin command
    --Takes stdin to define generic command that requires other parameters