Lock the Mac Desktop from the Command Line

Mar 30, 2012 - 5 Comments

Lock the Mac OS X Screen 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.

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.

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.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

Related articles:

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

5 Comments

» Comments RSS Feed

  1. Jared says:

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

  2. 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!

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

  4. 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 :(.

  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.

Leave a Reply

 

Shop for Apple & Mac Deals on Amazon.com

Subscribe to OSXDaily

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