Server

How To: Disable Sleep And Hibernation on Ubuntu Server

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 CTRL + ALT + T and then enter the following commands:

First, you mask ‘sleep.target‘:

sudo systemctl mask sleep.target

Then mask ‘suspend.target‘:

sudo systemctl mask suspend.target

And mask ‘hibernate.target‘:

sudo systemctl mask hibernate.target

Finally, mask ‘hybrid-sleep.target‘:

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:

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.

KGIII

Retired mathematician, residing in the mountains of Maine. I may be old and wise, but I am not infallible. Please point out any errors. And, as always, thanks again for reading.

View Comments

  • 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.

  • 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.

Recent Posts

Update Python Packages (PIP)

We've had a run of Python packages recently and you can tell that I'm a…

18 hours ago

Save A Command’s Output To A File (While Showing It In The Terminal)

The title is the best I can come up with to describe this exercise as…

3 days ago

Demystifying journalctl: A Comprehensive Guide to Linux System Logging

It was suggested that I write an article about journalctl, which seemed like a large…

5 days ago

Extract Text From Multiple File Types

Today we will have a fairly simple exercise as we're going to just use a…

7 days ago

Meta: I’ve Been At This For Three Years!

I have not done a meta article lately. I don't find them interesting to write…

1 week ago

How To: Disable CPU Cores

This is not something everyone is interested in doing but you might as well learn…

2 weeks ago