Lock macOS with a password from bash Is there a way to lock macOS so that waking it up requires the user’s password? (Even If normally the password won’t be requested if it is waked up in, say, 1 hour)
This should work on most versions 

#!/bin/bash

PATH=/usr/local/bin:/usr/bin:/usr/sbin:/sbin:/bin

"/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession" -suspend

sleep 5

pmset sleepnow

osascript -e 'tell application "System Events" to sleep'

exit 0


The -suspend line will switch to the login screen (which takes a few seconds, hence the sleep 5 before the next command).

The pmset line will sleep the display.

The osascript line will sleep the computer.

Only the -suspend line is necessary to require the password, so you can choose to include the other lines, or not, as you wish.