One of the first rules of cybersecurity is to always lock your computer before you walk away from it. While it may not be the quickest way, you can lock your Apple Mac using Terminal. Here's how you can do it.

Require a password to unlock your Mac
The command to lock your Mac using Terminal doesn't actually lock it. Instead, it just puts it to sleep. To be considered a "lock," a password must be required to access the Mac.
To password protect your Mac after it goes to sleep, click the Apple icon in the upper- left corner of the menu , then choose “System Preferences.”

Here, click on “Security & Privacy”.

On the “General” tab, select the checkbox next to “Require Password”.

Enter your password, then click the arrows next to “Require Password” to open the drop-down menu. You can then choose how long it takes before a password is required. Choose “Immediately” to lock your Mac immediately.

With this enabled, your Mac will lock immediately every time it goes to sleep.
Lock your Mac using Terminal
To lock your Mac via Terminal, you first need to boot up. Press Command+Space to open Spotlight Search. Type “Terminal” in the search bar, then click it when it appears in the search results.

In Terminal, type the following command, and then press Enter:


Your Mac will now be in sleep. If you set the option to require a password to wake it, then it is locked properly.
Information source: howtogeek.com
