Hibernating in Ubuntu has been a nuisance for as long as I can remember. And sadly 20.04 is no different. Each new Ubuntu release slightly improves the process, but we're still nowhere near the "working out of the box" experience enjoyed in other operating systems. A previous post covered getting hibernate working with
s2disk, and this post will cover integrating hibernate with GNOME.
The good news is GNOME settings in Ubuntu 20.04 now explicitly has an entry for deciding what the power button can do:
That's as far as the good news goes though, with hibernate missing from the list. I went digging into the corresponding
gsettings key to see what options were available for the
ben@pc:~$ gsettings range org.gnome.settings-daemon.plugins.power \
'hibernate' is a valid option, but not presented by GNOME settings' UI. Why? I tried setting it manually using
gsettings (which confusingly then shows up as "2 seconds" in the GNOME's UI):
ben@pc:~$ gsettings set org.gnome.settings-daemon.plugins.power \
Still no joy—the setting seemingly had no effect on the power button's behaviour. I opened an issue with the GNOME team to see if they had any hints. A maintainer got back to me with a response explaining what was going on:
systemd-logind needs to be explicitly told you can hibernate before the display manager (i.e. GNOME) will have permissions to execute the hibernation. Which makes sense.
systemctl hibernate works, your login daemon still needs to know about it.
Before telling the login daemon about hibernation, ensure your system can actually hibernate. To test this, run the hibernation service:
ben@pc:~$ systemctl hibernate
Your computer should go through the hibernate process, then turn off. Importantly, when turned back on all of your state should return (i.e. open windows, terminals, etc.).
If this doesn't happen, then your hibernate isn't working properly. My previous post picked apart getting hibernate working in Ubuntu 18.04. So if you're having troubles, I'd recommend heading there before continuing.
Ubuntu uses an implementation of PolicyKit called "Local Authority" for managing communications between privileged and unprivileged processes. PolicyKit is well documented if you want to dig further:
ben@pc:~$ man polkit
For allowing GNOME to request hibernation, we have to tell "Local Authority" our login daemon is allowed to hibernate. There is good documentation for "Local Authority" as well:
ben@pc:~$ man pklocalauthority
The key section is the description of where configuration changes should be made: we should use
/etc/polkit-1/localauthority for local configuration, and
50-local-d for our usage. Combined with appropriate file naming, create the following file:
/etc/polkit-1/localauthority/50-local-d/com.ubuntu.enable-hibernate.pkla[Re-enable hibernate by default in upower]
[Re-enable hibernate by default in logind]
Note: the permissions in these folders are pretty strict, so you'll need
sudo -i to create the file
Now you should be able to select hibernate in the GNOME settings interface without any issues (a reboot or logout may be required):
Done! The hibernate button should now successfully hibernate your computer.
I've used a hotkey in the past when I haven't been able to get the power button to work. If you're having troubles, or simply would prefer to use a hotkey, I've include the instructions below.
This involves two parts: allowing hibernation without opening a password prompt, and configuring the hotkey. Thankfully, they're both relatively easy:
Add an entry to
/etc/sudoers which permits password-less execution of the hibernate command under root permissions. These days this should be done by creating a file in the
/etc/sudoers.d/ directory to avoid directly editing
/etc/sudoers. Remember to use
visudo when editing the file:
/etc/sudoers.d/90-hibernateben ALL=NOPASSWD: /usr/bin/systemctl hibernate
Test this works by running
sudo systemctl hibernate in a new terminal. The system should hibernate without prompting for a password.
Then configure the hotkey in GNOME Settings as below:
The steps are getting fewer and fewer as the Ubuntu versions progress, but it is still more steps than it should be! Windows and Mac users have a kinder experience, maybe Ubuntu 22.04 will bring us up to scratch?
Let me know below if you liked this content, or found it valuable, using the buttons below.
© Ben Talbot. All rights reserved.