This article explains how to use systemd to enable hibernation when using swap files on recent Ubuntu releases (at least from 18.04 onwards, it is the default setting for Ubuntu).
My laptop consumes a lot of power when I sleep.I tried some solutions/workarounds, for example this (Except for me, the problem is not that the laptop loses half of its energy overnight, but that it loses all its energy within a few hours), but to no avail. So I decided to enable the hibernation option (suspend to disk) and use it instead of the default sleep option (suspend to RAM) because it uses less power, so the laptop battery will not run out when not in use, although it takes a bit The time to wake up from hibernation is longer than the time to wake up from sleep. So here is how to do it.
The instructions below may apply to other Linux distributions, but I only tested it on Ubuntu (it should work in exactly the same way on any Debian -based and Ubuntu-based Linux distribution, including Pop!_OS, etc.). On non-Debian-based Linux distributions, you may need to use different commands to update GRUB 2 (step 4) and regenerate initramfs (step 6), because these are Debian-specific commands.
It is also important to note that hibernation in Ubuntu is disabled because it has problems on some computers, so it may not be suitable for everyone.A little chatter About using version 20.10 to revisit hibernation on Ubuntu, but it is still disabled by default.
It is worth noting that you need a swap file that is at least as large as your computer’s RAM (if your computer’s swap file is smaller than the system RAM, hibernation may or may not succeed). This article explains how to convert from a swap partition to a swap file-you can also use these instructions to create a larger swap file.
In addition, if you specifically encrypt the exchange, these instructions will not work. Since the exchange on my laptop is not encrypted, I cannot try this operation using encrypted exchange, so the following instructions only apply to unencrypted exchange files. But you should know that if you already have an encrypted root file system, all files on it-including swap files-will also be encrypted. The following instructions are valid in this case.
To find out the size of the computer swap and whether it uses swap files or partitions (you need to know this later), you can use the following command:
This is an example of output from my laptop:
$ cat /proc/swaps Filename Type Size Used Priority /swapfile file 8388604 3213700 -2
As you can see, in
Type it says
file , So my computer is using a swap file (its path is
/swapfile ) Whose size is
8388604 Kilobytes (8,3 GB).
By default, Ubuntu will use the swap file for a period of time (at least starting with Ubuntu 18.04), so the instructions below will assume that you are using it. If you use a swap partition, please use the UUID of the swap partition instead of the UUID of the partition where the swap file is located in the following instructions, and do not add the swap file offset (
resume_offset Parameters) anywhere. These are the differences between using swap partitions and swap file hibernation.
You may also be interested: 3 tools to display Linux laptop battery information from the command line
How to enable hibernation on Ubuntu (when using swap file)
1. Find the UUID of the partition where the swap file is located.
We need to add it to the GRUB file and the initramfs recovery file.
To find out the UUID of the partition where the swap file is located, you can use the following command:
findmnt -no UUID -T /swapfile
/swapfile Is the path of the swap file, which may be different on your computer; use the exact path returned
cat /proc/swaps Order).
Example from my computer (with output):
$ findmnt -no UUID -T /swapfile4a59c6a7-ca54-4e24-a362-3eac83bfe226
2. Find out the offset of the exchange file.
Next, we need to find out the offset of the swap file. Like UUID, we need to add it 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 number block in the “physical_offset” column is the swap file offset you will need later. Only the number is copied, not the dot behind the number block.
For example, in my case, this is the output
sudo filefrag -v /swapfile Order:
$ 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 digital block
/etc/default/grub The file can be started with the resume and resume offset kernel parameters.
It is very important to make sure that you edit the file correctly, otherwise your computer may not start!
/etc/default/grub Open the file with a text editor as root. For example, using the following command, we will use the Nano command line text editor to open it:
sudo nano /etc/default/grub
At the end of this file
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 these values with the UUID you obtained in step 1 and the swap recovery offset you obtained in step 2.
To save the file and exit Nano, press
Ctrl + o , Then
Enter , and then
Ctrl + x .
example.How is this
GRUB_CMDLINE_LINUX_DEFAULT After adding the swap UUID and swap offset, the line appears on my computer:
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 a Debian-based Linux distribution, you only need to run the following command:
5. Create (or edit, if it already exists)
/etc/initramfs-tools/conf.d/resume Use swap UUID and restore offset.
Next, we need to edit (create if it does not exist)
/etc/initramfs-tools/conf.d/resume Use a text editor for the file as root. Use the following command to open this file through the Nano command line editor:
sudo nano /etc/initramfs-tools/conf.d/resume
In this file, if you have a line that starts with “RESUME”, edit the line, or if there is no such line (or the file is completely empty), 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 .
/etc/initramfs-tools/conf.d/resume Files from my laptop:
6. Regenerate the initramfs.
Now you need to regenerate the initramfs using the following command:
sudo update-initramfs -c -k all
7. Restart the computer.
After restarting, use the following command to hibernate your computer:
sudo systemctl hibernate
Currently, you need to provide a password to hibernate, and there is no menu option to put your computer to hibernation, so you must perform all operations from the command line. In order to be able to hibernate your system without entering a password (and you can choose to hibernate the computer in the power options of the desktop (for example, in the applet on the panel)), please refer to the next 2 steps.
To resume from hibernation, press the power button on the computer.
If you receive an error message when running this command saying “Cannot hibernate the system via logind: Hibernation swap space is insufficient”, then you may have missed the notification near the top of the article, in which I mentioned that it is best to have a swap file at least with Your computer’s RAM is the same size. This article explains how to convert 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 using the following command:
systemctl status systemd-hibernate.service
8. Optional. Allow hibernation without entering a password (required in the next step). In some desktop environments, this will re-enable the hibernation and hybrid sleep power menu options.
By default, to put the computer to sleep, you need to enter a password.To allow sleep without password, please create/open
/etc/polkit-1/localauthority/10-vendor.d/com.ubuntu.desktop.pkla Use a text editor as root.
Use the following command to open
/etc/polkit-1/localauthority/50-local.d/com.ubuntu.desktop.pkla Use the Nano command line editor as root:
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, after doing this (and after logging out/logging back in), you will get hibernation and hybrid sleep power options. The following is an example of Xfce power options after performing this step:
However, this does not work in some desktop environments that use Ubuntu, such as GNOME Shell. In this case, we need an extra step (see below).
This is through Ask Ubuntu
9. Optional. Add a hibernation application menu entry or use GNOME Shell’s hibernation button.
If you use GNOME Shell, you can use an extension that adds an option to hibernate your computer in the system power menu in the top bar, called Sleep button .
This is the shutdown/logout GNOME Shell menu with this extension enabled:
If you do not use GNOME Shell, but your desktop environment does not display the Hibernate power option, or you do not want to use the GNOME Shell extension above, you can create a file named hibernate.desktop in your application menu by creating an entry
~/.local/share/applications/ For example, use this file to open the Nano command line text editor:
And add the following content to this file:
[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 of the desktop and click
Hibernate The icon puts your system to sleep. To do this, you need to follow the optional step 9 above to be able to sleep without entering a password.
I should also clarify the content of the hybrid sleep option. In addition to sleep/suspend (suspend to RAM) and hibernate (suspend to disk), there is also a hybrid sleep (also called hybrid suspend) option, which first puts your computer to sleep and then transitions to hibernation after a given time .You can edit the mixed sleep time between suspend and hibernate (and other options)
/etc/systemd/sleep.conf File, and then restart the system.For more information on this, see Arch Wiki .
You might like: How to enable hardware accelerated video decoding in Google Chrome, Brave, Vivaldi and Opera browsers on Debian, Ubuntu or Linux Mint