Locking your Screen with dbus from the command line in Gnome, KDE4 and others.

dbus-send --session --dest=org.freedesktop.ScreenSaver --type=method_call \
--print-reply /ScreenSaver org.freedesktop.ScreenSaver.Lock

This will work with KDE4 and should as well work with newer Gnome versions. Tested with KDE 4.1 beta1. It needs the display variable to be set and be run as the appropriate user.
e.g. su $user -c "DISPLAY=:0 dbus-send... "

In Ubuntu 8.04 you can change your /etc/acpi/resume.d/90-xscreensaver.sh to lock the screen on resume like this:


# now, we should poke xscreensaver so you get a dialog
#if pidof xscreensaver > /dev/null; then
for x in /tmp/.X11-unix/*; do
displaynum=`echo $x | sed s#/tmp/.X11-unix/X##`
if [ x"$XAUTHORITY" != x"" ]; then
export DISPLAY=":$displaynum"
su $user -c "(xscreensaver-command -deactivate)"
su $user -c "dbus-send --session --dest=org.freedesktop.ScreenSaver \
--type=method_call --print-reply /ScreenSaver \


  1. I needed this. Thanks a lot! :)

  2. I'm glad it helped. Often takes some time to figure it out, always nice to help others save time.

  3. under kde 3.5, it was
    $ dcop kdesktop KScreensaverIface lock

  4. How I can unlock the screen via dbus from screensaver without entering password? I try to setup BlueProximity working with KDE4 screensaver.
    I can lock via command:
    $ qdbus org.kde.krunner /ScreenSaver Lock
    but don't know how to unlock...

  5. Hmmm... may I ask why you would want to circumvent entering the password?

  6. I want to lock computer when my mobile phone with bluetooth go away, and unlock when I with my phone goes back to computer.

    Program BlueProximity do this, but work only with gnome screensaver:

    gnome-screensaver-command -l

    Unlock without password:
    gnome-screensaver-command -d

    And I want to do this function in KDE.

  7. Ah... ok. Well in my experience it usually works quite well to simply killall (screensaver) (kscreenlock...).
    Just look at the processes active when it's locked. Good luck! And maybe you want to post the script. It would be a nice feature to include this automatically. :)

  8. I have no script, I using the program BlueProximity with thouse settings:
    qdbus org.kde.krunner /ScreenSaver Lock

    Unlock without password:
    qdbus org.kde.krunner /ScreenSaver SetActive false

    second string I get from kbluetooth 0.4 sources.

  9. I have found a working variant!
    qdbus didn't send the values, dbus-send do this.
    Working commands:
    dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.Lock

    Unlock without password:
    dbus-send --type=method_call --dest=org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.SetActive boolean:false

  10. Here is what i am doing :)


    for x in /tmp/.X11-unix/*; do display=`echo $x | sed s#/tmp/.X11-unix/X##`; done
    for x in /tmp/kde-*; do user=`echo $x | sed s#/tmp/kde-##`; done
    export DISPLAY=":$display"
    su $user -c "qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock"


I appreciate comments. Feel free to write anything you wish. Selected comments and questions will be published.