1. Overview

As a Linux administrator, locking the desktop screen is an important security measure we can use to prevent unauthorized access to our computers. In short, we prevent access to our files, applications, and sensitive information from unauthorized users. There are several ways we can use to lock the desktop screen, but using the command line is quick and easy.

In this tutorial, we’ll discuss how to lock the desktop screen in Linux using gnome-screensaver-command, xdg-screensaver, and dbus-send in the command line.

2. Using the gnome-screensaver-command

gnome-screensaver-command is a Linux utility that allows us to control the GNOME screensaver from the command line. Therefore, we can use it to lock the desktop screen from the command line.

Before using this command, we need to install it:

$ sudo apt install gnome-screensaver
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
...
Processing triggers for man-db (2.10.2-1) ...
Processing triggers for mailcap (3.70+nmu1ubuntu1) ...

The above command installs gnome-screensaver on Ubuntu and Debian-based distributions.

Next, let’s lock our desktop screen:

$ gnome-screensaver-command -l

In the above example, the -l option instructs the running screensaver process to lock the screen immediately. In order to unlock the screen, we’ll need to do some authentication.

3. Using the xdg-screensaver Command

xdg-screensaver is a command-line utility used to interact with the screensaver application in Linux. To clarify, we can use this command to lock the desktop screen, control the screensaver timeout, and get information about the screensaver status.

Now, let’s lock our desktop screen:

$ xdg-screensaver lock

Using the above command, our desktop screen will immediately lock, and therefore we’ll require authentication to unlock it.

4. Using the dbus-send Command

dbus-send is a powerful tool that allows us to send D-Bus messages to other applications, services, and even to the system itself. Additionally, we can use this command to lock the desktop screen from the command line in Linux.

The command follows this syntax:

$ dbus-send --type=TYPE --dest=DESTINATION PATH INTERFACE.METHOD [ARGUMENTS] 

It contains these parameters:

  • –type – used to specify the type of message being sent, either a method call, a signal, or a property change
  • –dest – specifies the name of the target application or service
  • PATH – specifies the object path on which to call the method or signal
  • INTERFACE.METHOD – specifies the interface and also the method to be called, respectively

Now, let’s use it:

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

The above command locks the desktop screen immediately and will work for the GNOME desktop environment. In order to unlock it, we’ll need to do some authentication.

5. Conclusion

In this article, we’ve discussed how to lock the desktop screen using the gnome-screensaver, xdg-screensaver, and dbus-send commands in Linux. Furthermore, we saw this is a straightforward process that we can complete in just a few steps.