MacOS

🍎️🗒️

Apple Networking

sudo ipconfig set en0 DHCP
  • Renew DHCP Lease.
sudo dscacheutil -flushcache && sudo killall -HUP mDNSResponder
  • Clear DNS Cache.

Avahi Daemon

# Disable
sudo defaults write /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist ProgramArguments -array-add "-NoMulticastAdvertisements"
# Enable (Default)
sudo defaults write /System/Library/LaunchDaemons/com.apple.mDNSResponder.plist ProgramArguments -array "/usr/sbin/mDNSResponder" "-launchd"

Set Static IP Address

networksetup -setmanual "Ethernet" 192.168.2.100 255.255.255.0 192.168.2.1

Wireless:

/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | awk '/ SSID/ {print substr($0, index($0, $2))}'
  • Show current SSID.
defaults read /Library/Preferences/SystemConfiguration/com.apple.airport.preferences | grep LastConnected -A 7
  • Show Connection History.
security find-generic-password -D "AirPort network password" -a "SSID" -gw
  • Show SSID Passwords.
networksetup -setairportpower en0 on
  • Turn on wifi Adapter.

Measure Wireless Strength from CLI:

#!/bin/zsh
while i=1; do echo -ne 'Wifi signal strength:' $(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep CtlRSSI | awk {'print $2'}) '\r'; sleep 0.5; done

Forget SSID

#!/bin/bash

SSID=$(/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport -I | grep SSID | grep -v BSSID | awk '{print $2,$3,$4,$5}')

networksetup -removepreferredwirelessnetwork en0 $SSID
networksetup -setnetworkserviceenabled Wi-Fi off
networksetup -setnetworkserviceenabled Wi-Fi on
unset SSID

exit 0
sudo ln -s /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport /usr/local/bin/airport
airport -s

Firewall:

# Show Status
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --getglobalstate

# Enable
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate on

# Disable (Default)
sudo /usr/libexec/ApplicationFirewall/socketfilterfw --setglobalstate off

Add Application to Firewall

sudo /usr/libexec/ApplicationFirewall/socketfilterfw --add /path/to/file

Filevault

sudo fdesetup status
  • Filevault Status.
sudo fdesetup enable
  • Filevault Enable.

Time Machine:

Prevent Time Machine from Prompting to Use New Drives:

sudo defaults write /Library/Preferences/com.apple.TimeMachine DoNotOfferNewDisksForBackup -bool true

Show Time Machine Logs:

#!/bin/sh

filter='processImagePath contains "backupd" and subsystem beginswith "com.apple.TimeMachine"'

# show the last 12 hours
start="$(date -j -v-12H +'%Y-%m-%d %H:%M:%S')"

echo ""
echo "[History (from $start)]"
echo ""

log show --style syslog --info --start "$start" --predicate "$filter"

echo ""
echo "[Following]"
echo ""

log stream --style syslog --info --predicate "$filter"

Time Machine Stats:

# List all backups
tmutil listbackups

# Show differences
tmutil calculatedrift /path/to/backup/folder/plus/machine/name/folder

Verify Backup

sudo tmutil verifychecksums /path/to/backup

Disk Images

Create Disk Image From Folder Contents

hdiutil create -volname "Volume Name" -srcfolder /path/to/folder -ov diskimage.dmg

If you'd like to encrypt the disk image:

hdiutil create -encryption -stdinpass -volname "Volume Name" -srcfolder /path/to/folder -ov encrypted.dmg

By default, you'll be prompted for a password. You can automate that by piping in a password:

echo -n YourPassword | hdiutil create -encryption -stdinpass -volname "Volume Name" -srcfolder /path/to/folder -ov encrypted.dmg

Burn Disk Images to DVD

This command applies to .iso, .img and .dmg images.

hdiutil burn /path/to/image_file

Disable Disk Image Verification

defaults write com.apple.frameworks.diskimages skip-verify -bool true && \
defaults write com.apple.frameworks.diskimages skip-verify-locked -bool true && \
defaults write com.apple.frameworks.diskimages skip-verify-remote -bool true

Make Volume OS X Bootable

bless --folder "/path/to/mounted/volume/System/Library/CoreServices" --bootinfo --bootefi

Mount Disk Image

hdiutil attach /path/to/diskimage.dmg

Unmount Disk Image

hdiutil detach /dev/disk2s1

Write Disk Image to Volume

Like the Disk Utility "Restore" function.

sudo asr -restore -noverify -source /path/to/diskimage.dmg -target /Volumes/VolumeToRestoreTo

Hardware Misc:

List All Hardware Ports

networksetup -listallhardwareports

Remaining Battery Percentage

pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f1 -d';'

Remaining Battery Time

pmset -g batt | egrep "([0-9]+\%).*" -o --colour=auto | cut -f3 -d';'

Show Connected Device's UDID

system_profiler SPUSBDataType | sed -n -e '/iPad/,/Serial/p' -e '/iPhone/,/Serial/p'

Show Current Screen Resolution

system_profiler SPDisplaysDataType | grep Resolution

Show CPU Brand String

sysctl -n machdep.cpu.brand_string

Prevent System Sleep

Prevent sleep for 1 hour:

caffeinate -u -t 3600

Show All Power Management Settings

sudo pmset -g

Put Display to Sleep after 15 Minutes of Inactivity

sudo pmset displaysleep 15

Put Computer to Sleep after 30 Minutes of Inactivity

sudo pmset sleep 30

Check System Sleep Idle Time

sudo systemsetup -getcomputersleep

Set System Sleep Idle Time to 60 Minutes

sudo systemsetup -setcomputersleep 60

Turn Off System Sleep Completely

sudo systemsetup -setcomputersleep Never

Automatic Restart on System Freeze

sudo systemsetup -setrestartfreeze on

Finder:

Show External Media

# Enable
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool true && \
killall Finder

# Disable (Default)
defaults write com.apple.finder ShowExternalHardDrivesOnDesktop -bool false && \
killall Finder

Show Internal Media

# Enable
defaults write com.apple.finder ShowHardDrivesOnDesktop -bool true && \
killall Finder

# Disable (Default)
defaults write com.apple.finder ShowHardDrivesOnDesktop -bool false && \
killall Finder

Show Removable Media

# Enable
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool true && \
killall Finder

# Disable (Default)
defaults write com.apple.finder ShowRemovableMediaOnDesktop -bool false && \
killall Finder

Show Network Volumes

# Enable
defaults write com.apple.finder ShowMountedServersOnDesktop -bool true && \
killall Finder

# Disable (Default)
defaults write com.apple.finder ShowMountedServersOnDesktop -bool false && \
killall Finder

Increase Number of Recent Places

defaults write -g NSNavRecentPlacesLimit -int 10 && \
killall Finder

Show All File Extensions

defaults write -g AppleShowAllExtensions -bool true

Show Hidden Files

# Show All
defaults write com.apple.finder AppleShowAllFiles true

# Restore Default File Visibility
defaults write com.apple.finder AppleShowAllFiles false

Show Full Path in Finder Title

defaults write com.apple.finder _FXShowPosixPathInTitle -bool true

Toggle Folder Visibility in Finder

By default, the ~/Library folder is hidden. You can easily show it again. The same method works with all other folders.

# Hidden (Default)
chflags hidden ~/Library

# Visible
chflags nohidden ~/Library

Expand Save Panel by Default

defaults write -g NSNavPanelExpandedStateForSaveMode -bool true && \
defaults write -g NSNavPanelExpandedStateForSaveMode2 -bool true

Desktop Icon Visibility

# Hide Icons
defaults write com.apple.finder CreateDesktop -bool false && \
killall Finder

# Show Icons (Default)
defaults write com.apple.finder CreateDesktop -bool true && \
killall Finder

Path Bar

# Show
defaults write com.apple.finder ShowPathbar -bool true

# Hide (Default)
defaults write com.apple.finder ShowPathbar -bool false

Scrollbar Visibility

Possible values: WhenScrolling, Automatic and Always.

defaults write -g AppleShowScrollBars -string "Always"

Status Bar

# Show
defaults write com.apple.finder ShowStatusBar -bool true

# Hide (Default)
defaults write com.apple.finder ShowStatusBar -bool false

Save to Disk by Default

Sets default save target to be a local disk, not iCloud.

defaults write -g NSDocumentSaveNewDocumentsToCloud -bool false

Set Current Folder as Default Search Scope

defaults write com.apple.finder FXDefaultSearchScope -string "SCcf"

Set Default Finder Location to Home Folder

defaults write com.apple.finder NewWindowTarget -string "PfLo" && \
defaults write com.apple.finder NewWindowTargetPath -string "file://${HOME}"

Recursively Delete .DS_Store Files

find . -type f -name '.DS_Store' -ls -delete

Kernel Extensions

sudo kextstat -l
  • List Kernel Extensions
sudo kextunload -b com.apple.driver.ExampleSomething
  • Unload Kernel Extensions

Updates

sudo softwareupdate -ia
  • Install all available updates.
defaults write com.apple.SoftwareUpdate ScheduleFrequency -int 1
  • Change the Update Checking Interval.
sudo softwareupdate --list
  • Show available updates.

Audio:

sudo osascript -e "set Volume 0"
  • Mute Audio.

Chime on Charge:

# Enable (Default)
defaults write com.apple.PowerChime ChimeOnNoHardware -bool false && \
open /System/Library/CoreServices/PowerChime.app

# Disable
defaults write com.apple.PowerChime ChimeOnNoHardware -bool true && \
killall PowerChime

Startup Chime:

# Enable
sudo nvram StartupMute=%00

# Disable (Default)
sudo nvram StartupMute=%01

# Alt:
sudo nvram SystemAudioVolume=" "

Convert Audio File to iPhone Ringtone

afconvert input.mp3 ringtone.m4r -f m4af

Create Audiobook From Text

Uses "Alex" voice, a plain UTF-8 encoded text file for input and AAC output.

say -v Alex -f file.txt -o "output.m4a"

Play Audio File

You can play all audio formats that are natively supported by QuickTime.

afplay -q 1 filename.mp3

Speak Text with System Default Voice

say 'All your base are belong to us!'

Apps:

List All Apps Downloaded from App Store

# Via find
find /Applications -path '*Contents/_MASReceipt/receipt' -maxdepth 4 -print |\sed 's#.app/Contents/_MASReceipt/receipt#.app#g; s#/Applications/##'

# Via Spotlight
mdfind kMDItemAppStoreHasReceipt=1

SSH:


Apple Remote Desktop:

Activate And Deactivate the ARD Agent and Helper

# Activate And Restart the ARD Agent and Helper
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -activate -restart -agent -console

# Deactivate and Stop the Remote Management Service
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -deactivate -stop

Remote Desktop Sharing

# Allow Access for All Users and Give All Users Full Access
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -configure -allowAccessFor -allUsers -privs -all

# Disable ARD Agent and Remove Access Privileges for All Users (Default)
sudo /System/Library/CoreServices/RemoteManagement/ARDAgent.app/Contents/Resources/kickstart -deactivate -configure -access -off

Remove Apple Remote Desktop Settings

sudo rm -rf /var/db/RemoteManagement ; \
sudo defaults delete /Library/Preferences/com.apple.RemoteDesktop.plist ; \
defaults delete ~/Library/Preferences/com.apple.RemoteDesktop.plist ; \
sudo rm -r /Library/Application\ Support/Apple/Remote\ Desktop/ ; \
rm -r ~/Library/Application\ Support/Remote\ Desktop/ ; \
rm -r ~/Library/Containers/com.apple.RemoteDesktop

Fonts:

Clear Font Cache for Current User

atsutil databases -removeUser && \
atsutil server -shutdown && \
atsutil server -ping

Get SF Mono Fonts

cp -v /Applications/Xcode-beta.app/Contents/SharedFrameworks/DVTKit.framework/Versions/A/Resources/Fonts/SFMono-* ~/Library/Fonts

Logs & Reporting:

System Software Version

sw_vers -productVersion
system_profiler SPSoftwareDataType
defaults read loginwindow SystemVersionStampAsString
sudo sysdiagnose -f ~/Desktop/
  • Run performance / diagnostic and place results on the Desktop.
mdutil -E /path/to/volume
  • Erase & Rebuild Spotlight Search.
/usr/bin/profiles list --type configuration --output stdout-xml
  • Dump Profiles to XML.

Links:

MacOS Deployment Overview:

MacOS Networking Articles:


MacOS Support Pages:

Useful for users.


MacOS VMs

TO-DO.

MacOS VM

MacOS Automated Setup:

TO-DO.