Hibernation is a power-saving mode that saves the entire system state to disk and fully shuts down the device. When the computer is started again, everything is restored exactly as it was before. I use hibernation because I often work with many open applications across different workspaces and virtual desktops, and it allows me to keep my window layout and organization exactly the way I set it up without having to rebuild my workflow each time.
Hibernation in Ubuntu / Kubuntu
For more than ten years, I have used Ubuntu partly as my daily driver. Later, I switched to Kubuntu. This gives me the advantages of the Ubuntu base system, including its large community and stability, combined with the customizability of the KDE Plasma desktop environment. With this system, I am satisfied.
However, one problem is that hibernation is not set up by default. The reasons for this decision can be many. Hibernation requires as much storage as you have RAM, which can be problematic for systems with limited storage. It can also cause issues if the hardware does not fully support it. Therefore, most Linux distributions do not enable it by default.
But if you follow this guide, you will have hibernation enabled in just a few minutes.
Setting up Hibernation
First check if you already have a swap file.
swapon --show
If you already have a swap file, you will get an output similar to this.
NAME TYPE SIZE USED PRIO
/swapfile file 2G 0B -2
If you already have a swap file, remove the file, so that we can start fresh.
sudo swapoff -a
sudo rm /swapfile
Create a new swapfile. For count enter the same amount of RAM that your system has. Enable the system to use the new swapfile afterwards.
sudo dd if=/dev/zero of=/swapfile bs=1G count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Open the following file.
sudo nano /etc/fstab
Check if there is already the following line.
/swapfile none swap sw 0 0
If there is not the line, add it with the following command.
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Run the following command and copy the UUID.
sudo findmnt -no UUID -T /swapfile
Then run this next command and copy the number that you see, without the dots at the end.
sudo filefrag -v /swapfile |grep " 0:"| awk '{print $4}'
Now open the default grub file.
sudo nano /etc/default/grub
And change the following line by adding the UUID and the offset that you previously copied.
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXXX"
Finally update grub
sudo update-grub
At this point you did everything that is needed to enable hibernation. You could already test it by running the command systemctl hibernate. But you will notice, that in the power options you will not see an option to hibernate your system. For this to work, we still need to make a small change. Update the following file.
sudo nano /etc/polkit-1/rules.d/10-enable-hibernate.rules
Add this content and save the file.
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;
}
});
You now only need to restart your system and then you will be able to hibernate your system in addition to turning it off or going into sleep. Now you could also open the power settings and configure your system to hibernate whenever you close your lid. I turn on this option on my systems so that whenever I need to stop working I can just close my notebook and whenever I open it again, it starts automatically and everything is as I left it. And because we store the state in the disk and not in the memory, it is really turned off and does not consume battery in contrast to sleep mode.
Comments