I have used Windows a couple years. Sometimes I had to shutdown laptop because of power loss or travel and the hibernate feature helped me. Now after switching to Ubuntu I found there is no hibernate feature. I have read some questions specifically this one. How to enable hibernate option in Ubuntu 20.04? Now after reading this guy's answer as he told I should have swap partition not swap file. Now unfortunately I have swap file. Now what if I wanted to hibernate WITH the swap file?
3 Answers
To enable Hibernation in 20.04 using Swapfile:
Increase swapfile
size to match RAM size up to 8GB.
Check the swap that is in use:
sudo swapon -s
If swap partition(s) are found:
sudo swapoff -a sudo nano -Bw /etc/fstab
Add
#
before theUUID
of the swap partition(s):# UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX none swap sw 0 0
Add a line for the
swapfile
, if one does not exist:/swapfile none swap sw 0 0
Create the
swapfile
:sudo fallocate -l XG /swapfile*
where
X
isswapfile
's size in GB:sudo mkswap /swapfile sudo chmod 0600 /swapfile sudo swapon /swapfile
Reboot:
sudo reboot
Add resume
location and offset to grub.cfg
:
Edit
/etc/default/grub
:GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX resume_offset=XXXXX"
Use
UUID
from root.Use offset from:
sudo filefrag -v /swapfile |grep " 0:"| awk '{print $4}'
Update GRUB:
sudo update-grub
Test hibernation:
sudo systemctl hibernate
A hibernate button can be added using GNOME extensions.
Note that there is a slight possibility of getting holes in a swapfile
when creating it with fallocate
. /var/log/syslog
can be searched for the phrase swapon: swapfile has holes
to ensure there will be no data loss.
A swap file can alternatively be created using dd
:
sudo dd if=/dev/zero of=/swapfile bs=1G count=8
An error when using dd
may overwrite your HDD.

- 19,519
-
After the first reboot it was stuck at the loading screen. I am kinda techy so I always press esc to see that Ido type something. It said failed to activate /swapfile Please help. I have windows usb disk and Ubuntu live usb – Rushi Kaneria Aug 02 '21 at 14:54
-
Ok the error was when you said to put # before UUID. Then I went to root through recovery mode then with a command mount -o remount,rw /dev/sda7 / which was my installation partition I got it in read write mode and then removed that # and rebooted. Worked. – Rushi Kaneria Aug 02 '21 at 15:22
Hibernating is very fragile, and requires both operating system and driver support, both in linux and in windows. If any part of the system doesn't support hibernation, it won't work. Best case, it disables it and doesn't offer the option; worst case, the hibernation fails and you end up rebooting anyway, possibly after finding out some devices don't work correctly on wake up.
An answer in the question you linked to explains how to use a swapfile for hibernation. However, you must satisfy several different conditions for this to work, and if any of them are not met, it will fail.

- 4,051
to continue this answer, in my case, after doing with grub, it did not work, so I added a file to initramfs-tools config with the below content:
# write the resume UUID (make sure to replace this with your UUID)
echo RESUME=UUID=<Swap Device UUID> > /etc/initramfs-tools/conf.d/resume
regenerate initramfs
sudo update-initramfs -c -k all

- 101
-
-
this answer not worked for me and
grub
withinitramfs
correct hibernate on ubuntu for me. – Hossein Vatani Mar 22 '22 at 16:39