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 \ power-button-action enum 'nothing' 'suspend' 'hibernate' 'interactive'
'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 \ power-button-action hibernate
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, you still need to tell your login daemon about it.
Before telling the login daemon about hibernation, ensure your system actually can 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, the following file needs to be created:
[Re-enable hibernate by default in upower] Identity=unix-user:* Action=org.freedesktop.upower.hibernate ResultActive=yes [Re-enable hibernate by default in logind] Identity=unix-user:* Action=org.freedesktop.login1.hibernate;org.freedesktop.login1.handle-hibernate-key;org.freedesktop.login1;org.freedesktop.login1.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit ResultActive=yes
Note: the permissions in these folders are pretty strict, so you'll probably have to use
sudo -i when creating this 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! You should now be able to hibernate using your power button.
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:
ben 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.