Computer hibernate does not work in your Ubuntu? Here’s step by step complete guide shows how to enable this feature.
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 is tested and works in Ubuntu on default GNOME desktop. For other desktop environments, you need to replace gedit in following commands with your favorite text editor, or use the universal nano command line editor.
Check if hibernate works:
Hibernate does not work in my laptop since Ubuntu 20.04 (maybe, can’t remember it), though it works in Debian 11 bullseye.
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.
The command will turn off your computer/laptop. Next boot the machine again, and see if it restore your system status after login.
Enable Hibernate on Swap Partition:
So it does not work and just log into blank desktop? Try enable this feature via following steps.
Check Swap Area:
The swap area is used when the memory is getting full. It can also be used to save your system status via hibernate.
Open terminal (Ctrl+Alt+T) and run command tell the swap area:
swapon --show
If it outputs “/dev/sdax partition …” continue this step. Or if it says no swap or a file, go next step: ‘Enable Hibernate on Swap File‘
Find swap area UUID
In my case the swap is a partition: “/dev/sda3”. Now find the UUID (universally unique identifier) for the area via command:
blkid
In case it does not show the id for swap partition. You can alternatively check in “/etc/fstab” file. To do so, run command:
cat /etc/fstab |grep swap
Copy the UUID for /dev/sda3 in my case. It will be used in next step.
Resume from swap via Kernel parameter
By adding a Kernel parameter into boot menu entry enables the hibernate in my case in both HP and ASUS laptops.
You can do this either via “Grub Customizer -> General Settings” or by editing the Grub boot-loader configuration file.
Run command in terminal to open the config file:
sudo gedit /etc/default/grub
When it opens, add resume=UUID=xxxx (replace xxxx with the id you copied in previous step) as value of “GRUB_CMDLINE_LINUX_DEFAULT”.
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!
Enable Hibernate on Swap File:
This step is only for those who don’t have a swap partition, since a swap file can also be used for hibernation.
Create 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 fallocate -l 4G /swapfile
Replace 4G 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
Find the UUID & Offset:
Since the file is created on Ubuntu file-system. It can be located via root UUID as well as physical offset.
To see your Ubuntu partition, run command and find the one mounted on ‘/‘:
df -h
Then find its UUID via command:
blkid
In my case, it’s ‘/dev/sda5’ and UUID is marked in white background.
To find the physical offset for /swapfile, run command:
sudo filefrag -v /swapfile
Copy the start number under physical_offset. It’s 927744 in my case.
Resume on Swap file via Kernel parameter:
Finally edit the Grub configuration file (or use Grub-Customizer) via command:
sudo gedit /etc/default/grub
Then add resume=UUID=xxx resume_offset=xxx as value of “GRUB_CMDLINE_LINUX_DEFAULT”. Also replace ‘xxx’ with the id and/or offset value.
And update grub to apply change via command:
sudo update-grub
Regenerate initramfs:
Hibernate now works in my case after reboot. If 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=UUID=xxx resume_offset=xxx” (replace xxx with correct id) and save it.
After saving the file, regenerate initramfs via command:
sudo update-initramfs -c -k all
Finally reboot and test hibernate function again.
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.) Firstly, open terminal (Ctrl+Alt+T) and run command to create & edit a config file:
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’s now a hibernate option after log-out and back in.
2.) 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.
Moreover:
If everything goes well after this tutorial, you can get more, including hibernate when lid is closed, and automatic hibernate on system idle.