Archives For November 30, 1999

Computer hibernate does not work in your Ubuntu? Here’s step by step complete guide shows how to enable this feature in Ubuntu 24.04 and Ubuntu 22.04 LTS.

Similar to suspend, hibernate can be used to save your system work. It moves the content from RAM memory into swap area in hard disk, then shutdown your machine completely. Next time you boot the computer, it restores the system status exactly as it was.

NOTE:This tutorial now is tested and works on my Ubuntu 22.04 and Ubuntu 24.04 Laptop with default GNOME. It may NOT work if you’ve used Grub-Customizer to edit the boot-menu!!

For other desktop environments, you need to replace gnome-text-editor or gedit in following commands with your favorite text editor, or use the universal nano command line editor that works in most desktops (use Ctrl+S to save, Ctrl+X to exit).

Step 1: Check if hibernate works:

Hibernate does not work in my laptop by default since Ubuntu 20.04 (maybe, can’t remember it), though it works in Debian 11 bullseye.

NOTE: The command below won’t work for secure boot!! You have to disable it in BIOS/EFI for using hibernate. With secure boot, you’ll get error:

“Failed to hibernate system via logind: Sleep verb “hibernate” not supported”.

To verify if the functions works, open terminal (Ctrl+Alt+T) and run command:

systemctl hibernate

Save you work before running the command!! Then type user password when it asks. In some case, you may need to run sudo systemctl hibernate instead.

The command will turn off your computer/laptop. Next boot the machine again, and see if it restore your system status after login.

Step 2: Check the ID of Swap Area & Create if NOT exist.

So, if it does NOT restore your desktop to previous status after reboot in step 1, then you may try following this tutorial to enable this feature.

Hibernate saves data into swap area. You need to first check if you have a swap and what’s the UUID.

a.) Check Swap Area:

First, press Ctrl+Alt+T on keyboard to open terminal. When it opens, run command:

swapon --show

The command will output ‘/dev/sdaX’ or ‘/dev/nvme0n1px’ or similar device if you have a Swap volume/partition mounted. Or, it outputs a file /swap.img in my case in Ubuntu 24.04. If you don’t have any swap area, then it says no swap or a file.

b.) Create Swap File, if you don’t have Swap area:

ONLY do this step when you have neither a swap partition nor swap file!

To create a file for swap if you don’t have it, run following commands one by one:

a.) Pre-allocate a disk place for a file via command:

sudo dd if=/dev/zero of=/swapfile count=8192 bs=1MiB

Replace 8192 MB (8G) to your desired size. As large as RAM is recommended for swap.

b.) Change the file permission to only read & write for root:

sudo chmod 600 /swapfile

c.) Set the file as swap area:

sudo mkswap /swapfile

d.) Finally enable it by running command:

sudo swapon /swapfile

e.) And write it into ‘/etc/fstab’, so Ubuntu will mount it automatically on startup.

echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

c.) Find swap Name and offset

For Swap Partition. You just need to get the partition name by running command:

swapon --show

In the terminal output, just copy the partition name. It’s usually /dev/sdaX for sata, or /dev/nvme0n1px for NVME.

For Swap File. Just use the Ubuntu system partition name, plus, offset number instead.

To get the system partition name, run the command below, and check the one mounted on “/”:

df -h

Then, you need to run one more command to get the offset of the Swap file (replace ‘/swapfile’ in command according to the filename. It’s ‘/swap.img’ in my case in 24.04):

sudo filefrag -v /swapfile

Copy the start number under physical_offset. It’s 3606528 in my case.

Step 3: Enable Hibernate by adding Kernel Parameter

After step 2, you should got either UUID of Swap partition, or UUID + offset of Swap file. And, here you can use them by adding a kernel parameter in Grub boot config file.

To do so, press Ctrl+Alt+T to open terminal, and run command to edit the config file:

sudo gnome-text-editor /etc/default/grub

For Ubuntu 22.04 and earlier, replace gnome-text-editor with gedit. For non-GNOME desktop, use other text editor or nano that works for all.

For Swap partition, add resume=/dev/xxx (do replace “/dev/xxx” to yours Swap partition name) as value of “GRUB_CMDLINE_LINUX_DEFAULT”.

For Swap file, add resume=/dev/xxx resume_offset=xxx (also replace xxxx with the partition name and offset number) instead for value of “GRUB_CMDLINE_LINUX_DEFAULT”.

Finally, save the file and update the Grub via command:

sudo update-grub

Finally reboot your computer, open a few app windows, and run systemctl hibernate command to test if it works now!

UPDATE for Ubuntu 24.04 with /swap.img file:

For hibernation with default /swap.img file, you may got “Call to Hibernate failed: Invalid argument” error when running the hibernate command in Ubuntu 24.04, due to this bug.

For choice, you may create and use swap partition instead, then write into /etc/fstab file, and also disable (add # at the beginning) the “/swap.img …” line.

If you insist on using the default /swap.img file, set MAJ:MIN to /sys/power/resume works for me.

1. First, open terminal (Ctrl+Alt+T) and run command lsblk to get the “MAJ:MIN” for the root permission. In my case, it’s 259:6.

2. Next, run command to set the “MAJ:MIN” value (259:6 in my case) you got to /sys/power/resume:

sudo sh -c "echo 259:6 > /sys/power/resume"

3. Finally, systemctl hibernate works even without sudo update-initramfs -c -k all.

4. /sys/power/resume will automatically reset its content to 0:0 after reboot! Thanks to @Vladimir, you may fix the issue by running command below to create & edit a config file:

sudo nano /etc/tmpfiles.d/hibernation_resume.conf

Write the lines below (replace 259:6) into it. Finally, press Ctrl+S to save and Ctrl+X to exit.

#    Path                   Mode UID  GID  Age Argument
w    /sys/power/resume       -    -    -    -   259:6

For choice, you may add one more line below (replace the value number accordingly). According to the archlinux wiki, decrease the value of /sys/power/image_size can make the suspend image as small as possible (for swap smaller than RAM, run free command to tell), and smaller values may drastically increase the speed of resuming a hibernating system.

w /sys/power/image_size - - - - 8036093952

Regenerate initramfs if hibernate to Swap file (For Ubuntu 22.04 & earlier):

Hibernate now works in my case after reboot. If it’s not working for you, try regenerating initramfs via a new rule.

Firstly, open terminal (Ctrl+Alt+T) and run command to create a config file:

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

Then paste the line “RESUME=/dev/xxx resume_offset=xxx” (replace xxx with correct partition name and offset number) and save it.

After saving the file, regenerate initramfs via command:

sudo update-initramfs -c -k all

Finally reboot and test hibernate function again.

Step 4: Enable Hibernate option in Power-Off Menu:

After enabled the function, you can now add a menu option into the system tray shutdown menu.

1.) First, open terminal (Ctrl+Alt+T) and run command to create the config directory, that’s not exist by default in Ubuntu 24.04:

sudo mkdir -p /etc/polkit-1/localauthority/50-local.d

2.) Thanks to @Joerg, Ubuntu 23.10 and Ubuntu 24.04 users need to install polkitd-plka package first:

sudo apt install polkitd-pkla

3.) Then, run command to create & edit a config file (replace gedit with gnome-text-editor for 24.04):

sudo gedit /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla

When the file opens, paste following lines and save it.

[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

For non-Gnome desktop, e.g., XUbuntu and Ubuntu MATE, there should be a hibernate option after log-out and back in. For KDE user, try this one to create /etc/polkit-1/rules.d/10-enable-hibernate.rules file and add following lines:

polkit.addRule(function(action, subject) {
    if (action.id == "org.freedesktop.login1.hibernate" ||
        action.id == "org.freedesktop.login1.hibernate-multiple-sessions" ||
        action.id == "org.freedesktop.upower.hibernate" ||
        action.id == "org.freedesktop.login1.handle-hibernate-key" ||
        action.id == "org.freedesktop.login1.hibernate-ignore-inhibit")
    {
        return polkit.Result.YES;
    }
});

2.) Enable Hibernate option in system menu for the default GNOME Desktop.

For default GNOME desktop, go to the link button below and install the extension for the menu option:

For those first time installing Gnome extension, you need to run command to install “chrome-gnome-shell” package first:

sudo apt install chrome-gnome-shell

And click the “click here to install browser extension” link in the extension page and refresh the page if you don’t see the toggle icon.

Or, search for and install “Extension Manager” from Ubuntu Software, and use the tool to search and install that extension:

After installed the extension, go back “Installed” tab in Extension Manager window, and click configure the extension for showing/hiding power-off menu options (May need log out and back in).

Moreover:

If everything goes well after this tutorial, you can get more, including hibernate when lid is closed, and automatic hibernate on system idle.