ubuntu-hibernation
Language: Shell | Stars: 0
Ubuntu Hibernation Enabler Script
This repository contains a shell script, setup_hibernation.sh, designed to automate the process of enabling hibernation on Ubuntu and its derivatives (like Linux Mint, Pop!_OS, etc.). It configures the system to use a dedicated swap partition for storing the contents of RAM when hibernating.
Overview
Hibernation saves the current state of your system (open applications, files, etc.) to your disk and then completely powers off the computer. When you turn it back on, your session is restored exactly as you left it. This is particularly useful for saving battery on laptops without losing your work.
This script makes the setup process easier by performing the necessary system checks and configuration changes automatically.
❗ DISCLAIMER ❗
This script modifies critical system files, specifically your GRUB (bootloader) configuration. While it includes safety checks and automatically creates a backup of your GRUB settings, there is always a small risk of issues.
Please back up your important data before proceeding. The author is not responsible for any data loss or system damage.
Prerequisites
Before you use this script, please ensure you meet the following requirements:
- Ubuntu-based Distribution: The script is designed for Ubuntu and its derivatives.
- Root Access: You must run the script with
sudoprivileges. - Secure Boot Disabled: Hibernation is incompatible with Secure Boot on most Ubuntu systems. You must disable Secure Boot in your computer's UEFI/BIOS settings. The script will check for this and stop if it's enabled.
- A Swap Partition: You need an active swap partition (not a swap file).
- Sufficient Swap Size: The swap partition must be at least as large as your system's RAM. If you have 16 GB of RAM, your swap partition needs to be 16 GB or larger.
Step 1: Preparing a Swap Partition
If you don't have a suitable swap partition, you'll need to create one. The easiest way is to use a graphical tool like GParted from a live USB. Alternatively, you can use command-line tools if you have unallocated space on your drive.
Using Command-Line Tools (fdisk)
-
Identify your disk: Find the name of the disk you want to add a partition to.
sudo lsblkYour main drive is likely
/dev/sdaor/dev/nvme0n1. -
Create a new partition: Replace
/dev/sdXwith your disk name.sudo fdisk /dev/sdXInside
fdisk:- Press
nto create a new partition. - Choose the partition type (primary is fine).
- Accept the default partition number and first sector.
- For the last sector, specify the size, e.g.,
+16Gfor 16 gigabytes. - Press
tto change the partition type. - Enter
19to select the "Linux swap" type. - Press
wto write the changes to the disk and exit.
- Press
-
Format and activate the swap partition: Replace
/dev/sdXNwith the new partition's name (e.g.,/dev/sda3).# Format the partition as swap space sudo mkswap /dev/sdXN # Turn on the swap partition sudo swapon /dev/sdXN -
Make the swap partition permanent: To ensure the system uses the swap partition after every reboot, add it to
/etc/fstab.- First, get the UUID of the new partition:
sudo blkid /dev/sdXN - Open
/etc/fstabwith a text editor:sudo nano /etc/fstab - Add the following line at the end, replacing
YOUR_UUID_HEREwith the UUID you copied:UUID=YOUR_UUID_HERE none swap sw 0 0 - Save and close the file.
- First, get the UUID of the new partition:
Step 2: Using the Script
Once your swap partition is ready, you can run the setup script.
-
Clone or Download the Script:
git clone https://zb-ss/ubuntu-hibernation.git cd ubuntu-hibernationAlternatively, just download the
setup_hibernation.shfile. -
Make the Script Executable: In the terminal, run the following command to give the script permission to execute:
chmod +x setup_hibernation.sh -
Run the Script: Execute the script with
sudo:sudo ./setup_hibernation.shThe script will guide you through the process, performing checks and asking for confirmation before making changes.
Step 3: Testing Hibernation
After the script finishes, it will prompt you to reboot. A reboot is necessary for the changes to take effect.
- Reboot your computer.
- Once you've logged back in, open a few applications.
- Open a terminal and run the test command:
Your computer should save its state and power off completely.sudo systemctl hibernate - Turn your computer back on. It should boot up and restore your session exactly as you left it.
Troubleshooting
- System Wakes Immediately After Hibernating: This can be caused by various issues, often related to hardware drivers (especially NVIDIA) or kernel versions. Searching online for your specific hardware and Ubuntu version may provide a solution.
- How to Undo Changes: The script creates a backup of your GRUB configuration in
/etc/default/with a timestamp, likegrub.bak.2025-08-26-12:34:56. To revert the changes, you can restore this file:
You should also delete the# Move the modified file sudo mv /etc/default/grub /etc/default/grub.modified # Restore the backup (use the actual backup filename) sudo mv /etc/default/grub.bak.YYYY-MM-DD-HH:MM:SS /etc/default/grub # Update GRUB again sudo update-grubresumefile created by the script:
Then reboot.sudo rm /etc/initramfs-tools/conf.d/resume sudo update-initramfs -u -k all
License
This project is licensed under the MIT License. See the LICENSE file for details.