How to lock your Mac from the Terminal

There are several ways to lock a Mac screen from the command line, and they are popular with anyone who wants to bind locking to a script, a launcher, or a programmable key. This guide covers the commands that actually work on current macOS, the widely-copied one that no longer does, how to wire any of them to a single keystroke, and the honest limitation they all share: a command only locks your Mac on the occasions you remember to run it.

The short answer

On current macOS the most reliable command-line ways to lock the screen are pmset displaysleepnow (which sleeps the display, locking the Mac if you require a password immediately), a Shortcuts action containing Lock Screen run with shortcuts run, or an osascript call that presses the built-in Control plus Command plus Q lock shortcut. The old CGSession -suspend command that many older answers still recommend has been removed and no longer exists on current macOS. Whichever method you choose, the screen only actually locks if "Require password after sleep or screen saver: immediately" is set in System Settings.

Method 1: pmset displaysleepnow

The simplest command needs nothing installed and no special permission:

pmset displaysleepnow

It puts the display to sleep immediately and does not require sudo. If your password requirement is set to immediately (covered at the end of this guide), the Mac is locked the instant the display sleeps. This is the same display-sleep primitive Peeku falls back on internally. The one thing to know is that it darkens the screen rather than showing the lock screen straight away: the Mac is still locked, you simply see a dark display until it is woken.

Method 2: a Shortcuts action, the cleanest modern way

The macOS Shortcuts app includes a first-party Lock Screen action, and the Shortcuts command-line tool can run it. Because it uses Apple's own action and triggers the real lock screen rather than display sleep, this tends to be the most predictable method across macOS versions.

Create the shortcut, then run it

  1. Open the Shortcuts app and create a new shortcut.
  2. Add the Lock Screen action (search for "Lock Screen" in the action list).
  3. Name the shortcut, for example "Lock Screen".
  4. From the Terminal, run it with shortcuts run "Lock Screen".

Tip: you can confirm the exact name to use with shortcuts list, which prints every shortcut available to the command-line tool.

Method 3: osascript pressing the lock shortcut

You can also have the system press the native lock shortcut for you:

osascript -e 'tell application "System Events" to keystroke "q" using {command down, control down}'

This sends Control plus Command plus Q, the built-in Lock Screen shortcut. Because it sends synthetic keystrokes through System Events, the app you run it from (Terminal, iTerm, or whatever calls it) needs Accessibility permission, which you grant in System Settings, then Privacy and Security, then Accessibility. If the command appears to do nothing, that missing permission is almost always the reason.

The command that no longer works

For years the most-copied answer was a path into a system menu bundle:

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

On current macOS that file no longer exists, so the command simply fails. If you have pasted it from an old forum post and nothing happens, that is expected rather than a fault on your machine. Use one of the three methods above instead.

Bind it to one keystroke

The point of a command-line lock is usually to make locking effortless, so wire it to something you will actually reach for:

Any of these turns locking into a single action, which is a genuine improvement over reaching for a menu. The guide on every way to lock your Mac screen covers the keyboard shortcut, hot corners, and menu methods if you would rather not touch the Terminal at all.

The catch every command shares

A command-line lock is fast, but it is still manual. It only protects your Mac on the occasions you remember to run it, and the moments you most need it, stepping away mid-thought for a call or a coffee, are exactly the moments you are most likely to forget. This is the same gap behind a Mac that locks too aggressively while you are still reading or one that never locks when you leave: a fixed command, like a fixed timer, cannot tell whether you are actually at the desk. A keep-awake utility such as caffeinate running in the background makes this worse, because it can stop the timer-based lock from ever firing. If you are running caffeinate on purpose during a long job, you can still lock the screen without putting the Mac to sleep.

Locking automatically, with the same native primitives

Presence-based locking keeps the speed of a command but removes the need to remember it. This is the approach Peeku uses to lock the moment you walk away. It locks within seconds of you genuinely leaving, using three signals that all stay on your Mac:

Under the hood it reaches for the same native macOS lock primitives a script would, the screen saver engine and display sleep among them, but it triggers them based on whether you are present rather than waiting for a keystroke. Two honest caveats carry over from the commands above. First, a lock is only secure if you set "Require password after sleep or screen saver: immediately" in System Settings, so set that first whichever method you use. Second, the full locking app ships free outside the Mac App Store, on Gumroad and as a notarised DMG, because the App Store sandbox blocks these same screen-lock calls. The two-minute setup guide and the overview of privacy-first, on-device locking cover the rest.

Quick reference

Skip the command and let your Mac lock itself

A terminal command is fast, but it still waits on your memory. Peeku locks the screen the moment you step away, using on-device presence detection. Free, no account, no cloud face data, and all detection stays on your Mac.

Get Peeku on Gumroad (free) See how Peeku works

macOS 14+, Apple Silicon. About 3.1 MB. Developer ID signed and notarised. Pay what you want on Gumroad (₹0 option available).

Free Mac App Store build

Prefer the App Store path?

Install Peeku Presence Coach for eye-rest nudges, walk reminders, optional Desk Health, and private local presence signals. The full walk-away lock still needs the direct Mac build because the App Store sandbox blocks automatic screen-lock commands.