Lock the Mac Desktop from the Command Line

Mar 30, 2012 - 10 Comments

Terminal in OS X 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.

Lock the Mac OS X Screen from the Command Line

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.

.

Related articles:

Posted by: William Pearson in Command Line, Mac OS, Tips & Tricks

10 Comments

» Comments RSS Feed

  1. Steven Klein says:

    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:

    pmset displaysleepnow

    • codes says:

      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.

  2. Steven Klein says:

    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.

  3. Amos says:

    Great tip.

    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.

  4. Not for 10.6.8 says:

    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.)

  5. Sad User says:

    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.

  6. Peter says:

    “..it can be included within scripts or entered from SSH to remotely lock a Mac.”

    Have somebody tried this out?
    It does’t work :(.

  7. Hans says:

    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).

  8. Don Cross says:

    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!

  9. Jared says:

    would be nice to have a script to do this from the iPhone or iOS

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site