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
Sample Output:
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
Sample Output:
● 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
Sample output:
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 /etc/systemd/logind.conf
file:
$ 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.
8 comments
Hi,
Very useful article
Thanks a lot
Excellent tip!
Thank you.
After the command
$ sudo systemctl restart systemd-logind.service
the system did not boot anymore. Tried to reinstall, but did not have the option without losing data. Please help!
What did you do actually? This how-to will not cause any data loss. This is just a basic tip. I guess you did something else.
Systemctl works fine for RHEL7, but how To Disable Suspend And Hibernation on RHEL 6 from command line?
I don’t have RHEL 6 at the moment, so I can’t check it. Check it yourself and tell us how did it go.
RHEL6 doesn’t support systemd, so the systemctl command is not available there:
# systemctl status sleep.target suspend.target hibernate.target hybrid-sleep.target
-bash: systemctl: command not found
Worked great!!