I'm trying to find out whether there is any kind of equivalent command line one-liner which replicates the exact identical functionality of the 'Lock Screen' menu option available via the Keychain app in OS X 10.6

I'm aware of the all the 'usual' solutions (sleep/screensaver preferences to require a password on reactivation) but these are not suitable to me.

I'm also aware of this option:

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

which is also not suitable for me.

The perfect functionality is the 'Lock Screen' option however I have not yet found an elegant way to call it.

At the moment, I am calling via Automator, which isn't elegant!

I have read much about this issue and my understanding is that no-one has yet found the 'holy grail' but I was wondering whether that might have changed.

  • What do you call with Automator ? Commented Jan 26, 2013 at 13:37
  • 3
    What is wrong with setting the Security preferences to require a password when the screen saver is active and then using a hot corner to activate the screen saver? That is fast and elegant, but you said you didn't like that solution.
    – Ɱark Ƭ
    Commented Jan 26, 2013 at 13:57
  • 3
    It's a pragmatic suggestion and one I've tried on and off over many years of usage but the nature of my work patterns means that this gets in the way >80% of the time. I need the ability to respond (physically) to engineering requests and emergencies and immediately lock the screen but equally have remote access to the box whilst it also maintains all of its existing connections and background tasks.
    – user40433
    Commented Jan 26, 2013 at 15:57
  • 1
    Re: "where is the Lock Screen menu in Keychain" Open Keychain... preferences... show in menu bar! Re: "what does it do?" It locks the screen! Beyond that, I think I have explained my requirements (and my aversion to 'solutions' that some folk believe meet my requirements, but don't) quite thoroughly.
    – user40433
    Commented Jan 30, 2013 at 13:35
  • 1
    See also stackoverflow.com/questions/1976520/…
    – rogerdpack
    Commented Dec 28, 2017 at 19:20

20 Answers 20


In OS X 10.9 and later:

pmset displaysleepnow

By itself, this command only causes the display to sleep, resulting in a black screen. By configuring your computer to require a password immediately after sleep, this one-liner works as a "lock computer" command. The preference is available at System Preferences > Security & Privacy > General.

Source: How to lock screen on MacBook Air?

  • 13
    just for completeness, this works only if "Rerquire password after sleep or screen saver begin" security option is set turned on in system preferences
    – Hofi
    Commented Sep 17, 2015 at 6:02
  • 1
    This seems most equivalent to CTRL+SHIFT+Eject. Commented Jul 6, 2017 at 16:19
  • This is not equivalent to the Keychain menu bar item. It does not lock the screen; it only puts the computer to sleep. Commented Aug 21, 2018 at 2:10
  • 1
    @RadonRosborough No, it doesn't put the computer to sleep. It does put the display to sleep, If you access the locked Mac remotely everything is working. This might be the shortest and fastest answer since it doesn't go through all the Keychain slow GUI or the Finder slow GUI.
    – dan
    Commented Jan 5, 2019 at 12:24
  • This is a much better solution than the old and ill placed Keychain menu entry 👏🏻.
    – dan
    Commented Jan 5, 2019 at 12:28

Note: This solution is unique in that it uses the Keychain Access menu bar status functionality, but it does not require you to enable the Show keychain status in menu bar option as the AppleScript methods do.

I was also looking for a solution for this. Today I just had some time to play around and found a way to programmatically actually call the functionality from the keychain menu plugin. This solution works perfectly as long as Apple doesn't change the relevant parts in the keychain menu plugin. You can create a small binary for locking your screen by pasting this into your terminal...

# Do our work in the temporary directory that gets cleaned on boot
cd /tmp

# Create the source file
cat > main.m <<END_OF_FILE

#import <objc/runtime.h>
#import <Foundation/Foundation.h>

int main () {
    NSBundle *bundle = [NSBundle bundleWithPath:@"/Applications/Utilities/Keychain Access.app/Contents/Resources/Keychain.menu"];

    Class principalClass = [bundle principalClass];

    id instance = [[principalClass alloc] init];

    [instance performSelector:@selector(_lockScreenMenuHit:) withObject:nil];

    return 0;


# Compile the source file
clang -framework Foundation main.m -o lockscreen

Execute the program by typing:

  • Could you elaborate on how to use this? Do I copy-paste this into an Automator workflow? Or make an Applescript out of it? As it stands now it's hard to do something with your answer. Commented Mar 10, 2014 at 1:09
  • 1
    This is a simple Objective-C Program. Copy it into a file called main.m. Then run 'clang -framework Foundation main.m -o lockscreen' to compile the program. './lockscreen' runs the program and locks your screen. Commented Mar 10, 2014 at 1:19
  • Tried it, works as described. The compilation generates one warning instance method '-_lockScreenMenuHit:' not found, but the lockscreen application works nonetheless. However, when doubleclicked it opens a Terminal window before it locks the screen. After unlocking you have to close the window manually. Is there a way to close that window automatically? Commented Mar 10, 2014 at 10:11
  • 6
    I collected an OS X App: Lock Screen.app based on this answer and somebody's work as detailed in the link, it simply wraps this code and add an beautiful icon, so we can run it directly, thank you.
    – Mengdi Gao
    Commented Dec 7, 2014 at 17:21
  • 1
    This doesn't work aymore on 10.13 because the MenuExtra has been removed. This works instead: stackoverflow.com/a/26492632/368409
    – Lloeki
    Commented Jun 30, 2017 at 9:07

UPDATE: 2019.

The Lock Screen for keychain no longer is available in current version of the MacOS Mojave & High Sierra.

Apple have now add a Lock Screen Menu item in the Apple menu. This has the default keyboard short cut of crtl + cmd + Q

enter image description here

------------- Old answer for previous OS below --------------

@Bart Arondson answer goes into using some key codes to navigate the Keychain Lock Screen menu.

All the key codes can be bypassed using GUI Scripting to Actually hit the menu items directly.

Using key codes to navigate menus can be hit and miss. And as much as I do not like GUI scripting because it can also be hit and miss imho it is less hit and miss in this case.

This applescript code: (updated 05/18/2016, sourced from Using AppleScript to lock screen, confirmed working in El Capitan)

tell application "System Events" to tell process "SystemUIServer"
    tell (menu bar item 1 of menu bar 1 where description is "Keychain menu extra")
        click menu item "Lock Screen" of menu 1
    end tell
end tell

Can be used in a Automator 'Run Applescript' Action in a Automator Service Workflow.

enter image description here

You then give the service a Keyboard shortcut. In the usual way in The Keyboard Shortcuts.

enter image description here

The Service:

enter image description here

  • This worked great for what I needed it for, thank you!
    – nictrix
    Commented Sep 24, 2014 at 3:12
  • you can also run a terminal command from automator, so I used that. it seamed more "elegant" :). very nice solution though! Commented Feb 26, 2015 at 23:56
  • 3
    I used this solution for a long time, but since osX El Captain it doesn't work anymore :(. Commented Oct 2, 2015 at 6:55
  • Still looking at this. But it looks like the menu does not report back on itself for the next item to be clicked. Running these two lines works but is slow. tell application "System Events" to tell process "SystemUIServer" to click (first menu bar item of menu bar 1 whose description is "Keychain menu extra") tell application "System Events" to click (menu item "Lock Screen" of menu 1 of menu bar item 1 of menu bar 1 of application process "SystemUIServer")
    – markhunte
    Commented Oct 2, 2015 at 10:58

I use Alfred →

enter image description here

The universal hotkey allows me to activate it using a keyboard.

  • Awesome! Is the universal hotkey built-in or part of the powerpack? Commented Nov 19, 2014 at 15:50
  • 1
    It's built-in ... Commented Nov 29, 2014 at 21:39
  • 2
    Sadly this is not it - it also shuts down internet connection, while using Keychain menu lock doesn't. Commented Mar 27, 2015 at 9:01
  • There's an equivalent in Launchbar too: "Lock Screen" Commented May 22, 2017 at 20:45
  • This is not equivalent to the Keychain menu item. It does not put the computer to sleep, and it also plays an annoying animation. Commented Aug 21, 2018 at 2:09

Easy alias. Add this to your bashrc

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

type afk in your terminal to execute.

  • This worked for me too
    – codematix
    Commented Jan 26, 2022 at 16:11

Set the preferences to lock the screen when the screen saver is active.

Security & Privacy Preference Pane

Then you can use this terminal command to start the screen saver.


As far as I can tell this is exactly what the Keychain menu item does.

  • Thanks you for your comment but this is what I've battled with for too long and am trying to find an alternative to.
    – user40433
    Commented Jan 26, 2013 at 16:01
  • 1
    Just for clarity and completeness, I should that the above does indeed start the screensaver but it does not do what the 'Lock Screen' menu function offers.
    – user40433
    Commented Jan 26, 2013 at 16:06
  • 1
    Then you need to be more specific as to what exactly you are looking for.
    – Ɱark Ƭ
    Commented Jan 26, 2013 at 16:17
  • Hmmm... not sure I can be any more specific or succinct then the opening paragraph in my original question!
    – user40433
    Commented Jan 26, 2013 at 16:23
  • 1
    I had a chance to test on 10.6.8. On that OS the monitor is turned of and the session is locked. I found a programatic way to do this, but it is not built in.
    – Ɱark Ƭ
    Commented Jan 29, 2013 at 14:05

I think the answer to your question is "No, there isn't a command line way to do this."

Because this is a menu bar item, it's not something that you can access easily using Keyboard Maestro or another similar tool.

However, if you are not averse to a solution using a 3rd party app, QuickLock will let you do this.

It's a free (donations accepted) app which will let you assign a keyboard shortcut to lock the screen. There's also a menu bar item which you can click to lock the screen.

The app will let you set a password (separate from your account password).

It does not require the use of password with screensaver, it's all completely separate.

You can see a video of it in action at http://www.youtube.com/watch?v=SBHwykPB19o

p.s. when the screen is locked, type your password to unlock it. I was confused because I expected to see a password field. There is none. You just type the password.

p.p.s. I've only used this for a few minutes, so there may be some other issues with it I'm not aware of, but it seems to fit the bill.

  • Thanks for your contribution... although not what I set out looking for (I love open source and DIY methods!) it does look very interesting and appears to offer the functionality that I need, albeit in a 3rd party app. I'll look into it more closely. Thanks!
    – user40433
    Commented Jan 29, 2013 at 22:30
  • I'd love to find a way to do it too, but I don't think there is one.
    – TJ Luoma
    Commented Jan 30, 2013 at 15:07
  • appears dead...
    – rogerdpack
    Commented Dec 28, 2017 at 19:19

The Mac is not designed to provide you with such an option. However, this article explains a workaround using QuickSilver. While I can't understand why you don't like the solution Apple provides natively, I do wish you luck on finding a solution.


A quick way to sleep your machine via command line with the help of AppleScript is

osascript -e 'tell application "Finder" to sleep'

That doesn't lock your screen unless you have it set to require a password immediately which I don't like to do. I'm just noting it for posterity.


I used the code posted above by jnk. Then I used appify to turn it into an OS X app, mathiasbynens.be/notes/shell-script-mac-apps. Then I used Quicksilver to assign my own keyboard shortcut (I chose cmd+L). Terminal is not opening/staying open (as some were concerned) with how I have it set up because I am launching an OS X app instead.

  1. Follow the answer above by jnk, Lock Screen Command One-Liner
  2. Use appify to turn it into an OS X app by following the link above in my summary.
  3. Install Quicksilver.
  4. Use Quicksilver to assign a keyboard shortcut to launch the new application. It is under 'Triggers.' I enjoy Quicksilver because I love having keyboard shortcuts/hotkeys to launch applications and websites on OS X.

I also ran into this problem. /System/Library/CoreServices/Menu\ Extras/user.menu/Contents/Resources/CGSession -suspend causes the system to suspend all tasks, rather than lock the screen.

JordanBtucker was on the right track, here is the full function I use:

lockscr() {
  if [ "$(defaults read com.apple.screensaver askForPasswordDelay 2>/dev/null)" ]; then
    local Olddelay="$(defaults read com.apple.screensaver askForPasswordDelay)"
    defaults write com.apple.screensaver askForPasswordDelay -int 0 && \
        pmset displaysleepnow
    defaults write com.apple.screensaver askForPasswordDelay -int "${Olddelay}"
    defaults write com.apple.screensaver askForPasswordDelay -int 0 && \
        pmset displaysleepnow
    defaults delete com.apple.screensaver askForPasswordDelay

This preserves the user his own settings but securely locks the screen :)

  • In High Sierra these values are not set by the UI any longer. However, if you set them manually the UI will honor them. defaults write com.apple.screensaver askForPassword -bool TRUE and defaults write com.apple.screensaver askForPasswordDelay -int 10 These are my default values. Now the above script will behave as described.
    – Sean Perry
    Commented Mar 12, 2018 at 21:12

On 10.14.6 Mojave, I start the screensaver engine using this command instead:


Edit: Make sure you set the preferences to lock the screen when the screen saver is active in system preferences and to require the password immediately.

  • But it didnt lock. I hit a key and it opened back. Mind if you read other answers' comments about " require password immediately after.." in system preferences and update yours accordingly?
    – anki
    Commented Sep 27, 2019 at 7:08

I use BetterTouchTool for that purpose and assigned a keyboard shortcut to "Switch to login screen" (which is lock screen): doesn't fulfill the command line requirement but may be helpful to you anyway.

keyboard shortcut in BetterTouchTool to lock screen

  • Thanks for your help and suggestion. Along with QuickLock mentioned above by @TJLuoma BetterTouchTool (although not the method I set out to find) does appear to come very close to my functional objectives. I will look more closely at this... thanks.
    – user40433
    Commented Jan 31, 2013 at 20:48

I'm not quite sure I understand your issue here. If you require a password on screensaver, it will not stop processes from running or block you from remotely accessing your Mac. I have this option set (using ControlPlane to activate it at work and deactivate it at home) and I have never once had the system refuse to respond to a Remote Desktop, VNC, or SSH connection. In all cases where GUI access is required I just need to input the password; SSH uses key pair so no password is requested. If you don't want apps to sleep, then make sure the system power settings are such that it will not enter system sleep before you return. Just set the sleep timeout to a long delay. If you just don't want to be bothered to enter a password when you are sitting in front of the computer and working, then increase the screensaver delay to a longer time before it activates, or add a 1 min delay between screensaver and password prompt so that you have a chance to catch it before it locks you out.

I use ctrl-shift-eject to lock the screen. It's fast, simple, and it works. This is the built-in way to lock the screen. No one is likely to come up with an alternative simply because replicating the functions already baked in to the OS is a little too redundant for anyone to put a lot of time into pursuing.

As far as I can tell there is no "lock screen" option in Keychain in 10.8 anyway, so your preferred solution is not going to exist if you ever upgrade. Edit: Didn't notice this in Keychain preferences; yes, it is still there in 10.8.

If you are using Automator for this can't you just package the script as an app and assign a hotkey combo or leave it in the Dock for quick access?

  • The ctrl-shift-eject really just enters "fast user switch" mode. At least one VPN solution (the one that I need to use for work purposes) doesn't like this, and disconnects the VPN when I do that, terminating any underlying sessions that I may have connected to. While I can simply re-connect to my VPN session when I unlock the computer, it can be a minute+ long experience, and thus fails the "quick" option. Therefore, this is not, IMO, an accurate solution to the question.
    – Jon V
    Commented Feb 8, 2016 at 16:25

Just like the question asker I was looking for a way to use the Keychains Lock Screen in a fast way.

Combining the knowledge from an old Mac OS X hint and some Applescripting I came up with the following:

First follow the steps from the linked Mac OS X hint to add a keyboard shortcut for Keychains Lock Screen functionality.

For me (on 10.8.4) this didn't solve the problem as I had to press ctrl+F8 to shift focus to the status bar, press down to open the Keychain menu bar, and then press ctrl+F9 to lock the screen. I've automated this process with the following Applescript:

# Press ^F8
tell application "System Events"
    key code 100 using control down
end tell

# Press "down"
tell application "System Events"
    key code 125
end tell

# Press ^F9
tell application "System Events"
    key code 101 using control down
end tell

Open Automator, create a new Service, add a Run Applescript module, copy-paste the above code and save the Automator Service with a name you desire. Now go to Keyboard Shortcuts in the System Preferences and add a keyboard shortcut for the freshly created Automator Service. I set mine to ctrl+cmd+L.

Now press the keyboard shortcut in any app and your screen will be locked instantly.

For those wondering why not just go with other methods, here's my opinion on them:

  • Fast user switching/lock screen
    With this method I get logged out of my corporate WPA2 network. This is not good as re-connecting sometimes takes ages.

  • Screensaver + password
    Replicates the functionality but this means that if my laptop is inactive for some time while I'm reading a paper next to it, the screen will be locked when I want to use it again. This is undesirable. Coupling this method with a hot-corner has the side effect that unexperienced Mac users might trigger the hot-corner by accident leading to screen lock. I also sometimes hit a hot-corner by accident which would stall my workflow considerably.

  • 3rd party apps
    These are all fine, but why run a 3rd part app if it's natively available in the OS?


As of 2021, I find the following solution to be the "closest to correct":

As pointed out in markhunte's answer, the Apple menu has an entry to initiate "Lock Screen" with the keyboard shortcut: controlcommandQ

So I simulate pressing this keyboard shortcut:

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


  • Will work in the future, as long as this keyboard shortcut is supported
  • Also works if "Require password after sleep or screen saver begins" security option is disabled (in contrast to the solutions which utilize sleep or display turn-off)
  • Display stays on (at least initially; it seems that the lock screen itself triggers a display turn-off shortly after being initiated)

Edit: I just saw that Aritro Paul's answer already contains that command (in form of an alias). I hope my elaboration is helpful though.


I use the Screensaver password setting with a keystroke (Ctrl+Shift+Eject) on 10.6.8, and from what I can see, it does the same as the Lock Screen item from the Keychain Access task bar icon.

I know that this is similar to some of the other answers, but instead of the "hot corner", it uses a keystroke, which is more convenient for me.

This solution is described in more detail here, that's where I got it from: http://osxdaily.com/2011/01/17/lock-screen-mac/

Have been using this for several years now without problems.

  • 1
    Thanks but, unfortunately this requires that the option to "require screensaver when returning from sleep or screensaver" option sis checked and, as mentioned earlier, this is not something I wish to have engaged.
    – user40433
    Commented Jan 30, 2013 at 13:32

Well, just combine two of the above answers. Run this command:

open -a /System/Library/Frameworks/ScreenSaver.framework//Versions/A/Resources/ScreenSaverEngine.app

After having set the Security & Privacy preferences to ask for a password immediately. Make an alias for your shell if you want to make the command easier to execute.

  • 3
    Yes but, as mentioned above, I don't wish to have the Security & Privacy preferences to always require a password immediately. This is why the Lock Screen command functionality (specifically) is so cool - it provides the level of security I require on a completely ad hoc basis.
    – user40433
    Commented Feb 1, 2013 at 22:12

FWIW, I understand your situation and have been looking for a good solution to this myself. I ran across this thread because I've been searching for a somewhat faster method than what I eventually came up with. I came up with something that, although not exactly what I'd hoped, works and is an acceptable compromise. I'm using the same kind of GUI scripting commands as @markhunte shows (slightly different structure, but same idea) but instead of just placing it all in an Automator service workflow, I separated it into an .app bundle built with Platypus. I did this because in my travels and testing I've found that Automator services run very slowly under certain conditions. If your Mac is under any kind of stress, i.e. a disk scan running, A/V software doing something, Mail pulling down email, etc., the OS seems to push services to the back of the line. This has the affect of it taking several seconds before the workflow runs (sometimes) and on occasion even several seconds before the GUI commands in the service get run. Quite annoying if you;re trying to "quickly" lock your screen and it takes 10 seconds to do it!

Having a standalone "app" also has the advantage that you can use literally dozens of different 3rd party apps to run it, or just use Spotlight or an Automator service to launch the app. Any of them work. The fastest I've found is an old utility called Spark, that miraculously still works even in 10.8.4. Don't ask me how since it was last developed foe 10.4! Spark can be found here - Spark

In addition to all this, I combatted the issue of GUI commands sometimes running slow by bundling in cocoaDialog and calling it first when the app runs to show a small floating bubble in the middle of the screen indicating a Lock Screen is happening. Usually the lock happens right after, but if there is a delay, this at least gives some visual "feedback" that you pressed the right keys. I also made it self healing - if "Enable Access to Assistive Devices" is not on, it alerts you with a dialog and lets you enable it right away. It will also make sure the Keychain Access Menu is showing in the menu bar since this is a requirement or the GUI scripting flat out fails.


Ctrl-Shift-⏏ Eject (on older Macs),
Ctrl-Shift-⌽ Power (on newer macs)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .