Are you struggling to locate the hibernate option on your Ubuntu 22.04 system?
Unlike sleep mode, which keeps the device powered on and saves your work in RAM, hibernation mode saves your work directly to the hard drive. As a result, when you power on the device from hibernation, it takes a bit longer to start up compared to sleep mode.
In this tutorial, we will show you step-by-step how to enable the hibernate option on your Ubuntu system.
Let’s dive right in!
What You’ll Need
Before we dive into the steps, here are the things you’ll need:
- A system running Ubuntu (22.04 or later)
- Access to the terminal for executing commands
- A text editor of your choice
Once you have those prerequisites, you can proceed with the steps below to enable hibernation on your Ubuntu machine.
How to Enable Hibernate on Ubuntu 22.04 LTS: Step-by-Step
Step 1: Check the Current Swap and Memory Size
On a Linux system, there is a particular space in your hard disk called the swap area. If your physical memory becomes full, the swap area is then used as a substitute to store your data.
To make your device hibernate, you need a swap area large enough to accommodate the data from the RAM. So you should have a swap area that’s equivalent to or larger than your RAM size.
- You can check the swap size of your system with the following command. The swap size in our case, as shown in the screenshot below, is 2 GB.
- Next, check the size of your memory with the following command. As you can see from the screenshot, the memory is almost 8 GB. Considering this, we will need a larger swap partition to accommodate hibernation. In the next step, we will proceed to create a new swap partition.
Step 2: Create a New Swap File to Enable Hibernate
Before proceeding, it is necessary to disable your current swap to avoid any potential conflicts. Follow the command below to disable the existing swap.
- Disable the current swap with this command:
sudo swapoff /swapfile
- After disabling the swap, increase its size using this command:
sudo dd if=/dev/zero of=/swapfile bs=1MB count=$((8*1024)) status=progress
Note: Ensure that you allocate an appropriate size based on your available RAM. It is recommended to assign a size equal to the capacity of your RAM. That’s because the data in the RAM will be saved in the swap space during hibernation. If it’s not as big as the RAM size, it won’t be able to hold all the data stored in the RAM. In this example, we assume our memory is 8 GB, so we set the count to 8 * 1024.
- Once you have determined the desired size, use the following command to grant read and write permissions exclusively to the owner:
sudo chmod 600 /swapfile
- Finally, create the swap space by formatting it using the following command. When the swapfile is formatted, you should see a similar output to the below screenshot:
sudo mkswap /swapfile
Step 3: Enable the New Swap Space on Ubuntu
Now that you have created the new swap area, you need to enable it to start using it. Follow the steps below:
- Enable the new swap file by running this command:
sudo swapon /swapfile
- To ensure the swap file was enabled, you can recheck its current size. Check the size of your new swap file and your RAM with the two commands below:
As you can notice, the size of your memory is 7.7 GB, and the newly created swap area is 7.6 GB. So the swap file and our available RAM has almost identical size.
Step 4: Ensure That Swap Is Enabled Upon Startup
After enabling the new swap space in the previous step, it’s important to configure your system to enable it automatically upon every startup. Follow the steps below to ensure the swap space is enabled during boot:
- Open the /etc/fstab file in a text editor. We will use the Nano text editor. To open the file in Nano, run this command:
sudo nano /etc/fstab
- Add the below line to the end of the file if it’s not already added:
/swapfile none swap sw 0 0
- Save the file by pressing the Ctrl + O keys. Then exit by pressing the Ctrl + X keys.
Step 5: Find the Filesystem UUID and the Physical Offset Number of the Swapfile
The Universal Unique Identifier (UUID) is an identifier for partitions that remain unchanged. The physical offset number refers to the location of the swap file within the filesystem. You will need to provide both if you want to wake up your GRUB bootloader from hibernation.
Follow the steps below to obtain and configure these values.
- Find the name of your main root filesystem with the below command. As you can see from the screenshot below, in our case, the name is /dev/sda2. Mark down the name that appears in your case. That will be your root filesystem name.
df -h /
- You can find the UUID of your filesystem with the following command. Take note that your UUID will appear different from ours. Make sure to save your UUID, as you will need it for later:
sudo blkid /dev/dsa2
- Now we need to find the physical offset number of the swap file. To find that, use this command:
sudo filefrag -v /swapfile | head --lines=10
- Grab the physical offset of the first row. If you look closely at the above screenshot, we’ve marked the required physical offset number in our case. After issuing the
filefragcommand, you will get many rows. Only get the offset number in the first row. Note that you will see a different number than us. Save the number somewhere for later use
Step 6: Configure GRUB to Resume Ubuntu from Hibernate
Now that we have both the UUID and the physical offset number, we are ready to configure GRUB so that it can resume from hibernation.
- Open the /etc/default/grub file in a text editor. To open it on Gedit, use this command:
sudo gedit /etc/default/grub
- On the tenth line, add the following text after
- In our case, it will look like the below screenshot. Notice that you need to write the text inside the double quotations.
- After adding the text, save the file by pressing the Ctrl +S keys.
- Since you made changes to the configuration file, you have to update GRUB, or else the changes won’t take effect. Update GRUB using this command:
This should update the GRUB configuration and make it ready to use.
Step 7: Enable Hibernate in Initramfs
Initramfs (initial RAM filesystem) is a temporary filesystem that is loaded into memory when your Linux system is in the early boot process. So we must enable hibernate in initramfs too.
- Create an initramfs configuration file on Nano with this command:
sudo nano /etc/initramfs-tools/conf.d/resume
- Add the below text in the newly created file:
- In our case, the text is as follows. You should expect a similar result to the screenshot below:
- Save the file and exit.
- To make the changes take effect, update the initramfs image using this command:
sudo update-initramfs -c -k all
Once updated, you will see the above output.
(Optional) How to Hibernate Ubuntu From the Command Line
So far, we’ve enabled hibernation mode on Ubuntu. Now it’s time to test it. If you want to go into hibernate mode on your Ubuntu device from your command line, run the below command:
sudo systemctl hibernate
If everything went well, your system will shut down. Once you turn it on, you will see all your works intact as they were before the shutdown.
Congratulations! You have successfully enabled hibernation on Ubuntu 22.04 LTS. We’ve covered all the necessary steps to enable hibernation and showed you how to send your device to hibernation mode.
If this guide helped you, please share it.