How to Enable Hibernate in Ubuntu 22.04 LTS

Hibernate is a method of shutting down your computer while keeping the user session saved on a hard disk or solid-state drive (SSD). This allows you to restore your session when the computer is powered back on, saving power compared to the sleep mode which continues to consume battery. This comprehensive guide will walk you through enabling the Hibernate power option in Ubuntu 22.04 LTS, ensuring you have a fully functional and energy-efficient system.

Hibernate stores the data from the memory (RAM) of a running computer onto the disk and then shuts it down. Upon restarting, the data is read back from the disk to the memory, and the user session is restored. Unlike sleep mode, which still draws power to keep the session in RAM, hibernate does not use any power, making it ideal for longer periods of inactivity.

Advantages of Hibernate Over Sleep

  • Power Savings: Hibernate saves more power compared to sleep because the computer is completely powered off.
  • Session Persistence: Even if the battery runs out, your session can be restored from the disk.

When to Use Hibernate?

Hibernate is particularly useful when you do not plan to use your computer for an extended period but want to resume your work exactly where you left off. It is also beneficial when traveling with a laptop, as it conserves battery life.

Checking the Current Swap and Memory Size

For hibernate to function correctly, the swap size of your Ubuntu machine must be equal to or greater than the installed memory (RAM). This ensures that all the data in RAM can be saved to the swap space during hibernation.

Step 1: Check Swap Size

Use the following command to check the current swap size on your system:

swapon --show

This command lists all active swap areas.

Step 2: Check Memory Size

Next, check the amount of installed memory (RAM) with the command:

free -h

The -h flag makes the output human-readable, displaying sizes in a more understandable format (e.g., MB, GB).

If the swap size is smaller than your physical memory, you will need to increase it.

Creating a New Swap File to Enable Hibernate

If your current swap size is insufficient, you will need to create a new swap file that matches or exceeds the size of your installed RAM.

Step 1: Disable Current Swap

First, disable the current swap file:

sudo swapoff /swapfile

Step 2: Create a New Swap File

Create a new swap file with the desired size (e.g., 4GB). Adjust the size according to your installed RAM.

sudo dd if=/dev/zero of=/swapfile bs=1M count=4096 status=progress

In this command:

  • if=/dev/zero specifies the input file as a source of zero bytes.
  • of=/swapfile specifies the output file (swap file location).
  • bs=1M sets the block size to 1 Megabyte.
  • count=4096 sets the number of blocks (4GB in this case).
  • status=progress shows the progress of the command.

Step 3: Set Swap File Permissions

Set the correct permissions to ensure only the root user can read and write to the swap file:

sudo chmod 600 /swapfile

Step 4: Format the Swap File

Format the file to swap:

sudo mkswap /swapfile

Step 5: Enable the New Swap File

Enable the newly created swap file:

sudo swapon /swapfile

Step 6: Verify the Swap File

Verify that the new swap file is active and the swap size is correct:

swapon --show
free -h

Ensuring Swap is Enabled on Startup

To ensure that the swap file is enabled upon system startup, it must be added to the /etc/fstab file.

Step 1: Open /etc/fstab

Edit the /etc/fstab file with a text editor such as gedit:

sudo gedit /etc/fstab

Step 2: Add Swap File Entry

Add the following line to the file:

/swapfile none swap sw 0 0

Step 3: Save and Close

Save the file and close the text editor.

Finding the Filesystem UUID and the Physical Offset Number of the Swapfile

To configure the system to resume from hibernation, you need to find the UUID of the filesystem where the swap file is located and the physical offset number of the swap file.

Step 1: Find Physical Offset

Find the physical offset number of the swap file:

sudo filefrag -v /swapfile | head -n 1

The output will display the physical offset number. Note this number as it is required for the next steps.

Step 2: Identify Root Filesystem

Identify the root filesystem device:

df -h /

This command shows the filesystem details of the root directory.

Step 3: Get UUID of Root Filesystem

Retrieve the UUID of the root filesystem:

sudo blkid /dev/sda1

Replace /dev/sda1 with your root filesystem device name.

Configuring GRUB to Resume Ubuntu from Hibernate

To enable hibernation, you must configure the GRUB bootloader to resume from hibernation.

Step 1: Open GRUB Configuration

Open the GRUB configuration file:

sudo gedit /etc/default/grub

Step 2: Edit GRUB_CMDLINE_LINUX_DEFAULT

Add the following parameters to the GRUB_CMDLINE_LINUX_DEFAULT line:

resume=UUID=<your-root-filesystem-UUID> resume_offset=<swapfile_physical_offset>

Replace <your-root-filesystem-UUID> with the UUID of your root filesystem and <swapfile_physical_offset> with the physical offset of the swap file.

Step 3: Update GRUB Configuration

Update the GRUB configuration:

sudo update-grub

Enabling Hibernate in Initramfs

Next, you need to configure the initramfs to enable hibernation.

Step 1: Create Initramfs Configuration File

Create a new initramfs configuration file:

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

Step 2: Add Resume Configuration

Add the following line to the file:

RESUME=UUID=<your-root-filesystem-UUID> resume_offset=<swapfile_physical_offset>

Again, replace <your-root-filesystem-UUID> and <swapfile_physical_offset> with the appropriate values.

Step 3: Update Initramfs

Update the initramfs:

sudo update-initramfs -c -k all

Affordable VPS Hosting With Dracula Servers

Looking for reliable and budget-friendly Virtual Private Server (VPS) hosting? Look no further than Dracula Servers. Dracula Servers offers a range of VPS hosting plans tailored to meet diverse needs. With competitive pricing, robust performance, and a user-friendly interface, it’s an excellent choice for individuals and businesses alike.

Explore the Dracula Servers website to discover hosting solutions that align with your requirements and take your online presence to new heights with their affordable and efficient VPS hosting services.

Visit Dracula Servers and experience reliable VPS hosting without breaking the bank.

Adding the Hibernate Option in the System Tray Power Off/Log Out Menu

To make hibernation easily accessible, you can add the hibernate option to the system tray power off/log out menu.

Step 1: Create Policy File

Create a new policy file:

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

Step 2: Add the Following Configuration

Add the following lines to the file:

[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.hibernate-multiple-sessions;org.freedesktop.login1.hibernate-ignore-inhibit
ResultActive=yes

Step 3: Install GNOME Extension Manager

Update the package list and install the GNOME Extension Manager:

sudo apt update
sudo apt install gnome-shell-extension-manager

Step 4: Open Extension Manager and Install Hibernate Extension

  1. Open the GNOME Extension Manager from the application menu.
  2. Navigate to the Browse Section: Search for “hibernate”.
  3. Install the Hibernate Status Button Extension.

Restarting Ubuntu for the Changes to Take Effect

To apply all the changes, restart your Ubuntu machine:

sudo reboot

Hibernating Ubuntu from the Command Line

To hibernate your system from the command line, use the following command:

sudo systemctl hibernate

Hibernating Ubuntu from the System Tray Power Off Menu

To hibernate your system using the graphical user interface:

  1. Access the System Tray.
  2. Select ” Power Off/Log Out”.
  3. Click on “Hibernate”.

Troubleshooting Hibernate Issues

While enabling hibernate, you might encounter some issues. Here are some common problems and their solutions:

Secure Boot

In some systems, hibernate may not work if Secure Boot is enabled. Secure Boot is a security feature in your computer’s BIOS that ensures only trusted software is loaded at boot time.

Step 1: Check Secure Boot Status

To check if Secure Boot is enabled, restart your computer and enter the BIOS/UEFI settings. This can usually be done by pressing a key like F2F10F12Del, or Esc during the boot process (the exact key depends on your system manufacturer).

Step 2: Disable Secure Boot

If Secure Boot is enabled, disable it from the BIOS/UEFI settings. The option is usually found under the “Boot” or “Security” tab, but it can vary based on the BIOS/UEFI version and manufacturer.

Swap File Not Properly Configured

If hibernate doesn’t work, it could be due to incorrect swap file configuration. Ensure that the swap file size is correctly set and that the UUID and physical offset are correctly specified in the GRUB and initramfs configurations.

Step 1: Verify Swap File Configuration

Run the following commands to ensure the swap file is configured correctly:

sudo swapon --show
sudo filefrag -v /swapfile | head -n 1
sudo blkid /dev/sda1

Replace /dev/sda1 with your root filesystem device name. Make sure the UUID and physical offset match the values in your GRUB and initramfs configurations.

Step 2: Check GRUB Configuration

Ensure that the resume parameters are correctly added to the GRUB_CMDLINE_LINUX_DEFAULT line in /etc/default/grub:

sudo gedit /etc/default/grub

Look for:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=<your-root-filesystem-UUID> resume_offset=<swapfile_physical_offset>"

Update GRUB if needed:

sudo update-grub

Step 3: Check Initramfs Configuration

Verify the initramfs configuration file:

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

Ensure it contains:

RESUME=UUID=<your-root-filesystem-UUID> resume_offset=<swapfile_physical_offset>

Update initramfs if needed:

sudo update-initramfs -c -k all

Hardware Compatibility

Some hardware configurations might not support hibernate due to drivers or firmware issues. Check the Ubuntu hardware compatibility list or forums to see if your hardware is known to have issues with hibernation.

Step 1: Update Drivers and Firmware

Ensure all your system drivers and firmware are up to date. Use the following commands to update your system:

sudo apt update
sudo apt upgrade

Also, check your hardware manufacturer’s website for any firmware updates.

Step 2: Test Hibernation with Minimal Peripherals

Disconnect all unnecessary peripherals (external drives, printers, etc.) and test hibernation. Sometimes, peripherals can interfere with the hibernation process.

Logs and Diagnostics

Examine system logs to diagnose hibernation issues. The logs can provide insights into what might be going wrong.

Step 1: Check System Logs

Use the following command to view system logs:

journalctl -xe

Look for any errors or warnings related to hibernation.

Step 2: Use dmesg for Kernel Messages

The dmesg command displays kernel-related messages, which can help diagnose hardware issues:

dmesg | grep -i hibernate

Hibernate from Live CD

As a last resort, you can try hibernating from a live CD to see if it’s a software issue with your installed system.

Step 1: Boot from Live CD

Boot your system from an Ubuntu live CD or USB.

Step 2: Configure Swap and Hibernate

Follow the same steps to configure swap and hibernate in the live environment. If hibernation works here, it indicates a configuration issue with your installed system rather than a hardware problem.

Conclusion

Enabling hibernate in Ubuntu 22.04 LTS can significantly improve your system’s power efficiency by allowing you to save your session and power off the computer completely. This guide walked you through checking and configuring swap space, updating GRUB and initramfs configurations, and adding hibernation options to the system tray. Additionally, we covered common troubleshooting steps to resolve hibernation issues.

By following these steps, you can enjoy the benefits of hibernation, ensuring that your work is saved and your system remains energy efficient. Whether you are a casual user or a power user, enabling hibernate can enhance your Ubuntu experience, providing a seamless way to manage your sessions and conserve power.

Check out More Linux Tutorials Here!

Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
× Dracula Servers

Subscribe to DraculaHosting and get exclusive content and discounts on VPS services.