1. Overview

Locking the screen when closing the laptop lid is a good practice, as it helps protect sensitive information and prevents unauthorized access. Additionally, it enhances the energy efficiency and durability of the battery in the system.

In this tutorial, we’ll explore three methods to lock the screen automatically upon closing the laptop lid in Linux.

2. Using System Settings

Using the System Settings in Linux, we can manage various power management tasks, including lid-closure events. It provides a user-friendly interface that allows us to control screen brightness, screen blanking, power management of USB devices, and battery power consumption settings.

In the XFCE desktop environment, we go to the Start menu and search for the Power Manager application in the search bar:

Searching in the start menu of linux

After opening the Power Manager application, we go to the System tab. Under the System tab, we can find the Laptop Lid section:

Laptop Lid

Here, we can set the action we want to execute when closing the lid of our laptop. The available actions include locking the screen, switching off the display, suspending the system, and hibernating the system. Additionally, based on whether the laptop is running on battery or the adapter is plugged in, we can set different actions as we close the laptop lid.

Other desktop environments like GNOME and KDE Plasma also provide similar power management tools, which are part of the System Settings. However, the name of the power management tools and their settings can vary based on the environment. For example, in GNOME, we go to the Start menu and search for the Power Management tool. We can find the options to set an action when we close the laptop lid under the Energy Saving tab within the Power Management tool.

3. Using the TLP Utility

TLP is a power management tool used to optimize the battery life of laptops. Using this tool, we can trigger a custom action when we close the lid of a laptop. Additionally, the TLP tool allows us to manage the speed of the cooling fans, frequency of CPU scaling, power of GPUs, and switching between different power profiles based on our needs.

3.1. Installing TLP

First, we install the TLP tool in the Linux terminal using the apt command:

$ sudo apt-get install tlp

Additionally, we can use the pacman command to install the TLP tool on Arch-based distributions like Arch Linux:

$ sudo pacman -S tlp

The TLP tool is also available in other Linux distributions. After we complete the installation, let’s check the status of the TLP tool:

$ tlp-stat -s
--- TLP 1.5.0 --------------------------------------------

+++ System Info
System         = HP Type1ProductConfigId HP Laptop 15q-ds1xxx
BIOS           = F.21
OS Release     = Linux Lite 6.6
Kernel         = 5.15.0-82-generic #91-Ubuntu SMP Mon Aug 14 14:14:14 UTC 2023 x86_64
/proc/cmdline  = BOOT_IMAGE=/boot/vmlinuz-5.15.0-82-generic root=UUID=add4453a-c614-402a-9bbc-c6f860346c41 ro quiet splash
Init system    = systemd v249 (249.11-0ubuntu3.9)
Boot mode      = UEFI

+++ TLP Status
State          = enabled
RDW state      = enabled
Last run       = unknown
Mode           = unknown
Power source   = AC

We can see the status is enabled. Additionally, we need to start it from the terminal in case the TLP status is disabled:

$ sudo tlp start

Now, we can proceed to configure TLP’s configuraion file.

3.2. Configuring the Lid-Close Event in TLP

TLP’s configuration file provides us with all the default settings and policies related to the power management of the system. Furthermore, we aim to find the policies related to the lid-close event and change the action as we want. Let’s open and edit the /etc/default/tlp configuration file using the nano editor:

$ sudo nano /etc/default/tlp
# Configuration for TLP (TLP is a tool to optimize power management on Linux) 
# Set to 1 to enable TLP (default) 
# Seconds after the disk goes to sleep 
# Restore radio device state on startup (1 for enabled, 0 for disabled) 
# Set power supply controller threshold value to switch to battery mode 
...output truncated...

First, we need to search for the RESTORE_DEVICE_STATE_ON_STARTUP line and uncomment it so that we can set a custom lid-close action.

Next, we look for the LID_SLEEP line, which determines the action the system executes when we close the lid of the system. Here, we want to lock the screen upon closing the lid. Therefore, we set LID_SLEEP to LockSimilarly, we can shut down or hibernate the system upon closing the lid by setting an appropriate action.

After we edit the configuration file, we save and close the file. Finally, we need to reload the TLP service to reflect the changes we made in the configuration file using the systemctl command:

$ sudo systemctl restart tlp

Now, when we close the lid, TLP initiates the system locking procedure automatically.

4. Modifying logind.conf File

We can modify the logind.conf file to set a custom action when we close the laptop lid. It’s a configuration file used by systemd, which is a system and service manager. Before trying this approach, it’s important to check if the systemd services are running in the system or not. To check the status, we can use the systemctl tool, which is responsible for managing the systemd services:

$ systemctl --version
systemd 249 (249.11-0ubuntu3.9)

Having the systemctl tool installed confirms that the systemd services are running in the system. Now, let’s edit the logind.conf file using the nano editor:

$ sudo nano /etc/systemd/logind.conf
# This file is part of systemd.
# Use 'systemd-analyze cat-config systemd/logind.conf' to display the full config.
# See logind.conf(5) for details.
<code class="language-bash">...output truncated...

In the file, first, we need to find the line with HandleLidSwitch and uncomment it. Furthermore, we need to remove the suspend and assign the lock option. Finally, we save the file and restart the systemd-logind service:

$ sudo systemctl restart systemd-logind

After restarting the service, it should lock the screen when we close the lid.

5. Conclusion

In this article, we discussed three methods to lock the screen upon closing the lid of the laptop.

Using the System Settings tool, we can lock the screen, and it doesn’t require any technical knowledge about Linux systems. On the other hand, in the case of advanced power management tasks, we can utilize the TLP tool. Finally, the logind.conf file provides a wide range of options for power management but works only in the systemd-based Linux distributions.

Comments are closed on this article!