How To Enable Hibernation On Ubuntu (When Using A Swap File) hibernate How To

This article explains how to enable hibernation using systemd on recent Ubuntu releases when using a swap file (which is default for Ubuntu at least since version 18.04).

My laptop’s battery drains quite fast while it’s sleeping. I’ve tried some solutions / workarounds, like this one (except for me the problem wasn’t that the laptop was losing half its energy overnight, but all of it in only a few hours), but to no avail. So I decided to enable the hibernate option (suspend to disk) and use that instead of the default sleep option (suspend to RAM), because it uses less power, so the laptop battery is not drained while not in use, albeit taking a bit longer to wake up from hibernation than from sleep. So here’s how to do that.

The instructions below may work on other Linux distributions, but I only tested this on Ubuntu (it should work in the exact same way on any Debian-based and Ubuntu-based Linux distributions, including Pop!_OS, etc.). On non Debian-based Linux distributions you may need to use different commands for updating GRUB 2 (step 4) and regenerating initramfs (step 6) as these are Debian-specific commands.

It’s also important to note that hibernation in Ubuntu is disabled because there are issues with it on some computers, so it might not work for everyone. There was a bit of a chatter about re-visiting hibernate on Ubuntu with the 20.10 release, but it remained disabled by default.

Prerequisites

It’s worth noting that you’ll want to have a swap file at least as large as the computer’s RAM (hibernation may or may not succeed if your computer has a swap file smaller than the system RAM). This article explains how to transition from a swap partition to a swap file – you can also use those instructions to create a bigger swap file. 

Also, these instructions won’t work if you’ve specifically encrypted your swap. Since my laptop’s swap is not encrypted, I’m not able to try this with an encrypted swap, so the instructions below are for unencrypted swap files only. But you should know that if you already have an encrypted root filesystem, all the files on it – including the swap file – will also be encrypted, and the instructions below work in that case.

To find out the size of your computer’s swap, and if it uses a swap file or partition (you’ll need to know this later on), you can use this command:

cat /proc/swaps

Here’s an example with output from my laptop:

$ cat /proc/swaps
Filename	Type	Size		Used		Priority
/swapfile	file	8388604		3213700		-2

As you can see, under Type it says file, so my computer is using a swap file (and its path is /swapfile), and its size is 8388604 kilobytes (8,3 GB).

By default, Ubuntu uses a swap file for some time (at least starting with Ubuntu 18.04), so the instructions below will assume that’s what you’re using. In case you use a swap partition, use the UUID of the swap partition instead of the UUID of the partition on which the swap file is located in the instructions below, and don’t add a swap file offset (resume_offset parameter) anywhere. Those are the differences between hibernating with a swap partition and a swap file.

You might also be interested in: 3 Tools To Display Linux Laptop Battery Information From the Command Line


How to enable hibernation on Ubuntu (when using a swap file)

1. Find out the UUID of the partition on which the swap file resides.

We’ll need to add this to the GRUB file and initramfs resume file.

To find out the UUID of the partition on which your swap file is located, you can use the following command:

findmnt -no UUID -T /swapfile

(/swapfile is the path to the swap file, and may be different on your computer; use the exact path as returned by the cat /proc/swaps command). 

Example from my computer (with output):

$ findmnt -no UUID -T /swapfile

4a59c6a7-ca54-4e24-a362-3eac83bfe226

2. Find out and the swap file offset.

Next, we’ll need to find out the swap file offset. Like the UUID, we’ll need to add this to the GRUB file and initramfs resume file.

This can be done by running the following command:

sudo filefrag -v /swapfile

From this command’s output, the first block of numbers from the “physical_offset” column is the swap file offset which you’ll need later on. Only copy the numbers, and not the dots that follow the number block.

E.g. in my case, this is the output of the sudo filefrag -v /swapfile command:

$ sudo filefrag -v /swapfile
Filesystem type is: ef53
File size of /swapfile is 8589934592 (2097152 blocks of 4096 bytes)
 ext:     logical_offset:        physical_offset: length:   expected: flags:
   0:        0..    6143:    4974592..   4980735:   6144:            
   1:     6144..    8191:    4982784..   4984831:   2048:    4980736:
   2:     8192..   10239:    4988928..   4990975:   2048:    4984832:
   3:    10240..   12287:    4997120..   4999167:   2048:    4990976:
........................................

In this example, the swap file offset is 4974592 (this is the first block of numbers from the psysical_offset column).

3. Edit the /etc/default/grub file in order to boot with the resume and resume offset kernel parameters.

It’s very important to make sure your edits of this file are correct, or else your computer may fail to boot!

Open the /etc/default/grub file with a text editor as root, e.g. using the command below we’ll open it using Nano command line text editor:

sudo nano /etc/default/grub

In this file, at the end of the GRUB_CMDLINE_LINUX_DEFAULT line, before the ending ", add the following: resume=UUID=UUID_FROM_STEP_1 resume_offset=SWAP_OFFSET_FROM_STEP_2. Replace the values with the UUID you’ve got under step 1 and the swap resume offset you’ve got under step 2.

To save the file and exit Nano, press Ctrl o, then Enter, and then Ctrl x.

Example. This is how the GRUB_CMDLINE_LINUX_DEFAULT line looks on my computer after I’ve added the swap UUID and swap offset:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=4a59c6a7-ca54-4e24-a362-3eac83bfe226 resume_offset=4974592"

4. Update the GRUB configuration.

To update GRUB 2 on Debian-based Linux distributions, all you have to do is run the following command:

sudo update-grub

5. Create (or edit if already exists) /etc/initramfs-tools/conf.d/resume with the swap UUID and resume offset.

Next, we need to edit (or create if it doesn’t exist) the /etc/initramfs-tools/conf.d/resume file with a text editor as root. Use the command below to open this file with Nano command line editor:

sudo nano /etc/initramfs-tools/conf.d/resume

In this file, if you have a line beginning with “RESUME”, edit that line, or if there’s no such line (or the file is completely empty), add the line so that it looks like this:

RESUME=UUID=UUID_FROM_STEP_1 resume_offset=SWAP_OFFSET_FROM_STEP_2

To save the file and exit Nano, press Ctrl o, then Enter, and then Ctrl x.

Example with the contents of the /etc/initramfs-tools/conf.d/resume file from my laptop:

RESUME=UUID=4a59c6a7-ca54-4e24-a362-3eac83bfe226 resume_offset=4974592

6. Regenerate initramfs.

Now you need to regenerate initramfs using this command:

sudo update-initramfs -c -k all

7. Reboot your computer.

After rebooting, hibernate your computer using the following command:

sudo systemctl hibernate

For now, you need to provide your password to be able to hibernate, and there’s no menu option to hibernate your computer, so you must do all that from the command line. To be able to hibernate your system without entering a password (and to have an option to hibernate the computer in your desktop’s power option (e.g. in an applet on the panel)), see the next 2 steps.

To resume from hibernation, press the power button on your computer.

If you get an error when running this command, saying “Failed to hibernate system via logind: Not enough swap space for hibernation”, then you may have missed the notice near the top of the article in which I mention that it’s best to have a swap file at least as large as your computer’s RAM. This article explains how to transition from a swap partition to a swap file – you can also use those instructions to create a bigger swap file. 

You can check for errors related to the systemd Hibernate service using:

systemctl status systemd-hibernate.service

8. Optional. Allow hibernation without entering a password (required for the next step). On some desktop environments, this re-enables the Hibernate and Hybrid Sleep power menu options.

By default, to hibernate your computer, you’ll need to enter your password. To allow hibernation without a password, create / open /etc/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla with a text editor as root.

Using the command below will open /etc/polkit-1/localauthority/50-local.d/com.ubuntu.desktop.pkla as root using Nano command line editor:

sudo nano /etc/polkit-1/localauthority/50-local.d/com.ubuntu.desktop.pkla

In this file, paste the following:

[Enable hibernate in upower]
Identity=unix-user:*
Action=org.freedesktop.upower.hibernate
ResultActive=yes

[Enable hibernate 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

Now save the file and exit Nano (press Ctrl o, then Enter, and then Ctrl x).

On some desktop environments, after doing this (and after a logout / re-login) you’ll get the Hibernate and Hybrid Sleep power options. Here’s an example with the Xfce power options after following this step:

<img data-ezsrc="https://www.linuxuprising.com/ezoimgfmt/blogger.googleusercontent.com/img/a/AVvXsEjzYAaHrvzX4pXc-iopT6Iimv1f15ITLaH6YVtNRAOHYJbJP4nm4ELbnArPmacy4CIDUKidwMCCXrHdW96Iaxy_Hz2kNJb6UMQT8cnsDbFFR0yXF2yhFwYbqrPEZ9hG7ROD4V0nArErtjstMB7ZVfDRaO0EiTx9fbxvmievoT7990W6TmHiOVqKQ44l=s16000" data-original-height="346" data-original-width="473" ezimgfmt="rs rscb269 src ng ngcb269" src="data:image/svg xml,”>

This doesn’t work on some desktop environments using Ubuntu though, like with GNOME Shell. In that case, we’ll need an extra step (see below).

this is via AskUbuntu

9. Optional. Add a Hibernate applications menu entry or use a hibernate status button for GNOME Shell.

If you use GNOME Shell, you can use an extension which adds an option to hibernate your computer in the system power menu from the top bar, called Hibernate Status Button.

Here’s the Power Off / Logout GNOME Shell menu with this extension enabled:

<img alt="Hibernate menu GNOME Shell" data-ezsrc="https://kirelos.com/wp-content/uploads/2021/08/echo/hibernate-menu-gnome-shell.png6124cf3319a49.jpg" data-original-height="582" data-original-width="738" ezimgfmt="rs rscb269 src ng ngcb269" height="504" src="data:image/svg xml,” title=”Hibernate menu GNOME Shell” width=”640″>

If you don’t use GNOME Shell, but your desktop environment doesn’t show a Hibernate power option, or you don’t want to use the above-mentioned GNOME Shell extension, you can create an entry in your applications menu by creating a file called hibernate.desktop in ~/.local/share/applications/, e.g. open Nano command line text editor with this file:

nano ~/.local/share/applications/hibernate.desktop

And in this file add the following:

[Desktop Entry]
Type=Application
Name=Hibernate desktop
GenericName=Hibernate desktop
Comment=Enter hibernation
NoDisplay=false
Icon=drive-multidisk
Exec=systemctl hibernate
Terminal=true
Categories=System;Utility;Settings;

You will now be able to go into your desktop’s applications menu and click on the Hibernate icon to hibernate your system. For this to work, you need to follow the optional step 9 from above in order to be able to hibernate without entering the password.

I should also clarify what’s with the Hybrid Sleep option. Besides sleep / suspend (suspend to RAM) and hibernate (suspend to disk), there’s also the option of Hybrid Sleep (also called Hybrid Suspend) which starts by putting your computer to sleep, then transitioning to hibernation after a given amount of time. You can set the hybrid sleep time between suspend and hibernation (along with other options) by editing the /etc/systemd/sleep.conf file, then rebooting your system. For more on this, see the Arch Wiki.

You might like: How To Enable Hardware Accelerated Video Decode In Google Chrome, Brave, Vivaldi And Opera Browsers On Debian, Ubuntu Or Linux Mint