Setup Hibernation in Ubuntu

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.

CONSOLE
swapon --show
Click to expand and view more

If you already have a swap file, you will get an output similar to this.

CONSOLE
NAME      TYPE SIZE USED PRIO
/swapfile file   2G   0B   -2
Click to expand and view more

If you already have a swap file, remove the file, so that we can start fresh.

CONSOLE
sudo swapoff -a 
sudo rm /swapfile
Click to expand and view more

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.

CONSOLE
sudo dd if=/dev/zero of=/swapfile bs=1G count=32
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
Click to expand and view more

Open the following file.

CONSOLE
sudo nano /etc/fstab
Click to expand and view more

Check if there is already the following line.

CONSOLE
/swapfile none swap sw 0 0
Click to expand and view more

If there is not the line, add it with the following command.

CONSOLE
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab
Click to expand and view more

Run the following command and copy the UUID.

CONSOLE
sudo findmnt -no UUID -T /swapfile
Click to expand and view more

Then run this next command and copy the number that you see, without the dots at the end.

CONSOLE
sudo filefrag -v /swapfile |grep " 0:"| awk '{print $4}'
Click to expand and view more

Now open the default grub file.

CONSOLE
sudo nano /etc/default/grub 
Click to expand and view more

And change the following line by adding the UUID and the offset that you previously copied.

CONSOLE
GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXXX"
Click to expand and view more

Finally update grub

CONSOLE
sudo update-grub
Click to expand and view more

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.

CONSOLE
sudo nano /etc/polkit-1/rules.d/10-enable-hibernate.rules
Click to expand and view more

Add this content and save the file.

CONSOLE
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;
    }
});
Click to expand and view more

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

Start searching

Enter keywords to search articles

↑↓
ESC
⌘K Shortcut