Useful Commands

Last Modification

macOS

Firewall: Turn on or off via the command line

Turn the firewall off:

sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 0
Turn the firewall on for specific apps/services:
sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 1
Turn the firewall on for essential services (DHCP, IPSEC) and block everything else:
sudo defaults write /Library/Preferences/com.apple.alf globalstate -int 2

Remote Desktop (ARD)

The kickstart tool is what us used to toggle the Remote Management setting on macOS. Apple has a good writeup on many of the options. kickstart options can be found by running:

sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -help
Restart the ARD Agent:
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -restart -agent
Disable the ARD Agent:
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -deactivate -configure -access -off

Remote Login (SSH/SFTP)

To enable and disable remote loginvia the command line: Enable:

sudo systemsetup -setremotelogin on
Disable:
sudo systemsetup -setremotelogin off

Screen Sharing

Enable Screen Sharing:

sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
Disable Screen Sharing:
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.screensharing.plist

File Sharing

Enable:

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

Disable:

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

Disable:

cupsctl --no-share-printers

Auto Updates - OS & App Store

To enable app updates from the Mac App Store to be installed automatically, run the following command with root privileges:

sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdate -bool TRUE
To stop app updates from the Mac App Store from being installed automatically, run the following command with root privileges:
sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdate -bool FALSE
To enable OS X updates to be installed automatically, run the following command with root privileges:
sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdateRestartRequired -bool TRUE
To stop OS X updates from being installed automatically, run the following command with root privileges:
sudo defaults write /Library/Preferences/com.apple.commerce AutoUpdateRestartRequired -bool FALSE

CLI Random Password Generator

Using OpenSSL:

openssl rand -base64 26
If you need to avoid characters like / or + you can use hex instead:
openssl rand -hex 26
Want to randomize it more, pipe it through md5 and trim the md5 hast of the output to the number of characters you want:
openssl rand -base64 26 |md5 |head -c26;echo

Apache Server on macOS

Starting the apache server:

sudo launchctl load -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Stoping the apache server:

sudo launchctl unload -w /System/Library/LaunchDaemons/org.apache.httpd.plist

Windows

Enable/Disable NetBIOS

0 Use NetBIOS setting from the DHCP server
1 Enable NetBIOS over TCP/IP
2 Disable NetBIOS over TCP/IP

To enable or disable NetBIOS you do it by interface, so the first command is to determine the interface number.

wmic nicconfig get caption,index,TcpipNetbiosOptions
To disable run something like the following:
wmic nicconfig where index=8 call SetTcpipNetbios 2
To enable run something like the following:
wmic nicconfig where index=8 call SetTcpipNetbios 1

Linux

macOS & Linux

Format JSON in VIM

Add the following line to your vimrc file.

com! FormatJSON %!python -m json.tool
Now when you are editing a file and want it formatted properly in JSON use the following command:
:FormatJSON