Skip to content

macOS

Apple's Unix desktop operating system.

Useful Commands

  • caffeinate
  • mdutil
  • networksetup
  • scutil
  • serverinfo
  • sharing
  • tccutil

Logout user from a shell

sudo launchctl bootout gui/$(id -u "$USERNAME")

or

sudo launchctl bootout user/$(id -u "$USERNAME")

Fix sshd client timeout config

macOS has an /etc/ssh/sshd_config that will never disconnect idle clients. With sketchy wifi, or for a variety of other reasons, this can cause ssh connections to pile up, causing a DoS on the ssh server. When this happens, a client that is attempting to connect may see kex_exchange_identification: read: Connection reset by peer. To fix this, set the following config values to anything other than 0. See man 5 sshd_config for more info.

The following settings would allow an unresponsive ssh session 10 minutes before terminating it:

ClientAliveInterval 120
ClientAliveCountMax 5

Using BSD sed, you can quickly set this:

sudo sed -i '' -E 's/^#?ClientAliveInterval [0-9]+/ClientAliveInterval 120/ ; s/^#?ClientAliveCountMax [0-9]+/ClientAliveCountMax 5/ ;' /etc/ssh/sshd_config
sudo bash -c "launchctl stop com.openssh.sshd ; launchctl start com.openssh.sshd ;"

Show hardware info

system_profiler SPHardwareDataType

Install package from CLI

Use brew.sh for most things. Otherwise:

sudo installer -pkg /Volumes/ExifTool-9.16/ExifTool-9.16.pkg -target /

Start FTP server

sudo -s launchctl load -w /System/Library/LaunchDaemons/ftp.plist

Check swap usage

sysctl vm.swapusage

Disable wifi disconnect when locking screen

sudo /System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources airport en1 prefs DisconnectOnLogout=NO

Show some downloaded files

This shows a list of all the quarantine checked downloads:

sqlite3 ~/Library/Preferences/com.apple.LaunchServices.QuarantineEventsV* 'select distinct LSQuarantineDataURLString from LSQuarantineEvent'

Send Notifications from Terminal

sudo gem install terminal-notifier
terminal-notifier -message "Hello, this is my message" -title "Message Title"

Enable verbose eap logging

sudo defaults write /Library/Preferences/SystemConfiguration/com.apple.eapolclient LogFlags -int -1

Network

  • gif0 - Generic Tunnel Interface. See man gif.
  • stf0 - Six To Four tunnel. See man stf.

Migration Assistant

Some files are not migrated using Migration Assistant. EG:

  • /etc/hosts
  • crontabs
  • /etc/apache2/httpd.conf

Remap caps-lock to escape

This remaps using a different mechanism than what the Keyboard pref pane uses, so the change will not be reflected there, so you will want to make sure to save both of these functions so you can reset when you need normal behavior.

caps_to_esc_map(){
    hidutil property --set \
    '{"UserKeyMapping":[{"HIDKeyboardModifierMappingSrc":0x700000039,"HIDKeyboardModifierMappingDst":0x700000029}]}'
}

caps_to_esc_unmap(){
    hidutil property --set '{"UserKeyMapping":[]}'
}