Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How can I lock my desktop screen from the command line?

share|improve this question
For newer versions of Ubuntu, you can try:… – Wilf Jan 2 '15 at 18:31
up vote 56 down vote accepted

You can lock the computer by running gnome-screensaver-command with the -l flag like so:

gnome-screensaver-command -l

However this will only work if you have Gnome Screensaver running for your session (should be - unless you've disabled it) you can start that from the commandline with:

share|improve this answer
cinnamon-screensaver-command -l for cinnamon! ;)) – user128334 Jan 5 '14 at 17:28
Likewise, use mate-screensaver-command -l for MATE desktop. – alanaktion Jun 13 at 19:41
gnome-screensaver-command -l

If you're in a different (desktop) session (e.g. virtual console, switched to another login, SSH), specify the display explicitly (:0 is the default display):

DISPLAY=:0 gnome-screensaver-command -l

To unlock, use the -d (--deactivate) option.

share|improve this answer
can you clarify me the DISPALY=:0 option? – Renjith G Oct 16 '10 at 19:29
Sorry for my English... DISPLAY is a variable and define in which display an X Window System program renders to (it can be another monitor). In TTY this variable is empty. By default, first display is ":0.0". So use DISPLAY=:x before your command, indicates in which display run the command. And use DISPLAY=:0 or DISPLAY=:0.0 indicates: run the program in the first display. – Snip Oct 16 '10 at 20:46
@RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment. – hexafraction May 2 '12 at 18:02
This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command. – domih Feb 8 at 12:57

gnome-screensaver-command --lock will do it.

share|improve this answer

Under KDE dm-tool lock will work (for me on Kubuntu 15.04).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.