Disable Suspend and Hibernation in Linux desktop

When you basically suspend or Hibernate your Linux desktop, all the open applications and files get saved to the memory (either to the Disk or the RAM of your device) and your screen turns off, resulting in saving your battery life in laptops and Power if you are using a desktop PC.

Resuming your work is always easy with just a press of a button or a simple mouse click. If you have hibernated, well, then you might have to press the Power button as well.

Also read: Linux System Restart – How to Perform a System Restart the Terminal and Python/C++

How does suspension work on Linux?

There are currently 3 modes by which you can suspend your PC, they are:

  • Suspension to RAM (Normal Suspend) – This mode is most widely used in the distribution installed on laptops and is activated when you leave your device IDLE on battery for a certain amount of time or close the lid of the laptop. Power is cut from all the PC components and is only reserved for RAM, which holds your data.
  • Suspension to Disk (Hibernate) – If you have SWAP enabled on your device, then your machine state is saved to the Swap file and your device is completely powered off. When you turn it on again, then you can resume your work again.
  • Hybrid Suspend – In this case, all the machine state is saved to Swap, but the PC is suspended to RAM and the machine does not turn off completely. The battery is still consumed, and this process also takes more time to boot your PC than the suspension to RAM method does.

Disable Suspend and Hibernation Features

If you want to disable these features on your PC for some reason, then you can do so by typing the following commands:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
Disabling Suspension On Linux
Disabling Suspension On Linux

Now, verify the changes by typing the following commands in your Terminal:

systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target
Suspend And Hibernation Are Now Disabled On Linux
Suspension And Hibernation Are Now Disabled On Linux

As you can see, all the services are now disabled/dead. On a laptop, you can disable suspension while closing the lid by editing the /etc/systemd/logind.conf file. To do that, you can open the file in your preferred text editor. Type the following commands in your Terminal:

# For Vim users
sudo vim /etc/systemd/logind.conf

# For nano Users
sudo nano /etc/systemd/logind.conf

At the end of the file, add the following lines:

[Login] 
HandleLidSwitch=ignore 
HandleLidSwitchDocked=ignore
Edit The Config Files To Disable Laptop Suspends On Closing The Lid
Edit The Config Files To Disable Laptop Suspends On Closing The Lid

Now, save and exit out of the file by pressing Escape key and then :wq if you used vim editor and press Ctrl+O and Ctrl+X to exit the nano editor. Now, reboot your PC for changes to take effect.

Reverting the changes

In case you need to revert the changes which we made above, you can simply type the following commands in your Terminal:

sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
Enabling Suspension And Hibermation On Linux
Enabling Suspension And Hibernation On Linux

Again, you can verify the status of these services to make sure the changes have successfully taken effect. You might have to restart your PC for changes to take effect fully.

Suspension Is Now Enabled On Your PC
Suspension Is Now Enabled On Your PC

If you have disabled the Laptop suspends on closing the lid, then just edit and comment out the lines which we have added in the /etc/systemd/logind.conf file. Open the file using your text editor such as Vim or Nano and then add ‘#’ in front of both the comments that we have added, like this:

Comment Out All The Options To Disable Laptop Suspend On Lid Closure
Comment Out All The Options To Enable Laptop Suspend On Lid Closure

Again, save and exit the file by pressing the Escape key and then :wq in Vim editor and Ctrl+O and Ctrl+X in nano editor. Reboot your PC for changes to take effect.

Summary

In this tutorial, we learned how can you disable suspension on any device which has a Linux-based (and systemd) based operating system installed. There can be tons of applications of these settings as you can make your Old PC or laptop server using this method and disable Hibernate modes.

References

Arch Wiki – Suspend and Hibernate