Lock the Mac Desktop from the Command Line
With the help of a buried menu item, we can lock the Mac OS X screen right from the Terminal. This does not log a user out, it just brings up the standard Mac OS X lock screen and login window, requiring a valid user and password before the Mac can be used again.
This is very simple to use, if you find yourself locking the Mac often with this trick you may want to make an alias for easier access.
How to Lock the Mac Screen from Terminal in OS X
Open Terminal and enter the following in a single line:
/System/Library/CoreServices/Menu\ Extras/User.menu/Contents/Resources/CGSession -suspend
There isn’t any confirmation, the desktop is immediately locked and the lock screen appears regardless of what is currently going on with the active user account.
To make an alias, add something like the following to your profile:
alias lockscreen='/System/Library/CoreServices/"Menu Extras"/User.menu/Contents/Resources/CGSession -suspend'
For those wondering, the menu item being used is the same Fast User Switching menu that displays a user name in the upper right corner, and the lock screen shown is identical to what is summoned if one were to select “Login Window…” from that same menu.
You can lock a screen using a keyboard shortcut as well, but using the command line offers two obvious advantages; it can be included within scripts or entered from SSH to remotely lock a Mac.
Okay, a little more research, and I found the answer.
To bring up the real Lock Screen (not Login Window), all you need is this:
nevermind. this isn’t going to work if you have your setting to lock the display take anything other than ‘immediate’. Not what I was looking for.
The terminal command described here brings up the Login Window, which is NOT the Lock Screen.
The login window lets you select any user account and enter the password. (Or it prompts you to enter a username and password, if you have your login window set to display “Name and password”).
The Lock Screen shows a password prompt only for the user who was active when the Lock Screen was invoked. You don’t have to select a user (or type in a user name).
When I press Shift-Control-Eject to lock my screen, I only have to enter my password to unlock it. When I use CGSession command described here, I have to enter both my username and my password.
I’d be curious to know whether it’s possible to trigger the lock screen without user switch – just the screen that would come up when the Mac locks itself after a timeout. So far I didn’t find any documentation for CGSession.
The other person mentioned a system crash, but then said it worked the second time, so I thought maybe something was messed up on their system. Well, I got a literal blue screen for about 10 seconds, then the login screen, then the “you must restart this computer by holding down the power button” message.
I will not be trying it a second time.
(To be clear, I blame Apple, not the tip author. Nothing the end-user does without root access should ever cause the OS to behave like that.)
Ouch. I tried this on Snow Leopard and it crashed my laptop. It started to do that 3D cube rotate thing, stopped, and 10 seconds later I get the BSOD )or whatever the Mac equivalent is called). The second time I tried it, it worked. But I’m a little nervous using this on a regular basis.
“..it can be included within scripts or entered from SSH to remotely lock a Mac.”
Have somebody tried this out?
It does’t work :(.
This is nice to know; fyi, it didn’t work from a terminal session within x11.app. (I’m running it in full screen mode with wmaker instead of aquawm, though).
Thanks for publishing this. I made a bash script called “lock”. Now I can just type “lock” from Terminal and walk away from the machine, knowing my cat can’t walk on the keyboard and delete my files!
would be nice to have a script to do this from the iPhone or iOS