macOS: Useful Commands

The intent of this page is a quick reference of some interesting and useful commands.  The following commands had been tested against macOS versions 11.5.2+ .

Serial Number

ioreg -rd1 -c IOPlatformExpertDevice | awk -F'"' '/IOPlatformSerialNumber/{print $4}'

Model Number

sysctl -n hw.model | cut -d "," -f 1 | tr -d '[0-9]_'

Current logged On User

ls -l /dev/console | awk '{ print $3 }'

Change Computer Name

Note: ComputerName works with FQDN.  The LocalHostName did not like the .local (as some sites would suggest).

hostname "$computerName".domain.com
scutil --set ComputerName "$computerName".domain.com
scutil --set LocalHostName "$computerName"
scutil --set HostName "$computerName"

Display names of all local users

I have added the ‘-x’ otherwise, any domain user that begins with uid 5XX will be displayed.

dscacheutil -q user | grep -A 3 -B 2 -x -e uid:\ 5'[0-9][0-9]'

Add SecureToken access to user

sysadminctl -adminUser <LocalAdminAccount> -adminPassword - -secureTokenOn <user> -password -

Domain User.  You changed a domain user account group access and want it to propagate quickly.

exec su -l $USER

Add color to vim, edit ~/.vimrc

syntax on

Check OS version

sw_vers -productVersion
  • 10.15 – Catalina
  • 10.14 – Mojave
  • 10.13 – High Sierra
  • 10.12 – Sierra
  • 10.11 – El Capitan
  • 10.10 – Yosemite
  • 10.9 – Mavericks
  • 10.8 – Mountain Lion
  • 10.7 – Lion
  • 10.6 – Snow Leopard
  • 10.5 – Leopard
  • 10.4 – Tiger
  • 10.3 – Panther
  • 10.2 – Jaguar
  • 10.1 – Puma
  • 10.0 – Cheetah

Source(s)

  • https://www.jamf.com/jamf-nation/discussions/26912/script-to-rename-computer-name-letter-of-first-name-and-last-name
  • https://knowledge.autodesk.com/support/flame-products/learn-explore/caas/sfdcarticles/sfdcarticles/Setting-the-Mac-hostname-or-computer-name-from-the-terminal.html
  • https://apple.stackexchange.com/questions/29874/how-can-i-list-all-user-accounts-in-the-terminal
  • https://www.jamf.com/jamf-nation/discussions/18602/migration-assistant-tool-and-migrating-ad-accounts-to-new-machines
  • https://superuser.com/questions/272061/reload-a-linux-users-group-assignments-without-logging-out
  • https://stackoverflow.com/questions/1653538/how-can-i-activate-vim-color-schemes-in-os-xs-terminal
  • https://www.digitalcitizen.life/macos-version