This article explains how to use systemd to enable hibernation on newer versions of Ubuntu when using a paging file (which has been standard for Ubuntu since at least version 18.04).
My laptop battery drains pretty quickly while it sleeps. I’ve tried some solutions / workarounds like This one here (Except for me the problem wasn’t that the laptop lost half of its power overnight, but everything in just a few hours), but to no avail. So I decided to enable the Suspend to Disk option and use that instead of the standard Suspend to RAM option as it uses less power so the laptop battery doesn’t drain when it’s not in use will, even if it takes a little longer to wake up from hibernation than from sleep. That’s how it is done.
The instructions below may work on other Linux distributions, but I’ve only tested this on Ubuntu (it should work the same way on all Debian-based and Ubuntu-based Linux distributions, including Pop! _OS, etc.). On non-Debian Linux distributions, you may need to use different commands to update GRUB 2 (step 4) and refresh initramfs (step 6) as these are Debian-specific commands.
It is also important to note that hibernation is disabled in Ubuntu as it will cause problems on some computers and therefore it may not work for all. It was a bit a Chatter about revisiting hibernation on Ubuntu with version 20.10, but it stayed disabled by default.
That is worth mentioning You should have a paging file that is at least as large as the computer’s memory (Hibernation may or may not succeed if your computer has a paging file that is less than the system’s memory). This article explains how to switch from a swap partition to a swap file – you can also use these instructions to create a larger swap file.
Also, if you’ve specially encrypted your swap, these instructions won’t work. Since my laptop’s swap is not encrypted, I cannot attempt this with an encrypted swap, so the instructions below only apply to unencrypted swap files. However, you should know that if you already have an encrypted root file system, all files in it – including the paging file – will also be encrypted, and the following instructions will work in this case.
To find out the size of your computer’s swap and if it is using a swap file or partition (you will need to know this later) you can use this command:
Here is an example with output from my laptop:
$ cat /proc/swaps Filename Type Size Used Priority /swapfile file 8388604 3213700 -2
As you can see below
Type it says
file, so my computer is using a paging file (and its path is
/swapfile), and its size is
8388604 Kilobytes (8.3 GB).
By default, Ubuntu uses a paging file for some time (at least starting with Ubuntu 18.04), so the instructions below assume that you are using it. If you are using a swap partition, in the following instructions use the UUID of the swap partition instead of the UUID of the partition on which the swap file is located, and do not add a swap file offset (
resume_offset Parameters) everywhere. These are the differences between hibernation with a swap partition and a swap file.
You may also be interested in: 3 Tools To View Linux Laptop Battery Information From The Command Line
How to enable hibernation on Ubuntu (when using a paging file)
1. Determine the UUID of the partition on which the swap file is located.
We need to add this to the GRUB file and the initramfs resume file.
To find out the UUID of the partition where your paging file is located, you can use the following command:
findmnt -no UUID -T /swapfile
/swapfile is the path to the swap file and can be different on your computer; Use the exact path that is returned from the
cat /proc/swaps Command).
Example from my computer (with output):
$ findmnt -no UUID -T /swapfile4a59c6a7-ca54-4e24-a362-3eac83bfe226
2. Find out the offset of the paging file.
Next we need to find out the offset of the paging file. Like the UUID, we need to add this to the GRUB file and the initramfs resume file.
This can be done by running the following command:
sudo filefrag -v /swapfile
In the output of this command, the first block of numbers from the “physical_offset” column is the offset of the swap file, which you will need later. Copy only the numbers and not the dots that follow the number pad.
In my case, for example, 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 offset is the paging file
4974592 (This is the first number block from the
3. Edit the
/etc/default/grub File to boot with the kernel parameters “resume” and “resume offset”.
It is very important to make sure that your edits to this file are correct or your computer may not start!
/etc/default/grub File with a text editor as root, e.g. with the following command we open it with the 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 end
", add the following:
resume=UUID=UUID_FROM_STEP_1 resume_offset=SWAP_OFFSET_FROM_STEP_2. Replace the values with the UUID you got in step 1 and the swap resume offset you got in step 2.
To save the file and exit Nano, press
Ctrl + o, then
Enter, and then
Ctrl + x.
Example. That’s how it’s done
GRUB_CMDLINE_LINUX_DEFAULT line looks like this on my computer after adding 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 upgrade GRUB 2 on Debian-based Linux distributions, all you need to do is run the following command:
5. Create (or edit if you already have)
/etc/initramfs-tools/conf.d/resume with the swap UUID and the resume offset.
Next we need to edit the file (or create it if it doesn’t exist).
/etc/initramfs-tools/conf.d/resume File with a text editor as root. Use the following command to open this file with the Nano command line editor:
sudo nano /etc/initramfs-tools/conf.d/resume
If you have a line in this file that starts with “RESUME”, edit that line, or if there is no such line (or the file is completely blank), add the line so that it looks like this:
To save the file and exit Nano, press
Ctrl +o, then
Enter, and then
Ctrl + x.
Example with the content of the
/etc/initramfs-tools/conf.d/resume File from my laptop:
6. Refresh initramfs.
Now you need to regenerate initramfs with this command:
sudo update-initramfs -c -k all
7. Restart your computer.
After restarting your computer, use the following command to put your computer to sleep:
sudo systemctl hibernate
For now, you will need to provide your password to go to sleep and there is no menu option to put your computer to sleep, so you will have to do all of this from the command line. To put your system to sleep 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 control panel)), see the next 2 steps.
To resume from hibernation, press the power button on your computer.
If you get an error message when you run this command that says the system could not be hibernated via logind: Not enough swap space to hibernate, then you may have overlooked the note at the beginning of the article where I Mention that it is best to have a paging file that is at least as large as your computer’s memory. This article explains how to switch from a swap partition to a swap file – you can also use these instructions to create a larger swap file.
You can check for errors related to the systemd Hibernate service by:
systemctl status systemd-hibernate.service
8. Optional. Allow hibernation without entering a password (required for the next step). In some desktop environments, this reactivates the “Hibernate” and “Hybrid Sleep” power menu options.
In order to put your computer to sleep, you will have to enter your password by default. 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.
The following command opens
/etc/polkit-1/localauthority/50-local.d/com.ubuntu.desktop.pkla as root using the 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).
In some desktop environments, you will then receive (and after logging out / logging in again) the power options hibernation and hybrid sleep. Here is an example with the Xfce power options after following this step:
However, this doesn’t work on some desktop environments that use Ubuntu, such as: B. with the GNOME Shell. In this case we need an additional step (see below).
that goes over AskUbuntu
9. Optional. Add a menu item for hibernate applications or use a hibernate button for the GNOME Shell.
If you’re using the GNOME Shell, you can use an extension that adds an option to put your computer to sleep in the system power menu on the top bar, called Sleep button.
Here is the Power Off / Logout GNOME Shell menu with this extension enabled:
If you’re not using the GNOME Shell, but your desktop environment doesn’t show a hibernate power option, or you don’t want to use the GNOME Shell extension mentioned above, you can create an entry in your application menu by creating a file called hibernate. desktop in
~/.local/share/applications/, e.g. open the Nano command line text editor with this file:
And in that 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 can now go to the application menu on your desktop and click on the
Hibernate Icon to put your system to sleep. For this to work you need to follow the optional step 9 from above to go to sleep without entering the password.
I should also clarify what about the Hybrid Sleep option. In addition to Sleep / Suspend (Suspend to RAM) and Hibernate (Suspend to Disk), there is also the Hybrid Sleep (also called Hybrid Suspend) option, which puts your computer into hibernation and then goes into hibernation after a certain period of time. You can set the hybrid sleep time between Suspend and Hibernation (along with other options) by using the
/etc/systemd/sleep.conf File and then restart your system. You can find more about this in the Arch Wiki.
You Might Like This: How to Enable Hardware Accelerated Video Decoding in Google Chrome, Brave, Vivaldi and Opera browsers on Debian, Ubuntu or Linux Mint