Today we are going to learn how to disable suspend and hibernation in Linux. Before we get into the topic, let us see a brief introduction to suspend and hibernate modes in Computer.
What are Suspend and Hibernate Modes?
Whenever you suspend your system, the computer will enter into sleep mode. Meaning - all of the opened applications, files and documents remain open, but the monitor and other parts of the system will be switched off to save the power. When you suspend the computer, it is not going to be completely powered off. The computer is still switched on and it will still be using a small amount of power.
The hibernation is one of the type of suspend. There are currently three suspend methods available:
- Suspend to RAM or Normal Suspend - In this mode, the screen and most parts of the system will be turned off (but not completely) except the RAM. All Laptops will enter this suspend mode when the user is inactive for a particular time or when the system is running on batteries or the lid is closed. When you press a Key or click the mouse button, the computer will wake up to the normal mode.
- Suspend to DISK or Hibernate- In this mode, the current computer's state will be saved into swap space and the system will be turned off completely. When you turn on the system again, the state is restored. This is usually known as hibernation.
- Suspend to BOTH or Hybrid Suspend- In this mode, the current computer's state is saved into swap space and the usual suspend to RAM mode is invoked. So, the computer is not turned off completely. If the battery is depleted, the computer can be resumed from disk. If the battery is not depleted, the computer can be resumed from RAM. This is sometimes called as hybrid suspend.
Hope you get the basic idea about suspend and hibernate modes. Now, let us get back to the topic and see how to disable suspend and hibernate modes.
Disable Suspend and Hibernation in Linux
If you want to prevent your Linux machine from suspending and hibernating, disable the following targets at the systemd level:
$ sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
Created symlink /etc/systemd/system/sleep.target → /dev/null. Created symlink /etc/systemd/system/suspend.target → /dev/null. Created symlink /etc/systemd/system/hibernate.target → /dev/null. Created symlink /etc/systemd/system/hybrid-sleep.target → /dev/null.
Save all works (just in case) and then run the following command to apply the changes:
$ sudo systemctl restart systemd-logind.service
Alternatively, reboot the system for this to take effect.
Check if the changes have been really applied:
$ systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target
● sleep.target Loaded: masked (/dev/null; bad) Active: inactive (dead) ● suspend.target Loaded: masked (/dev/null; bad) Active: inactive (dead) ● hibernate.target Loaded: masked (/dev/null; bad) Active: inactive (dead) ● hybrid-sleep.target Loaded: masked (/dev/null; bad) Active: inactive (dead)
As you see in the above output, the suspend and hibernate system targets have been masked and inactive.
To re-enable suspend and hibernate on your system, simply run:
$ sudo systemctl unmask sleep.target suspend.target hibernate.target hybrid-sleep.target
Removed /etc/systemd/system/sleep.target. Removed /etc/systemd/system/suspend.target. Removed /etc/systemd/system/hibernate.target. Removed /etc/systemd/system/hybrid-sleep.target.
If you just want to prevent suspending when the lid is closed, edit
$ sudo nano /etc/systemd/logind.conf
Add/modify the following lines:
[Login] HandleLidSwitch=ignore HandleLidSwitchDocked=ignore
Save and close the file. Don't forget to restart systemd-logind.service or reboot your system to take effect the changes.
For more details, check man pages.
$ man logind.conf
Hope this helps.