Keyboard Shortcuts
There are many useful keyboard shortcuts that I’ve come across over time, and I try to include them here so they don’t get forgotten.
DevTools keyboard shortcut
COMMAND+SHIFT+I
Resource: https://developer.chrome.com/docs/devtools/shortcuts/
Open Finder
OPTION + COMMAND + SPACE
Open Spotlight
COMMAND + SPACE
Maximize window
COMMAND + TAB to highlight the minimized window you want to maximize.
Before releasing the COMMAND button, hit the OPTION button (ALT on a windows keyboard).
Resource: https://superuser.com/questions/196141/keyboard-shortcut-to-unhide-or-unminimize-a-window-in-os-x
Put mac to sleep
OPTION + COMMAND + POWER
Put display to sleep
CTRL + SHIFT + POWER
Resource: https://support.apple.com/en-us/HT201236
Jump to URL bar in Chrome
COMMAND + L
Resource: https://apple.stackexchange.com/questions/11573/chrome-keyboard-shortcut-to-go-to-address-bar
Show hidden files and folders in Finder
COMMAND + SHIFT + .
Resource: https://ianlunn.co.uk/articles/quickly-showhide-hidden-files-mac-os-x-mavericks/
Go to folder in Finder
COMMAND + SHIFT + G
Resource: https://blog.bejarano.io/fixing-cron-jobs-in-mojave.html
Lock Screen
CTRL + COMMAND + Q
Mirror display (or stop mirroring)
COMMAND + F1
Highlight text
Hold OPTION + COMMAND and select the text you want with your mouse.
Downloads for Apple Developers
https://developer.apple.com/download/more/
Check ssh service status
sudo systemsetup -getremotelogin
Enable SSH
sudo systemsetup -setremotelogin on
Disable SSH
sudo systemsetup -setremotelogin off
Resource: http://osxdaily.com/2016/08/16/enable-ssh-mac-command-line/
Disable screensharing
sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.screensharing.plist
Script to create new users
Be sure to run this as root
.
#!/bin/bash
USERNAME=user1
FULLNAME="User 1"
PASSWORD="yourpasswordgoeshere"
SECONDARY_GROUPS="staff"
if [[ $UID -ne 0 ]]; then echo "Please run $0 as root." && exit 1; fi
# Find the next available user ID
MAXID=$(dscl . -list /Users UniqueID | awk '{print $2}' | sort -ug | tail -1)
USERID=$((MAXID+1))
# Create the user account
dscl . -create /Users/$USERNAME
dscl . -create /Users/$USERNAME UserShell /bin/bash
dscl . -create /Users/$USERNAME RealName "$FULLNAME"
dscl . -create /Users/$USERNAME UniqueID "$USERID"
dscl . -create /Users/$USERNAME PrimaryGroupID 20
dscl . -create /Users/$USERNAME NFSHomeDirectory /Users/$USERNAME
dscl . -passwd /Users/$USERNAME $PASSWORD
# Add use to any specified groups
for GROUP in $SECONDARY_GROUPS ; do
dseditgroup -o edit -t user -a $USERNAME $GROUP
done
# Create the home directory
createhomedir -c -u $USERNAME > /dev/null
echo "Created user #$USERID: $USERNAME ($FULLNAME)"
Remove user account
sudo dscl . -delete /Users/$USERNAME
Delete the home directory if you created it:
sudo rm -rf /Users/$USERNAME
Resource: https://apple.stackexchange.com/questions/310308/delete-a-standard-user-from-mac-os
List users
sudo dscl . list /Users | grep -v "^_"
Take screenshot
The output will be written to /var/tmp/.tmp
screencapture -x /var/tmp/.tmp
Get version of OSX Running
sw_vers -productVersion
Resource: https://www.tecmint.com/send-a-message-to-logged-users-in-linux-terminal/
Mount NFS share
sudo mkdir /mnt/name_of_system_folder
sudo mount -t nfs \
-o rsize=65536,wsize=65536,intr,hard,tcp,\
locallocks,rdirplus,readahead=128 system.com:<path to volume mount> \
/mnt/name_of_system_folder
Resources: https://care.qumulo.com/hc/en-us/articles/115008111268-Recommended-NFS-Mount-Options https://willhaley.com/blog/mount-nfs-share-on-a-mac/
Unmount NFS share
sudo umount -Af -t nfs,smbfs
Resources: https://askubuntu.com/questions/292043/how-to-unmount-nfs-when-server-is-gone https://superuser.com/questions/249611/how-to-forceably-unmount-stuck-network-share-in-mac-os-x
Automount an NFS share in /Volumes
https://gist.github.com/L422Y/8697518
SSHFS
Install:
brew cask install osxfuse
brew install sshfs
Mount remote system:
mkdir /mnt/remote_system
cd /mnt/remote_system
sudo sshfs -o allow_other,defer_permissions user@remote_system.com:/ /mnt/remote_system
Resources: https://www.digitalocean.com/community/tutorials/how-to-use-sshfs-to-mount-remote-file-systems-over-ssh https://howchoo.com/g/ymmxmzlmndb/how-to-install-sshfs
Creating plist files
Plist files can be used as an alternative to cron jobs. This specific example will run even if there are no users logged in.
This particular one will run a python script, not_evil.py
every day at 3 am.
Create /Library/LaunchDaemons/not.evil.plist
with this content:
<?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>not.evil</string>
<key>Program</key>
<string>/Users/notabackdooruser/not_evil.py</string>
<key>WorkingDirectory</key>
<string>/Users/notabackdooruser/</string>
<key>StartCalendarInterval</key>
<dict>
<key>Hour</key>
<integer>3</integer>
<key>Minute</key>
<integer>0</integer>
</dict>
<key>EnvironmentVariables</key>
<dict>
<key>http_proxy</key>
<string>http://companyproxy.com:80</string>
</dict>
</dict>
</plist>
Make sure the file is owned by root (sudo chown root job.plist
) and that
wheel is set for the group (sudo chgrp wheel job.plist
).
To load the file to the running daemons, run:
sudo launchctl load not.evil.plist
To remove the file from the running daemons, run:
sudo launchctl unload not.evil.plist
Another example
This will run the a bash script in the context of a specific user every minute.
It will output the results to /Users/yourusername/brew-output.txt
or /Users/yourusername/brew-errors.txt
if there is an error in running the command.
To start, navigate to ~/Library/LaunchAgents
and put this in a file called com.yourusername.job-name.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>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
</dict>
<key>Label</key>
<string>com.yourusername.job-name</string>
<key>ProgramArguments</key>
<array>
<string>bash</string>
<string>-c</string>
<string>/Users/yourusername/Library/LaunchAgents/bashscript.sh</string>
</array>
<key>StartInterval</key>
<integer>60</integer>
<key>StandardOutPath</key>
<string>/Users/yourusername/brew-output.txt</string>
<key>StandardErrorPath</key>
<string>/Users/yourusername/brew-errors.txt</string>
</dict>
</plist>
Follow this by adding the bash script to
/Users/yourusername/Library/LaunchAgents/bashscript.sh
and then running:
chmod +x /Users/yourusername/Library/LaunchAgents/bashscript.sh
With the plist and script in place, you can enable it with:
launchctl load com.yourusername.job-name.plist
and disable it with:
launchctl unload com.yourusername.job-name.plist
A complete example that automates brew updates for a user can be found here: https://gist.github.com/l50/884919db8c6819e73ebe2f50c6928fcc
Resources: https://alvinalexander.com/mac-os-x/mac-osx-startup-crontab-launchd-jobs https://medium.com/@chetcorcos/a-simple-launchd-tutorial-9fecfcf2dbb3
Run bash script on startup
~/Library/LaunchAgents/com.yourusername.bashscript.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>EnvironmentVariables</key>
<dict>
<key>PATH</key>
<string>/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:</string>
</dict>
<key>Label</key>
<string>com.yourusername.job-name</string>
<key>Program</key>
<string>/Users/yourusername/Library/LaunchAgents/bashscript.sh</string>
<key>RunAtLoad</key>
<true/>
</dict>
</plist>
Run every 6 hours
<key>StartInterval</key>
<integer>21600</integer>
Resource: https://www.maketecheasier.com/use-launchd-run-scripts-on-schedule-macos/
Directory to run from
To specify which directory to run the job from:
<key>WorkingDirectory</key>
<string>/Users/yourname/demo</string>
Resource: https://medium.com/@chetcorcos/a-simple-launchd-tutorial-9fecfcf2dbb3
Debugging
Add these lines to your .plist
file:
<!-- Log STDERR -->
<key>StandardErrorPath</key>
<string>/Users/yourusername/Library/Logs/come.yourusername.job-name-err.log</string>
<!-- Log STDOUT -->
<key>StandardOutPath</key>
<string>/Users/yourusername/Library/Logs/com.yourusername.job-name.log</string>
<!-- Run when the jobs is loaded -->
<key>RunAtLoad</key>
<true/>
Run this command to view the logs:
tail -f /var/log/system.log
Then run this to load the job:
launchctl load -w ~/Library/LaunchAgents/com.yourusername.job-name.plist
and this to unload the job once you’re done:
launchctl unload ~/Library/LaunchAgents/com.yourusername.job-name.plist
Resources: https://stackoverflow.com/questions/6337513/how-can-i-debug-a-launchd-script-that-doesnt-run-on-startup https://stackoverflow.com/questions/1370901/very-simple-launchd-plist-not-running-my-script
Avoid escaping characters
You may have characters in your plist that require you to escape them. For example:
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/bash</string>
<string>runBgScript.sh&</string>
</array>
The &
is problematic here. Instead of playing the escape all
the things game, you can just do this instead:
<key>ProgramArguments</key>
<array>
<string>/usr/local/bin/bash</string>
<string><![CDATA[runBgScript.sh&]]></string>
</array>
Resource: https://stackoverflow.com/questions/3757817/plist-contains-the-character
Run Software Update via CLI
# Update all
softwareupdate -i -a
Resource: https://www.jamf.com/jamf-nation/discussions/30828/softwareupdate-l-no-updates-found
Store SSH Key in Keychain
ssh-add -K ~/.ssh/[your-private-key]
Get CPU thread count
sysctl -a | grep cpu.thread_count | egrep -o '(\d+)'
Show reason for reboot
log show --predicate 'eventMessage contains "Previous shutdown cause"' --last 24h
Kill all chrome processes
killall 'Google Chrome'
Resource: https://apple.stackexchange.com/questions/158684/chrome-wont-quit-mac-yosemite
Capture output of running process
This would call for something like strace in linux,
which is not available in OSX. Instead, we use dtrace
:
capture() {
sudo dtrace -p "$1" -qn '
syscall::write*:entry
/pid == $target && arg0 == 1/ {
printf("%s", copyinstr(arg1, arg2));
}
'
}
Get SHA-256 hash of an application
shasum -a 256 <path to binary>
# For example, to get the SHA-256 of the Chromium binary:
shasum -a 256 /Applications/Chromium.app/Contents/MacOS/Chromium
Get ipv6 address
ifconfig en0
The value next to inet6
is your ipv6 address.
You can also:
- Click the Apple menu and select System Preferences
- Click Network
- Choose your connection type (Built-in Ethernet or AirPort) and click the Advanced button.
- Select the TCP⁄IP tab to display the IP address information
Resource: https://it.umn.edu/services-technologies/how-tos/ipv6-setup-guides-mac-os-x
Open all pdfs in CWD with default PDF app
for file in ./*.pdf; do open "${file}"; done
Resource: https://apple.stackexchange.com/questions/316008/open-several-pdfs-from-terminal
Screen sharing via finder
- Open Finder
- Ctrl-k
- vnc://target_ip
Resource: http://hints.macworld.com/article.php?story=20100927085636535
Fix permission denied for creating cronjobs
- Give Full Disk Access to iterm (if you’re using it) or terminal via these instructions
- Relaunch the terminal and run this command to test if the issue has
been resolved:
ls ~/Library/Messages
Resources: https://serverfault.com/questions/954586/osx-mojave-crontab-tmp-tmp-x-operation-not-permitted http://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/
You may need to do this bit of horribleness as well (obviously not ideal from a security standpoint): https://blog.bejarano.io/fixing-cron-jobs-in-mojave.html
Brew Notes
Install specific version of package
You can install a specific version of a package using @
.
For example, to install golang 1.12:
brew install go@1.12
Get path to brew packages
brew --prefix
Switch to another version of a package
brew install terraform@0.12
cp -rf /usr/local/Cellar/terraform@0.12/0.12.29 usr/local/Cellar/terraform/.
brew switch terraform 0.12.29
Update script
brew update; brew upgrade; brew cleanup
Fix permissions
Here’s a one-liner:
sudo chown -R $(whoami) /usr/local/bin /usr/local/lib /usr/local/sbin && \
chmod u+w /usr/local/bin /usr/local/lib /usr/local/sbin
Brewfile
Generate Brewfile with existing packages
brew bundle dump
Resource: https://www.caseyliss.com/2019/10/8/brew-bundle
Fix Alt key in iterm
This is a great tutorial that covers the process in depth: https://dev.to/clairecodes/making-the-alt-key-work-in-iterm2-1aa9
Strike-through keyboard shortcut not working in Google Docs
This happens when Chrome Extensions claim the shortcut. Navigate to chrome://extensions/shortcuts and find which one is doing it. Once you’ve identified it, change that shortcut and the issue should be resolved.
Access sites with NET::ERR_CERT_INVALID in Chrome
Simply type “thisisunsafe”
Change hostname of system
NEW_HOSTNAME=myhostname
# Change FQDN:
sudo scutil --set HostName "${NEW_HOSTNAME}.domain.com"
# Change Bonjour hostname:
sudo scutil --set LocalHostName "${NEW_HOSTNAME}.local"
# Change the computer name:
sudo scutil --set ComputerName "${NEW_HOSTNAME}"
# Flush DNS cache:
dscacheutil -flushcache
# Reboot the system for the changes to take effect:
sudo reboot
Resource: https://apple.stackexchange.com/questions/287760/set-the-hostname-computer-name-for-macos
Use touch id for sudo
Add the following line to the top of
sudo vi /etc/pam.d/sudo
# Add this line to the top and save:
auth sufficient pam_tid.so
Resource: https://sixcolors.com/post/2020/11/quick-tip-enable-touch-id-for-sudo/
Remove past link from chrome address bar
Open chrome and type the URL you want to remove
Find links on the suggestions list that are associated with the site you want to remove
Type the following to remove the link from the suggested list:
SHIFT+FN+DELETE
Resource: https://osxdaily.com/2021/10/22/how-delete-chrome-url-links-address-bar/
List login items
osascript -e 'tell application "System Events" to get login items'
Alternatively, you can get the full path to all login items with:
osascript -e 'tell application "System Events" to get path of every login item'
Revert to cgroup v1 on macOS
# Stop running Docker
test -z "$(docker ps -q 2>/dev/null)" && osascript -e 'quit app "Docker"'
# Install jq and moreutils so we can merge into the existing json file
brew install jq moreutils
# Add the needed cgroup config to docker settings.json
echo '{"deprecatedCgroupv1": true}' | \
jq -s '.[0] * .[1]' ~/Library/Group\ Containers/group.com.docker/settings.json - | \
sponge ~/Library/Group\ Containers/group.com.docker/settings.json
# Restart docker desktop
open --background -a Docker
Resource: https://github.com/geerlingguy/docker-ubuntu2004-ansible/issues/18
Fix Library Not Loaded (/usr/local/lib/libintl.8.dylib) error
Remove all installed packages:
brew list -1 | xargs brew rm --force
Resource: https://apple.stackexchange.com/questions/334349/some-homebrew-formulas-broken-after-migration