How to setup file share using terminal

Here’s a detailed article you can use — written in a clear, professional style — about File Sharing for Macs Using Terminal Commands:

Enable SMB File Sharingsudo launchctl load -w /System/Library/LaunchDaemons/com.apple.smbd.plist

Disable SMB File Sharingsudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbd.plist

🖥️ File Sharing on macOS Using Terminal Commands

File sharing on macOS doesn’t always require navigating through the System Settings or Finder. You can manage, enable, and control file sharing directly from the Terminal using built-in command-line tools. This approach is especially useful for system administrators, power users, or those managing headless Mac servers.

1. Understanding macOS File Sharing

macOS supports several types of file sharing, including:

  • AFP (Apple Filing Protocol) — legacy protocol for older macOS systems.
  • SMB (Server Message Block) — the modern default for sharing files with both macOS and Windows systems.
  • NFS (Network File System) — typically used in Unix and Linux environments.

Apple’s graphical “File Sharing” toggle in System Settings > General > Sharing controls these services under the hood. You can do the same via Terminal.

2. Enabling SMB File Sharing via Terminal

SMB is the recommended protocol for modern macOS systems.

Step 1: Enable File Sharing

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.smbd.plist

This command starts the SMB daemon (smbd), which enables SMB-based file sharing.

To disable it:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbd.plist

Step 2: Add a Shared Folder

You can manually add shared folders to the SMB configuration using:

sudo sharing -a /path/to/folder -S "ShareName" -s 001 -g 001

Or more simply:

sudo sharing -a /path/to/folder -S "ShareName"

Example:

sudo sharing -a /Users/alex/Documents/Projects -S Projects

This shares the folder Projects under the name Projects.

Step 3: View Current Shares

sharing -l

This lists all active shared folders and their properties.

Example output:

Share point: /Users/alex/Documents/Projects
  Share name: Projects
  Protocols: SMB
  Guests: no

Step 4: Remove a Shared Folder

sudo sharing -r "ShareName"

Example:

sudo sharing -r Projects

3. Enabling AFP (Apple File Protocol) Sharing

AFP is deprecated but still available on some macOS versions.
To start the AFP service:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist

To stop it:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.AppleFileServer.plist

4. Managing SMB Users and Permissions

SMB sharing uses your macOS user accounts.
To allow guest access:

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server AllowGuestAccess -bool YES

To disallow:

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.smb.server AllowGuestAccess -bool NO

After changing SMB settings, restart the service:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.smbd.plist
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.smbd.plist

5. Connecting to a Shared Folder from Another Mac

From another macOS system, open Finder → Go → Connect to Server, then enter:

smb://<hostname or IP>/<ShareName>

Or use Terminal:

open smb://username@hostname/ShareName

Example:

open smb://alex@192.168.1.10/Projects

6. Troubleshooting Tips

  • If you can’t access shared folders, check the firewall:sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off (Only disable temporarily for testing.)
  • Verify smbd is running:sudo launchctl list | grep smbd
  • Check your SMB configuration:cat /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist

✅ Summary

ActionCommand
Enable SMB Sharingsudo launchctl load -w /System/Library/LaunchDaemons/com.apple.smbd.plist
List Sharessharing -l
Add Sharesudo sharing -a /path/to/folder -S ShareName
Remove Sharesudo sharing -r ShareName
Restart SMBsudo launchctl unload -w ... && sudo launchctl load -w ...

Using Terminal gives you precise control over file sharing on macOS — ideal for automation, scripting, or remote management.