Lock your KDE screen from the command line

By | January 29, 2009

Did you know you can lock your current KDE screen by using the command line? For the 3.x series use

dcop kdesktop KScreensaverIface lock

and for KDE 4.x try

qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock

With the help of these you should be able to craft a simple shell script that will lock your screen at given actions. For example, you can leave your screen unlocked and make the PC activate the commands above when it detects mouse movement or when a certain application is launched.

13 thoughts on “Lock your KDE screen from the command line

  1. BeteNoire

    The command for KDE 4 does not work.

    qdbus org.kde.krunner /ScreenSaver Lock
    Service ‘org.kde.krunner’ does not exist.

    Reply
  2. Tyler Wagner

    BeteNoire, try:

    qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock
    xset dpms force off

    I’ve put this in a script in $HOME/bin, created a shortcut in the menu, and set “Win+L” as a shortcut. Thanks, T4L!

    Reply
  3. Roy Smith

    If you wish to launch the kde4 screen saver without the password prompt to exit, use this command:

    qdbus org.freedesktop.ScreenSaver /ScreenSaver org.freedesktop.ScreenSaver.SetActive True

    Reply
  4. Jeffery Williams

    The command may not work from within “screen”. If you are running screen and it doesn’t work, try the command in a new konsole window.

    Reply
  5. Tom

    Is there a way to do this remotely. I ssh into the box and try to lock it remotely but get this error:

    Could not connect to D-Bus server: org.freedesktop.DBus.Error.Spawn.ExecFailed: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed.

    Reply
  6. Doran Barton

    Tom, you have to set the DISPLAY environment variable.

    export DISPLAY=:0
    qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock

    Reply
  7. Guiguiboy

    As the command is a bit long, you can set an alias in your .bashrc

    alias lockme=”qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock”

    Reply
  8. Tom Bell

    I use the following with BlueProximity to lock my screen when I leave.
    qdbus org.kde.screensaver /ScreenSaver SetActive 1
    It seems to work on Kubuntu 14.04 AMD64.

    Reply
  9. Landis

    Thank You.
    I use a countdown timer on my desktop which has option to display a ‘notification’, but this is easily ignored as I have KDE system notifications set to only a few seconds.

    The timer also has an option to ‘run command’ on reaching Zero.
    LOCKING the Screen is more difficult to ignore : ), and a bonus to Your Command is that It doesn’t terminate things like Audio Streams that may be running.
    The Screen is Just LOCKED! … Time to go check on dinner, don’t want it to burn : )

    Thanks.
    LandisTwo.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *