Mac OS instructions

Add to user administrator privileges and all access abilities:

dscl . -append /groups/admin GroupMembership <USERNAME>

# confirm
dscl . -read /groups/admin GroupMembership

Download and install DMG from commandline:

curl -O https://site.com/<file.dmg>
sudo hdiutil attach <file.dmg>
sudo installer -package /Volumes/<volume>/<app_name.pkg> -target /
sudo hdiutil detach /Volumes/<volume>

Show partition information:

diskutil list

Check resize partition restrictions:

diskutil list
diskutil resizeVolume <disk> limits

Resize Mac OS partition:

sudo diskutil resizeVolume <disk> <size>GB

Get CPU Information:

sysctl -n machdep.cpu.brand_string

system_profiler | grep Processor

sysctl -a | grep machdep.cpu

# CPU instructions
sysctl -a | grep machdep.cpu.features
sysctl -a | grep machdep.cpu.leaf7_features

On boot, hold Command + Option + P and R keys to clear NVRAM.

Get username of user that currently uses GUI of WindowServer:

stat -f '%Su' /dev/console
# or
ls -l /dev/console | cut -d " " -f4

Converting plist/xml:

# convert from binary PLIST to XML
plutil -convert xml1 <binary.plist>

# convert from XML to binary PLIST
plutil -convert binary1 <xml.plist>

Get System Info on Mac OS:

hostinfo

Get screencapture via terminal:

screencapture -xr -t gif capture.gif

Turn on performance mode for macOS Server (OS X El Capitan 10.11 and later). Provides extended system limits (maxproc, etc.):

# check
nvram boot-args

# turn on (reboot after turn on)
sudo nvram boot-args="serverperfmode=1 $(nvram boot-args 2>/dev/null | cut -f 2-)"


# turn off (reboot after turn off)
sudo nvram boot-args="$(nvram boot-args 2>/dev/null | sed -e $'s/boot-args\t//;s/serverperfmode=1//')"

Delete a local account:

dscl localhost delete /Local/Default/Users/<user>
rm -rf /Users/<user>

Show listen ports:

sudo lsof -PiTCP -sTCP:LISTEN

Manipulating OS X login items from command line

Snoop new processes as they are executed, creatbyproc.d for files:

sudo newproc.d
sudo creatbyproc.d

List memory usage in real time:

sudo fs_usage | grep -v 0.0000

Force flush cached memory:

sudo sync && sudo purge

Enable/disable swapping in Mac OS X (not checked solution):

# to disable swap (pager daemon)
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

# after stopping pager daemon, you may want to remove swapfiles
sudo rm /private/var/vm/swapfile*

# to enable swap, you need to boot in Single Mode (Hold [CMD + S] at booting time) and run this command:
sudo launchctl load /System/Library/LaunchDaemons/com.apple.dynamic_pager.plist

Remove RecoveryHD partition:

diskutil list
diskutil eraseVolume HFS+ Blank /dev/<RecoveryHD_ID>
diskutil mergePartitions HFS+ <nameOfMainDisk> <mainDisk_ID> <RecoveryHD_ID>

List of users (with information):

dscacheutil -q user

# only not system users
dscacheutil -q user | grep -A 3 -B 2 -e uid:\ 5'[0-9][0-9]'

Default background pictures paths:

# all pictures
/Library/Desktop\ Pictures/*.jpg
/Library/Desktop\ Pictures/Solid\ Colors/*.jpg

# default background
/System/Library/CoreServices/DefaultDesktop.jpg

Logout user:

sudo launchctl bootout user/$(id -u test)

# graceful method
sudo launchctl asuser $(id -u <USERNAME>) osascript -e 'tell application "System Events"' -e 'log out' -e 'keystroke return' -e 'end tell'

Set/get Computer/Host/LocalHost names. The settings are stored in /Library/Preferences/SystemConfiguration/preferences.plist.:

# set
sudo scutil --set ComputerName "newname"
sudo scutil --set LocalHostName "newname"
sudo scutil --set HostName "newname"

# get
scutil --get ComputerName
scutil --get LocalHostName
scutil --get HostName

Allow only ssh key authentication /etc/ssh/sshd_config:

PasswordAuthentication no
ChallengeResponseAuthentication no

Control autoupdate:

# https://derflounder.wordpress.com/2014/12/29/managing-automatic-app-store-and-os-x-update-installation-on-yosemite/
# to disable the automatic update check
softwareupdate --schedule off
# or
defaults write /Library/Preferences/com.apple.SoftwareUpdate AutomaticCheckEnabled -bool FALSE

Allow to install apps from anywhere:

sudo spctl --master-disable

Switch user:

uid=`id -u ${username}`
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -switchToUserID ${uid}

Restart Screensharing:

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

Enable/disable fast user switching:

# enable
sudo defaults write /Library/Preferences/.GlobalPreferences MultipleSessionEnabled -bool YES

# disable
sudo defaults write /Library/Preferences/.GlobalPreferences MultipleSessionEnabled -bool no

Open multiple instances of application:

open -na "Messages"
open -n /Applications/Messages.app/Contents/MacOS/Messages

Run application at another user:

sudo login -f <username> /Applications/Messages.app/Contents/MacOS/Messages

Remote connect:

https://support.apple.com/en-us/HT201710

LaunchDaemons

/System/Library/LaunchDaemons/ - load when system running

Bonjour:

/System/Library/LaunchDaemons/com.apple.mDNSResponder.plist
/System/Library/LaunchDaemons/com.apple.mDNSResponderHelper.plist

Launchctl

Agents and daemons paths:

~/Library/LaunchAgents         # Per-user agents provided by the user.
/Library/LaunchAgents          # Per-user agents provided by the administrator.
/Library/LaunchDaemons         # System-wide daemons provided by the administrator.
/System/Library/LaunchAgents   # Per-user agents provided by Mac OS X.
/System/Library/LaunchDaemons  # System-wide daemons provided by Mac OS X.

Install GUI application:

brew install Caskroom/cask/launchcontrol

Create user agent:

mkdir $HOME/Library/LaunchAgents
# drwxr-xr-x    3 spider  staff   102 Apr 28 16:46 LaunchAgents

Disabled jobs are stored in /var/db/com.apple.xpc

launchctl
launchctl   list                                    # list all loaded services (PID LAST_EXIT_CODE NAME)
                    <service_name>                  # show info about service

launchctl   print-disabled user/<user_id>           # show disabled services for user

launchctl   enable|disable user/<user_id>/<service> # disable service for user
                           system/<service>         # disable daemons whir starts as root

launchctl   remove <service>                        # unload service from launchd
launchctl   unload <path_to_plist>                  # unload service from launchd
                   -w                               # pick disabled (save after reboot)

launchctl   load <path_to_plist>                    # load service to launchd
                   -w                               # pick enabled (save after reboot)

launchctl   start|stop                              # start|stop service

# run app as another user
sudo launchctl asuser $(id -u <user>) <command>

Brew

Installation:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install some app:

brew install <app>

System limits

Note

To increase system limits turn on performance mode. Mac OS Sierra:

sudo nvram boot-args="serverperfmode=1 $(nvram boot-args 2>/dev/null | cut -f 2-)"
launchctl limit

# set limit (before system restart)
sysctl -w kern.maxproc=2048
sysctl -w kern.maxfiles=5000

sysctl -a

# show sysem limits
sysctl kern.maxproc
sysctl kern.maxfiles

# show user limits
ulimit -a

# set user limit
ulimit -u 2048

launchctl limit maxfiles 1000000 1000000
launchctl limit maxproc 1000000 1000000

# set mac server mode
sudo serverinfo --setperfmode true

For 10.9 (Mavericks), 10.10 (Yosemite), 10.11 (El Capitan), and 10.12 (Sierra)

Create file (owner: root:wheel, mode: 0644) /Library/LaunchDaemons/limit.maxfiles.plist:

<?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>limit.maxfiles</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxfiles</string>
          <string>524288</string>
          <string>524288</string>
        </array>
       <key>RunAtLoad</key>
        <true/>
      <key>ServiceIPC</key>
        <false/>
    </dict>
  </plist>

Create file (owner: root:wheel, mode: 0644) /Library/LaunchDaemons/limit.maxproc.plist:

<?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>limit.maxproc</string>
      <key>ProgramArguments</key>
        <array>
          <string>launchctl</string>
          <string>limit</string>
          <string>maxproc</string>
          <string>2048</string>
          <string>2048</string>
        </array>
      <key>RunAtLoad</key>
        <true />
      <key>ServiceIPC</key>
        <false />
    </dict>
  </plist>