How to Enable Hibernation on Ubuntu with a Swap File

Hibernation saves the contents of your computer’s memory to swap, then powers the machine off completely. When Ubuntu starts again, it reloads that saved session, allowing you to continue with open applications and documents. This differs from suspend, which keeps memory powered and uses a small amount of electricity while the computer sleeps.

Ubuntu commonly uses a swap file rather than a separate swap partition. That arrangement is convenient, but hibernation needs extra information so the Linux kernel can find the saved memory image. You must check the swap file, identify its physical offset on the disk, add resume parameters to the boot configuration, and test the result before relying on it.

Why Hibernation Needs Extra Setup

During hibernation, Ubuntu writes RAM contents into swap. At the next boot, the kernel needs two details: the storage device containing swap and the location of the swap file’s first physical block. A normal swap-on command does not provide enough information for the early boot process to restore the session automatically.

A swap partition has a straightforward location, so hibernation is often simpler when one is used. With a swap file, the file’s logical path, such as /swapfile, does not directly identify its physical position. The resume_offset value bridges that gap.

The swap file also needs enough capacity. A practical starting point is a size at least as large as installed RAM, with additional room useful for systems running virtual machines, browsers with many tabs, or large creative applications. Hibernation can still fail if the saved memory image does not fit, even when ordinary swap activity works correctly.

Check Hardware and Current Swap

Before changing anything, check your Ubuntu release, memory, active swap, and filesystem:

lsb_release -ds
free -h
swapon --show
findmnt -no FSTYPE /

The swapon --show command might display /swapfile, /swap.img, a swap partition, or a zram device. Ubuntu installations vary, and some newer setups use compressed zram in addition to disk-backed swap. Zram is useful for reducing disk activity, but it cannot normally serve as the storage location for a hibernation image.

Hibernation depends on firmware and kernel support. Most reasonably recent Intel and AMD laptops and desktops work, but some systems have problems with graphics drivers, unusual firmware, or devices that do not restore correctly after a power-off. If your computer already fails to resume reliably from suspend, hibernation may require separate hardware or driver troubleshooting.

On an Australian laptop used between a Sydney office and a home in the Blue Mountains, hibernation can be useful because it consumes no overnight power and preserves a working session. It is also practical for regional users dealing with occasional outages, although a hibernated system should still be shut down normally before hardware maintenance.

Prepare a Suitable Swap File

If swapon --show already lists a suitably large file, you may be able to use it without creating another one. Confirm its path and size:

swapon --show
ls -lh /swapfile

If no suitable file exists, create one on a standard ext4 installation. The example below creates an 8 GiB swap file. Change 8G to match your system’s memory and workload:

sudo swapoff -a
sudo rm -f /swapfile
sudo fallocate -l 8G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

The chmod 600 command is important because swap may contain sensitive information from running applications. The mkswap command formats the file for swap use, while swapon activates it for the current session.

Make sure the file will be activated after future boots. Open the filesystem table:

sudo nano /etc/fstab

Add this line if it is missing:

/swapfile none swap sw 0 0

Save the file and verify the entry without rebooting:

sudo swapoff /swapfile
sudo swapon -a
swapon --show

Do not use these ext4 instructions unchanged on a Btrfs root filesystem. Btrfs swap files have special requirements, and recent Ubuntu versions provide a command such as btrfs filesystem mkswapfile for creating them safely. Existing swap files should be checked with the filesystem documentation before they are resized or replaced.

Find the Resume Offset

The root filesystem UUID identifies the device where the swap file resides. Obtain it with:

findmnt -no UUID -T /swapfile

Store the displayed UUID temporarily. It will look similar to:

12345678-abcd-4ef0-9876-123456789abc

Next, find the swap file’s physical offset:

sudo filefrag -v /swapfile

Look at the first data extent, usually the line beginning with 0:. The physical offset is commonly the fourth field on that line. This command extracts it on typical Ubuntu ext4 installations:

sudo filefrag -v /swapfile | awk '$1=="0:" {gsub(/\.\./,"",$4); print $4}'

Keep the resulting number. For example, it might be 123456, though your value will be different. The offset must be generated after the final swap file has been created. If you delete, recreate, move, or resize the file later, calculate the offset again.

A fragmented swap file can produce a result that needs extra care, and Btrfs uses different handling. The filefrag output is therefore worth reading rather than blindly copying. On a normal ext4 Ubuntu installation with a newly created file, the first extent is generally suitable for the kernel’s resume process.

Add Resume Settings to GRUB

Ubuntu’s bootloader needs the root filesystem UUID and swap-file offset. Open the GRUB defaults file:

sudo nano /etc/default/grub

Find the existing line beginning with:

GRUB_CMDLINE_LINUX_DEFAULT=

Keep options already present, such as quiet and splash, and append the two resume parameters. The completed line will resemble this:

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=12345678-abcd-4ef0-9876-123456789abc resume_offset=123456"

Replace the example UUID and number with your own values. Do not add a second GRUB_CMDLINE_LINUX_DEFAULT line, because only one setting may be used reliably.

Regenerate the bootloader configuration and the initial RAM filesystem:

sudo update-grub
sudo update-initramfs -c -k all

If update-initramfs reports that an image already exists, use the update form instead:

sudo update-initramfs -u -k all

Reboot once so the new kernel command line is active:

sudo reboot

After logging in again, check that the parameters were accepted:

cat /proc/cmdline

You should see resume=UUID=... and resume_offset=... in the output. The resume device can also be declared in /etc/initramfs-tools/conf.d/resume, although the GRUB command line is the key setting for a swap file and is easier to inspect.

Test Hibernation and Troubleshoot

Save your work before testing. Run:

systemctl hibernate

The display should turn off, the machine should power down, and it should remain off rather than sitting in a low-power sleep state. Start it again with the power button. If the configuration is correct, Ubuntu should restore the desktop and applications that were open before hibernation.

If the command is refused, inspect the current session and logs:

systemctl status systemd-logind
journalctl -b -0 | grep -iE 'hibernate|resume|swap'

A common cause is insufficient swap capacity. Another is an incorrect UUID or offset, especially after recreating the swap file. Confirm all three values independently:

swapon --show
findmnt -no UUID -T /swapfile
sudo filefrag -v /swapfile

If the computer powers on but starts a fresh session, the kernel probably did not locate the saved image. Check /proc/cmdline, regenerate the initramfs, and verify that the swap file is listed in /etc/fstab. If the machine freezes during resume, graphics drivers or firmware may be involved. Testing with a current Ubuntu kernel and proprietary or open-source graphics drivers can help identify that problem.

The hibernation option may not appear in Ubuntu’s desktop menus even when the command works. Menu availability is controlled by GNOME and Ubuntu policy, while the underlying systemd operation can be tested directly. A working systemctl hibernate command is the more useful first check.

Use Hibernation Safely on Ubuntu

Hibernation writes the contents of memory to disk, so it should be treated differently from an ordinary screen lock. Anyone able to access an unencrypted swap file might recover fragments of documents, browser sessions, credentials, or other application data. Full-disk encryption provides stronger protection when the computer is powered off, but the exact security of an existing installation depends on how its root and swap storage were configured.

Avoid hibernating while a system update, firmware flash, disk repair, or large file transfer is running. Those tasks can be interrupted when the saved session is restored. Close applications that manage removable drives, unmount external storage, and let pending updates finish before hibernating.

Battery behaviour also matters. Suspend is convenient for a short trip, such as a Melbourne tram ride or an afternoon away from a Brisbane desk, because it resumes quickly. Hibernation takes longer to save and restore but avoids battery drain during a long flight, a weekend away, or an unplanned power interruption. Hybrid sleep can offer both behaviours, though support varies by hardware.

Keep an eye on swap usage with:

free -h
swapon --show

If normal workloads regularly consume nearly all available swap, increase the file size and recalculate resume_offset. Make the change deliberately: disable the old file, recreate or resize it, activate it, update /etc/fstab, and regenerate the boot configuration if the file’s physical layout has changed.

A swap file can make Ubuntu hibernation dependable without repartitioning a disk. The essential details are a sufficiently large file, correct permissions, a valid fstab entry, the root filesystem UUID, and the physical offset reported for that exact file. Verify those values, test with systemctl hibernate, and Ubuntu can restore a complete working session after the computer has been fully powered off.