Learn through the super-clean Baeldung Pro experience:
>> Membership and Baeldung Pro.
No ads, dark-mode and 6 months free of IntelliJ Idea Ultimate to start with.
Last updated: September 6, 2024
When we’re not using our computer, it can enter a low-power state known as sleep mode. During sleep mode, most processes pause, and only a small amount of power is used to keep data in memory. In addition to sleep mode, there’s also hibernate mode, which saves everything from RAM to the hard drive, allowing us to turn off the computer completely.
In this tutorial, we’ll explore various methods to customize sleep settings based on our needs in Linux.
If we want to stop the computer from going to sleep when left on the login screen, we can change the settings in GNOME Display Manager (gdm3). This method applies specifically to users running the GNOME desktop environment.
Furthermore, we can find the sleep-related settings in the /etc/gdm3/greeter.dconf-defaults file.
Let’s open this file as a superuser in the nano text editor:
$ sudo nano /etc/gdm3/greeter.dconf-defaults
Once the file is open in the editor, we need to find the Automatic Suspend section by scrolling down the file using the arrow keys.
Next, let’s change the value of sleep-inactive-ac-type to ‘blank’ if it’s not already set:
Lastly, we need to save and exit the file and restart the gdm3 service for the changes to take effect:
$ sudo systemctl restart gdm3
That’s it! Our computer will no longer go to sleep automatically at the login screen.
We can use the systemctl command-line tool to configure and manage the systemd system and service manager. Additionally, we can use it to adjust sleep settings in Linux through CLI.
If we don’t want our computer to automatically go to sleep or hibernate, we can completely turn it off using systemctl.
Let’s disable all the sleep-related features:
$ sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
Here, we tell our computer to mask (or block) the sleep, suspend, hibernate, and hybrid-sleep features. Additionally, the mask operation creates a symbolic link that points to /dev/null for the specified target or service.
In addition, we can re-enable these features if we decide to revert our changes:
$ sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
Here, we remove the symbolic link that points to /dev/null through the unmasking technique, which effectively re-enables the service or target.
We can also directly change other sleep settings via the terminal, such as putting our computer into sleep or hibernate mode.
To suspend our system and put it to sleep, let’s use this systemctl command:
$ systemctl suspend
Moreover, let’s put our system into hibernate mode:
$ systemctl hibernate
After running this, the screen will go black, indicating that the system is now in hibernation. We can wake it up by dragging the mouse or by pressing a key.
We can also schedule sleep mode by piping the output of the systemctl command to the at command:
$ echo 'systemctl suspend' | at now + 20 minutes
Moreover, the above command schedules our computer to enter sleep mode after 20 minutes.
Configuring the lid power settings in Linux determines what happens when we close our laptop’s lid. For example, we can set it to do things like shutting down, suspending, hibernating, or doing nothing.
We can make these changes by opening and editing the /etc/systemd/logind.conf file in the Linux terminal. This file controls some of the power management settings on our computer.
Let’s open this file in the nano editor:
$ sudo nano /etc/systemd/logind.conf
After opening the file, we can now configure the HandleLidSwitch setting that controls what our laptop does when we close the lid.
For example, we can change the word suspend to poweroff, hibernate, suspend, or ignore, based on what we want to happen when the lid is closed:
Finally, we exit the file after saving it, and restart the service or reboot our system to apply these changes:
$ sudo systemctl restart systemd-logind
We need to ensure that the HandleLidSwitch isn’t commented out (meaning the setting is ignored). If it’s commented out, we need to remove the # from the beginning of the setting line.
We can also adjust and change the sleep and other power-related settings for GNOME systems using gsettings.
For example, let’s set a screen blank timeout to 600 seconds (10 minutes):
$ gsettings set org.gnome.desktop.session idle-delay 600
Additionally, we can disable the automatic suspend when the system is running on AC power:
$ gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-type 'nothing'
We can also set the amount of time the system waits before suspending when operating on battery. For instance, let’s set the delay to 1200 seconds (20 minutes):
$ gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout 1200
Furthermore, we can set the system to sleep after 300 seconds (5 minutes) when running on AC power:
$ gsettings set org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout 300
Finally, let’s display the current timeout settings for AC power sleep and battery power sleep:
$ gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-ac-timeout
$ gsettings get org.gnome.settings-daemon.plugins.power sleep-inactive-battery-timeout
We need to ensure that we don’t confuse sleep-inactive-ac-timeout with sleep-inactive-battery-timeout. The AC timeout applies to AC power settings, and the battery timeout applies to battery power.
In this article, we explored various Linux CLI methods to adjust and manage our system sleep settings. Some methods include using systemctl, changing gdm3 settings, and editing configuration files.
Furthermore, we can use these methods to suspend, hibernate, make the screen go blank, or completely disable the sleep-related features from our terminal interface.