Files
oam/knowledge base/mac os x.md
2022-04-24 15:27:47 +02:00

9.3 KiB

Mac OS X

TL;DR

# install a .pkg file from cli
# target needs to be a device, not a path
installer -pkg /path/to/non-root-package.pkg -target CurrentUserHomeDirectory
sudo installer -pkg /path/to/root-needed-package.pkg -target /

# install xcode cli tools
xcode-select --install

# list all available updates
softwareupdate --list --all

# install all recommended updates agreeing to software license agreement without interaction and automatically restart if required
softwareupdate --install --recommended --restart --agree-to-license

# download (but not install) recommended updates
softwareupdate --download --recommended

# add a password to the default keychain
# the password needs to be left last
security add-generic-password -a johnny -s github -w 'b.good'

# add a password to the default keychain giving it some optional data
security add-generic-password -a johnny -s github -l work -j 'my key for work' -w 'b.good'

# update a passwork value
security add-generic-password -a johnny -s github -l work -U -w 'new-pass'

# print a password to stdout
security find-generic-password -w -a johnny -s github
security find-generic-password -w -l work
security find-generic-password -w -l work -s github

# delete a password from the default keychain
security delete-generic-password -a johnny -s github

# get the host's bonjour name
scutil --get LocalHostName
/usr/libexec/PlistBuddy -c "Print :System:Network:HostNames:LocalHostName" /Library/Preferences/SystemConfiguration/preferences.plist

# get the host's netbios name
defaults read /Library/Preferences/SystemConfiguration/com.apple.smb.server NetBIOSName
/usr/libexec/PlistBuddy -c "Print :NetBIOSName" /Library/Preferences/SystemConfiguration/com.apple.smb.server.plist

# get the host's computer name
scutil --get ComputerName
/usr/libexec/PlistBuddy -c "Print :System:System:ComputerName" /Library/Preferences/SystemConfiguration/preferences.plist

Xcode CLI tools

xcode-select --install

Headless installation

In CLI:

  1. prompt the softwareupdate utility to list the Command Line Tools

    touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
    
  2. get their label

    export cli_tools_label="$(/usr/sbin/softwareupdate -l \
    | grep -B 1 -E 'Command Line Tools' \
    | awk -F'*' '/^ *\\*/ {print $2}' \
    | sed -e 's/^ *Label: //' -e 's/^ *//' \
    | sort -V \
    | tail -n1)"
    
  3. install them

    /usr/sbin/softwareupdate -i ${cli_tools_label.stdout}
    

As Ansible task:

- name: Check Command Line Tools are avilable
  command: xcode-select --print-path
  ignore_errors: true
  register: cli_tools_check
- name: Trying headless installing command line tools
  when: cli_tools_check is failed
  block:
  - name: Prompt the 'softwareupdate' utility to list the Command Line Tools
    file:
      path: /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress
      state: touch
  - name: Get CLI tools label
    shell: >-
      /usr/sbin/softwareupdate -l
      | grep -B 1 -E 'Command Line Tools'
      | awk -F'*' '/^ *\\*/ {print $2}'
      | sed -e 's/^ *Label: //' -e 's/^ *//'
      | sort -V
      | tail -n1
    register: cli_tools_label
  - name: Install CLI tools
    command: /usr/sbin/softwareupdate -i '{{cli_tools_label.stdout}}'
    register: headless_cli_tools_installation
  - name: Fail with error
    when: headless_cli_tools_installation is failed
    fail:
      msg: Command Line Tools are not installed. Please execute 'xcode-select --install' in a terminal and accept the license first

Upgrade

See How to update Xcode from command line for details.

# remove and reinstall
sudo rm -rf /Library/Developer/CommandLineTools
xcode-select --install

Hidden settings

Note: once set something, you'll probably need to restart the dock with killall Dock

# show hidden apps indicators in the dock
defaults write com.apple.dock showhidden -bool TRUE

# reset changes to the dock
defaults delete com.apple.dock

# change the number of columns and rows in the springboard
defaults write com.apple.dock springboard-columns -int 9
defaults write com.apple.dock springboard-rows -int 7

# reset changes to the launchpad
defaults delete com.apple.dock springboard-rows
defaults delete com.apple.dock springboard-columns
defaults write com.apple.dock ResetLaunchPad -bool TRUE

Resize an image from CLI

Note:

  • edits the input image
  • -Z retains ratio
sips -Z 1000 Downloads/IMG_20190527_013903.jpg

Boot keys cheatsheet

only available for Intel based Macs

To use any of these key combinations, press and hold the keys immediately after pressing the power button to turn on your Mac, or after your Mac begins to restart. Keep holding until the described behavior occurs.

Combination Behaviour
⌥ Option or Alt Start to Startup Manager, which allows you to choose other available startup disks or volumes. If your Mac is using a firmware password, you're prompted to enter the password
⌥ Option + ⌘ Command + P + R Reset the NVRAM or PRAM. If your Mac is using a firmware password, it ignores this key combination or starts up from Recovery
⇧ Shift Start in safe mode. Disabled when using a firmware password
⌘ Command + R Start from the built-in Recovery system
⌥ Option + ⌘ Command + R or ⇧ Shift + ⌥ Option + ⌘ Command + R Start from Recovery over the Internet. It installs different versions of macOS, depending on the key combination you use while starting up. If your Mac is using a firmware password, you're prompted to enter the password
⏏ Eject or F12 or mouse button or trackpad button Eject a removable media, such as an optical disc. Disabled when using a firmware password
T Start in target disk mode. Disabled when using a firmware password
⌘ Command + V Start in verbose mode. Disabled when using a firmware password
D Start to Apple Diagnostics
⌥ Option + D Start to Apple Diagnostics over the Internet. Disabled when using a firmware password
N Start from a NetBoot server, if your Mac supports network startup volumes. Disabled when using a firmware password
⌥ Option + N Start from a NetBoot server and use the default boot image on it. Disabled when using a firmware password
⌘ Command + S Start in single-user mode. Disabled in macOS Mojave or later, or when using a firmware password

Update from CLI

# list all available updates
softwareupdate --list --all

# install all recommended updates agreeing to software license agreement without interaction and automatically restart if required
softwareupdate --install --recommended --restart --agree-to-license

# download (but not install) recommended updates
softwareupdate --download --recommended

Keychain access from CLI

Save a password with the following settings:

  • user (a.k.a. account): johnny
  • password: b.good
  • service name: github
  • [optional] entry name (a.k.a. label): work; if not given, the service name will be used
  • [optional] comment: my key for work; if not given, it will be left blank

the password's value needs to be given last

# add the password to the default keychain
security add-generic-password -a johnny -s github -w 'b.good'
# also give it some optional data
security add-generic-password -a johnny -s github -l work -j 'my key for work' -w 'b.good'
# update the passwork value
security add-generic-password -a johnny -s github -l work -U -w 'new-pass'

# print the above password to stdout
security find-generic-password -w -a johnny -s github
security find-generic-password -w -l work
security find-generic-password -w -l work -s github

# delete it
security delete-generic-password -a johnny -s github

Further readings