How do I lock the desktop screen via command line?
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).
Related videos on Youtube
Comments
-
Renjith G over 1 year
How can I lock my desktop screen from the command line?
-
Wilf over 9 yearsFor newer versions of Ubuntu, you can try: askubuntu.com/questions/457204/…
-
-
Renjith G over 13 yearscan you clarify me the DISPALY=:0 option?
-
It Grunt over 13 yearsSorry 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 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 over 10 years
cinnamon-screensaver-command -l
for cinnamon! ;)) -
domih over 8 yearsThis works perfect, thx! Now I can add a keyboard shortcut to fix my broken lock screen command.
-
solsTiCe over 5 yearsyes. very good answer. one command to rule them all ! :-)
-
StaNov over 4 yearsThis is the answer you are looking for in 2019.
-
Thomas W. over 3 yearsThis is the solution when using lightdm.
-
xliiv almost 3 years
xdg-screensaver lock
works for me in Mate Desktop on FreeBSD -
pbhj over 2 years@xliiv, same command works for
sddm
in Kubuntu too.