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


7 Answers 7


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:

  • 11
    cinnamon-screensaver-command -l for cinnamon! ;))
    – user128334
    Jan 5, 2014 at 17:28
  • 4
    Likewise, use mate-screensaver-command -l for MATE desktop.
    – alanaktion
    Jun 13, 2016 at 19:41

In newer versions of GNOME 3, gnome-screensaver is no more.

The generic solution seems to be

xdg-screensaver lock

You also can call dbus directly instead (source):

dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock

It also seems they have taken away the possibility to unlock the screen from the command line.

  • yes. very good answer. one command to rule them all ! :-)
    – solsTiCe
    Nov 21, 2018 at 15:43
  • 1
    This is the answer you are looking for in 2019.
    – StaNov
    Dec 20, 2019 at 8:18
  • xdg-screensaver lock works for me in Mate Desktop on FreeBSD
    – xliiv
    Jul 21, 2021 at 21:52
  • 1
    @xliiv, same command works for sddm in Kubuntu too.
    – pbhj
    Feb 6, 2022 at 0:18
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.

  • 1
    can you clarify me the DISPALY=:0 option?
    – Renjith G
    Oct 16, 2010 at 19:29
  • 1
    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, 2010 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.
    – nanofarad
    May 2, 2012 at 18:02
  • This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
    – domih
    Feb 8, 2016 at 12:57

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


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

  • This is the solution when using lightdm.
    – Thomas W.
    Oct 22, 2020 at 20:37

There are also many minimalistic utilities that really only lock, they don't do anything else. (This can be more secure as in general keeping software simple leads to less bugs.)

For example:


Here is a long way of accomplishing it (adding a quicker way below):

to make it even easier to lock, you can add an executable script to an executable path, call it "lock" and then the locking of your screen will be as easy as typing "lock" in cli

Here's how to do it:

mkdir ~/bin

vim ~/bin/lock

gnome-screensaver-command -l

save and quit

chmod +x ~/bin/lock

don't forget to add ~/bin to your ~/.profile - note the dot at the beginning of the file name one word of caution about this, make sure you add it to the right file. Here is what the .profile says about it:

# This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login
# exists.

therefore you want to first check if the above-mentioned files exist in your home directory, you should add it there, if they don't exist, then add the path to bin to the .profile

vim ~/.profile # or one of the the other files if they exist

append the following at the end:

# set PATH so it includes user's private bin directories

at this point you can launch the following shortcut from cli

you@yourUbuntu:~$ lock

[EDIT] Here is the quick and easy way to do it: add an alias to your ~/.bashrc file, which is executed every time a shell is opened, thus ensuring Alias persists:

vim ~/.bashrc
# set lockscreen
alias lock="gnome-screensaver-command -l"

the result is the same, but quicker

You must log in to answer this question.

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