To lock your Mac’s desktop screen from the command line, use the pmset displaysleepnow command, which immediately puts the display to sleep and requires your password to wake. Alternatively, /System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend locks the screen without sleeping the display. Both commands work in Terminal and can be combined with keyboard shortcuts or automation for quicker access.
Why Lock from the Command Line?
Locking your Mac via the command line is useful when you want to automate the lock, trigger it from a script, or create a custom keyboard shortcut. The standard methods—pressing Control+Command+Q or choosing “Lock Screen” from the Apple menu—are fine for manual use, but they don’t fit into scripts or remote sessions. Command-line locking is also handy for IT administrators who need to lock multiple machines or for users who want to lock their screen after a period of inactivity without changing system settings.
How the Commands Work
The two primary commands operate differently:
pmset displaysleepnowtells the power management system to sleep the display immediately. This is the same as the display sleeping naturally after a timeout. The screen turns off, and the Mac remains fully awake (unless you also set the system to sleep). When you wake the display by moving the mouse or pressing a key, the login window appears, requiring your password.CGSession -suspend(full path:/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend) directly invokes the session manager to lock the screen. The display stays on, showing the login window immediately. This is closer to pressingControl+Command+Q.
Both commands require you to be logged in as the current user; they don’t work if you’re at the login window already. They also work over SSH, which makes them useful for remote management.
Using the Commands in Terminal
To use either command, open Terminal (found in /Applications/Utilities/). Type the command and press Return. For example:
pmset displaysleepnow
or
/System/Library/CoreServices/Menu Extras/User.menu/Contents/Resources/CGSession -suspend
You don’t need sudo for either command—they work with your normal user privileges. If you’re using SSH, you may need to ensure your user has permission to control the display; usually it works if you’re logged in as the same user.
Creating a Keyboard Shortcut or Service
If you want a one-key lock, you can create a macOS Service that runs the command and then assign a keyboard shortcut. Here’s how:
- Open Automator (in /Applications/).
- Choose “New Document” and select “Quick Action” (or “Service” in older macOS versions).
- Set “Workflow receives current” to “no input” and select “any application”.
- Drag a “Run Shell Script” action into the workflow.
- In the script box, enter
pmset displaysleepnow(or the CGSession command). - Save the service with a name like “Lock Screen”.
- Go to System Settings (or System Preferences) > Keyboard > Keyboard Shortcuts > Services, find your service, and assign a shortcut like
Command+Option+L.
Now pressing that shortcut will lock your screen instantly. The service runs the command in the background, so you don’t see a Terminal window.
Common Problems and Solutions
Sometimes the commands don’t work as expected. Here are the most common issues:
- Command not found: If you get “command not found” for
pmset, you may have a PATH issue. Use the full path/usr/bin/pmsetor/bin/pmset(usually the latter). - Display doesn’t sleep: If
pmset displaysleepnowdoesn’t turn off the display, check if your Mac has a setting that prevents display sleep (e.g., “Prevent automatic sleeping when the display is off” in Energy Saver). Also, some external displays may not respond to the command. - Lock doesn’t happen: If the screen locks but immediately unlocks, check if you have “Require password after sleep” set to “immediately” in System Settings > Lock Screen. If it’s set to a longer delay, the lock will still happen, but the password prompt may appear later.
- SSH issues: When using SSH, the command may fail if the user session is not active. Ensure you’re logged in as the same user and that the Mac is awake.
Alternatives to Command Line
If you prefer not to use Terminal, there are other ways to lock your Mac quickly:
- Keyboard shortcut: Press
Control+Command+Qto lock the screen immediately. - Hot corner: Set a hot corner in System Settings > Desktop & Dock > Hot Corners to trigger “Lock Screen” when you move the mouse to a corner.
- Touch ID: If your Mac has Touch ID, you can set it to lock the screen when you place your finger on it (requires macOS Sequoia or later).
- Menu bar icon: Some third-party apps add a lock icon to the menu bar, but the built-in methods are sufficient for most users.
The command-line method is most valuable for automation and remote control, not for everyday manual locking.
Security Considerations
Locking your screen is a basic security measure, but it’s important to understand its limitations:
- The lock only prevents access to the desktop. It does not encrypt your data or protect against physical theft of the Mac.
- If you use FileVault, the disk is encrypted, so data is safe even if the Mac is stolen. Locking the screen is still recommended to prevent unauthorized use while you’re away.
- When you lock via command line, any running processes continue. This is fine for most cases, but if you’re concerned about sensitive information on screen, consider also logging out or using a screensaver with a password.
- Never share your password, and use a strong password or passphrase. The command line doesn’t bypass any security; it just triggers the same lock as the GUI.
Frequently Asked Questions
Does pmset displaysleepnow actually lock the screen?
Yes, it puts the display to sleep, and when you wake it, the login window appears. This effectively locks the screen, provided you have “Require password after screen saver begins or display is turned off” set to “immediately” in System Settings > Lock Screen. If that setting is set to a delay, you’ll have a grace period before the password is required.
Can I lock the screen from a remote SSH session?
Yes, both commands work over SSH, but you must be logged in as the same user who owns the console session. If you’re logged in as a different user, the command may fail because the session is not active. Also, ensure the Mac is awake and not in a locked state already.
Is there a difference between locking and sleeping the display?
Locking the screen shows the login window immediately while the display stays on. Sleeping the display turns off the screen, and the Mac goes into a low-power state for the display only. Both require a password to regain access, but sleeping the display saves more power. The pmset displaysleepnow command sleeps the display, which is a form of locking if password is required.
Can I use these commands in a script to lock after inactivity?
Yes, you can write a shell script that waits for a certain time and then runs the command. For example, you could use sleep 300 && pmset displaysleepnow to lock after 5 minutes. However, macOS already has built-in inactivity locking, so this is only useful for custom scenarios.
What if I get “You don’t have permission to save the file” when creating a service?
This usually happens if you’re saving the service in a system location. Save it in your user’s Services folder (usually ~/Library/Services). If that folder doesn’t exist, create it. You can also save it to iCloud Drive, but it may not appear in the Services menu.
Do these commands work on all Mac models?
Yes, they work on all Macs running macOS, including Intel and Apple Silicon models. The pmset command is part of the power management system, and CGSession is part of the Core Graphics session manager, both present in all macOS versions.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- How to Fact-Check Artificial Intelligence Claims
- Sony Bravia 3 II vs Hisense U6SF Pro: Which to Buy?
- Best Fire Emblem Games Without Monastery Grind
- AI for Playwright Locators: Draft, Verify, and Refine
- Are AI Agents Safe for Private Information?
- Best MMORPG Games: Top Picks for Every Play Style