For some reason, Ubuntu Server comes with ‘power management’ enabled. This is an article that tells you how to disable sleep and hibernation in Ubuntu Server. It’ll work just fine for non-server installs, but this is aimed specifically at the Ubuntu Server release.
I recently was working on my own router. For this, I used Ubuntu Server as the base operating system. For some reason, it was shutting itself down after periods of inactivity. This isn’t acceptable behavior for a device that’s meant to be running all the time.
I checked in my logs and I found entries like this one:
Apr 3 12:18:27 server systemd[1]: Reached target Sleep.
That was entirely unacceptable. I do not know why power management was installed, nor do I know why it was active by default. I merely know that it was and that I couldn’t have that behavior with a server, a device meant to be always powered on.
So, I did what anyone would do in my shoes. I disabled sleep and hibernation entirely. It’s quick and easy – and effective! I’ll show you how!
Disable Sleep/Hiberation:
Like most articles, you’re gonna need a terminal. If it’s actually a server, you’re likely already able to connect with SSH. So, add the step of connecting to the server if you’re doing this remotely. If not, just proceed.
Once you have your terminal open, you’re to kill everything that has to do with suspend, sleep, or hybrid-sleep. It’s actually pretty easy. Start by opening said terminal, by pressing
First, you mask ‘sleep.target‘:
1 | sudo systemctl mask sleep.target |
Then mask ‘suspend.target‘:
1 | sudo systemctl mask suspend.target |
And mask ‘hibernate.target‘:
1 | sudo systemctl mask hibernate.target |
Finally, mask ‘hybrid-sleep.target‘:
1 | sudo systemctl mask hybrid-sleep.target |
Later, should you change your mind, you can unmask them and that’ll enable them again. Just change ‘mask’ to ‘unmask’ and run the commands again. See? Pretty easy!
If you want, you can verify the efficacy. Simply use the following:
1 | systemctl status sleep.target |
(You can change ‘sleep.target’ to one of the above services and check them individually.)
Closure:
That’s it! I told you that it’d be pretty easy. It’s not only easy, it’s easy to undo this should you change your mind. Again, I do not know why power management is enabled by default in a server release. Nobody asked me! So, that’s how you disable sleep and hibernation with Ubuntu. (It’ll surely work with other distros.)
Thanks for reading! If you want to help, you can donate, write an article, vote for articles you like, share this article on social media, buy cheap hosting, register to help, etc… Nobody ever reads the last paragraph anyhow. Still, you can help if you want!
Update 01.27.2020 Per comment.
Last Updated on January 27, 2022 by KGIII
This is ridiculous. Those are systemctl calls. Requiring sudo privileges even. Why aren’t they available as a checkbox in Settings? I mean, I can’t afford to have a screen fail because of a typo.
If you have a GUI, you can look under ‘power management’. This is for Ubuntu Server – which has no GUI unless you install one.
And, power management probably *should* be a sudo-level function. Do you really want a regular user deciding those things?
You are repeating the command ‘sudo systemctl mask hibernate.target’ at the end of this useful text.
Updated and good catch! Thanks! Feedback makes the article better!
Ubuntu sleep is a real problem with 22.04 and probably other LTR’s. The issue is that the server goes to sleep and there is absolutely no way to wake it up — short of crashing the system and rebooting.
I’m an official Ubuntu member and not even I know why sleep is even enabled on a server distro.
It just means more work for admins. I obviously love Ubuntu but I do think that having sleep enabled by default is a bit of an oversight. Most folks don’t want their server going to sleep.
Fortunately, it’s a pretty easy fix.