How do I lock the desktop screen via command line?

108,255

Solution 1

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:

gnome-screensaver

Solution 2

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.

Solution 3

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.

Solution 4

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

Solution 5

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

Share:
108,255

Related videos on Youtube

Renjith G
Author by

Renjith G

Working as a system programmer

Updated on September 17, 2022

Comments

  • Renjith G
    Renjith G over 1 year

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

  • Renjith G
    Renjith G over 13 years
    can you clarify me the DISPALY=:0 option?
  • It Grunt
    It Grunt over 13 years
    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.
  • nanofarad
    nanofarad about 12 years
    @RenjithG this is quite clear if you think about it, also please do not be rude to the author of the previous comment.
  • Admin
    Admin over 10 years
    cinnamon-screensaver-command -l for cinnamon! ;))
  • domih
    domih over 8 years
    This works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
  • solsTiCe
    solsTiCe over 5 years
    yes. very good answer. one command to rule them all ! :-)
  • StaNov
    StaNov over 4 years
    This is the answer you are looking for in 2019.
  • Thomas W.
    Thomas W. over 3 years
    This is the solution when using lightdm.
  • xliiv
    xliiv almost 3 years
    xdg-screensaver lock works for me in Mate Desktop on FreeBSD
  • pbhj
    pbhj over 2 years
    @xliiv, same command works for sddm in Kubuntu too.