75

I tried different options to enable hibernation in Ubuntu 20.04 but nothing is working. How can I enable the hibernate option in Ubuntu 20.04?

1
  • Hibernate does not work with all equipment. Install the pm-utils package.
    – user535733
    May 16 '20 at 3:48
72

I assume you have a swap partition ready to use (if you have a swap file you cannot hibernate). Follow these steps:

  1. Install pm-utils and hibernate:

    sudo apt install pm-utils hibernate
    
  2. Then:

    cat /sys/power/state
    
  3. You should see:

    freeze mem disk
    
  4. Then run:

    grep swap /etc/fstab
    
  5. Copy the UUID value. You will need it later.

  6. Then run (use your favorite editor if not nano):

    sudo nano /etc/default/grub
    
  7. Change the line that says:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
    

    so that it instead says:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=<YOUR_COPIED_UUID>"
    

    Be careful not to miss the UUID= part.

  8. Then, after saving the file and quitting the text editor, run:

    sudo update-grub
    
  9. To test it, run:

    sudo systemctl hibernate
    

This extension seems to enable showing the "Hibernate" menu entry, but it changes the overall look of this sub-menu: https://extensions.gnome.org/extension/3070/simpler-off-menu/ .


Tested on Ubuntu 20.04 using kernel version 5.4.0-31 on my Lenovo ThinkPad X1 Carbon.

20
  • 2
    Works. :) it worked for me only with UUID.
    – adazem009
    Jun 23 '20 at 18:08
  • 2
    just to mention this extension for gnome 36 / ubuntu 20.04, handy once the setup is done. extensions.gnome.org/extension/755/hibernate-status-button (check github page of the extension for additional configuration required by ubuntu)
    – squalou
    Jun 28 '20 at 8:53
  • 13
    What if grep swap /etc/fstab doesn't return a UUID? Aug 8 '20 at 7:57
  • 11
    Running grep swap /etc/fstab returns /swapfile none swap sw 0 0 . Any suggestion? Sep 28 '20 at 19:30
  • 3
    @DouglasLeeder Why switch to a swap partition when a swap file is perfectly fine for hibernation (assuming it's big enough). You just need to find the offset of the swap file and in addition to the resume parameter, pass a resume_offset parameter. This is described in detail on Arch Wiki: wiki.archlinux.org/index.php/Power_management/…
    – user31389
    Dec 19 '20 at 11:56
18

If you want to use a /swapfile to hibernate instead of the swap partition:

The top answer works well, but you don't have to use a partition, you can also use a default /swapfile.

First of all, you should increase the size of the /swapfile at least to the size of your RAM.

  1. Install dependencies:

    sudo apt install pm-utils hibernate uswsusp
    
  2. Find your UUID and swap offset:

    findmnt -no UUID -T /swapfile && sudo swap-offset /swapfile
    

    You will see something like this:

    371b1a95-d91b-49f8-aa4a-da51cbf780b2
    resume offset = 23888916
    
  3. Edit /etc/default/grub and replace the string:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash"
    

    with your UUID and offset:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=371b1a95-d91b-49f8-aa4a-da51cbf780b2 resume_offset=23888916"
    
  4. Update GRUB:

    sudo update-grub
    
  5. Test your hibernation:

    sudo systemctl hibernate
    

Probably you should not change the size of your swap after enabling the hibernation (at least without changing the swap-offset in GRUB).

See wiki for more details.

UPD: If you want to hibernate when a laptop lid is closed (see this):

  1. Disable any options in the settings that touch the laptop lid, set them to "do nothing".

  2. Run:

    sudo mkdir -p /etc/acpi/events/ && sudo nano /etc/acpi/events/laptop-lid
    

    and paste:

    event=button/lid.*
    action=/etc/acpi/laptop-lid.sh
    
  3. Run:

    sudo touch /etc/acpi/laptop-lid.sh && sudo chmod +x /etc/acpi/laptop-lid.sh && sudo nano /etc/acpi/laptop-lid.sh
    

    and paste:

    #!/bin/bash
    
    LOG_FILE='/var/log/laptop-lid.log'
    touch $LOG_FILE && chmod 0666 $LOG_FILE
    
    grep -q closed /proc/acpi/button/lid/LID/state
    if [ $? = 0 ]
    then
        # close action
        echo "$(date '+%Y.%m.%d %H:%M:%S.%3N'): closed" >> $LOG_FILE
        systemctl hibernate
    else
        # open action
        echo "$(date '+%Y.%m.%d %H:%M:%S.%3N'): opened" >> $LOG_FILE
    fi
    
  4. Run:

    sudo /etc/init.d/acpid restart
    
5
  • 1
    Note: do not use the UUID returned by mkswap for resume_offset. The mkswap UUID is useless according to ubuntuforums.org/showthread.php?t=1042946
    – mark
    Mar 29 at 1:33
  • 2
    This works for me. No need to use a swap partition. The swap file is enough. Apr 1 at 5:50
  • 1
    This worked for me! I don't understand why all the answers use a swap partition... My brand new install of ubuntu came with a swapfile but no swap partition.
    – gatoWololo
    May 13 at 17:21
  • 1
    Don't know why but did'nt work for me (lubuntu 20.04) I had to make a swap partition and then it worked. Anyway it was an informative response. Jun 24 at 22:25
  • Unfortunately, this doesn't work for me, because sudo systemctl hibernate returns the error message, Failed to hibernate system via logind: Sleep verb "hibernate" not supported. According to an internet search, hibernation is incompatible with Secure Boot. Do you concur? Aug 14 at 17:44
10

And if you want to turn on hibernation in your Ubuntu 20.04*, follow these steps:

  1. First ensure you allocate swap memory in your machine to check:

    swapon --show
    
  2. Then check whether the swap memory you allocated is more than or at least equal to the Physical memory(RAM).

  3. Use the following command to find the swap partition:

    grep swap /etc/fstab
    
  4. Copy the UUID of the output for example(UUID=XXXXX-XXX-XXXX-XXXX-YYYYYYYYYY).

  5. Add a boot parameter by the following command:

    sudoedit /etc/default/grub
    
  6. At the line starting with GRUB_CMDLINE_LINUX_DEFAULT, add:

    resume=UUID=XXXXX-XXX-XXXX-XXXX-YYYYYYYYYY
    

    Note: In all other threads they used to ask to add swap partition but here we are adding the UUID value.

    The final line will be like:

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=UUID=XXXXX-XXX-XXXX-XXXX-YYYYYYYYYY"
    
  7. Update the file:

    sudo update-grub
    
  8. sudo systemctl hibernate and hibernation will now work in your Ubuntu 20.04.

6
  • 3
    If grep swap /etc/fstab does not give you swap's UUID try sudo blkid. Also, if sudo systemctl hibernate doesn't work, try sudo hibernate. I think you don't even need to edit grub because when you install hibernate it will detect your swap partition. Jul 23 '20 at 11:00
  • 2
    My RAM is 16 GB and my swap is 2 GB. How do I increase the swap to match the RAM?
    – Yehuda
    Aug 4 '20 at 17:47
  • 2
    what about if swapping is to a file instead of a partition? Nov 6 '20 at 10:10
  • 2
    @david.perez "Using a swap file requires also setting the resume=swap_device and additionally a resume_offset=swap_file_offset kernel parameters." wiki.archlinux.org/index.php/Power_management/…
    – user31389
    Dec 19 '20 at 12:01
  • Is this any different from the answer by eldwist? Jan 2 at 9:33
6

eldwist's solution worked for me, except...

...not initially, 'cos I got the following error:

$ sudo systemctl hibernate
Failed to hibernate system via logind: Sleep verb not supported

If you get this error, you probably need to disable secure boot under the security menu in the UEFI/BIOS (see similar problem with 16.04). Then it worked for me (tested on Ubuntu 20.04; 5.4.0-33 kernel, configured for Dual-boot with Win10).

6

I just added a keyboard shortcut. I disabled the Super+H, since I don't need it (the default action is to hide the window) and created a new shortcut with the command:

systemctl hibernate
1
  • 2
    systemctl hibernate did a shutdown without saving the current state, not what I expect from hibernation. -1
    – Pavel V.
    Mar 19 at 16:10
4

SuRa's answer at the top works great, however, if your laptop came with a swap file instead of a swap partition then you'll have to follow a few steps first to make it all work. At a high level:

  1. Boot from live CD/USB drive, shrink you main partition by the amount of memory you have plus 1-2 GBs for margin. E.g. I have 16 GBs so I shrunk my partition by 18 GB for good measure. That will leave empty/unpartitioned space on your disk.
  2. Create a new partition of type swap in the empty/unpartitioned space on your disk.
  3. Reboot back into your installed OS (ie without the live CD/USB)
  4. Go to "partitions" or "gparted" to get the UUID of your new swap partition.
  5. Add your new swap partition to /etc/fstab and delete (or comment out) the old swap file
  6. Use the swapoff and swapon commands to switch from the swap file to the swap partition
  7. Don't forget to the delete the swap file to reclaim that space
  8. Now you can follow SuRa's instructions.
2
  • 3
    Why do all this when you can hibernate to a swap file just fine?
    – user31389
    Dec 19 '20 at 12:03
  • This answer assumes that you do not have an encrypted volume with all your partitions in it (choosing encrypt disk from the install dvd)... I is simply pure insanity to opt for the Windows 95 level of security by NOT encrypting your drive. Therefor this answer solves for a use case that I really hope nobody has!
    – SLS
    Feb 13 at 15:55
4

To enable Hibernation in 20.04:

All of the examples on this page seem to be missing resume-offset it is necessary when using a swapfile rather than a swap partition.

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 the UUID 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 is swapfile'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.

5
  • 1
    Your fstab entry should be /swapfile, not swapfile.
    – heynnema
    Jun 23 at 15:39
  • @heynnema: Good catch, Thank you. I have finally got my cataracts replaced. }8-D Jun 24 at 5:26
  • Thanks for your answer. But I think it only works for me and for this person askubuntu.com/a/1247952/48214 if we disable Secure Boot. Is that true for you? Can you think of a way to enable Secure Boot too? Also, do you think resume=swap_device is required (which you didn't mention)? See askubuntu.com/questions/1240123/… Thanks!
    – Ryan
    Aug 3 at 12:17
  • @Ryan I'm not sure how to include secure boot, the laptop I have with me is a pre secure boot BIOS machine. I think resume=swap_device is equivalent to my resume=UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX. I use this method on my bootable USB flash drives and they normally work on the computers I plug them into okay. Aug 3 at 12:41
  • Thank you so much for your response!
    – Ryan
    Aug 3 at 12:52
3

Using a partition device instead of an UUID

I had to deviate a bit from the "how to" above, probably since I encrypted my hard drive. My /etc/fstab file does not contain an UUID for the swap partition but a partition device.

If your /etc/fstab contains a <partition_device> rather than a UUID for the swap partition, edit the line starting with GRUB_CMDLINE_LINUX_DEFAULT in the file /etc/default/grub to contain resume=<partition_device>

Example:
if /etc/fstab contains this

    # <file system> <mount point>   <type>  <options>       <dump>  <pass>
    /dev/mapper/ubuntu--vg-swap_1 none            swap    sw              0       0

/etc/default/grub should contain the line

    GRUB_CMDLINE_LINUX_DEFAULT="quiet splash resume=/dev/mapper/ubuntu--vg-swap_1"

Then execute sudo update-grub and test by executing sudo systemctl hibernate

2
  • 1
    This did not work either... just boots up to a clean desktop...
    – SLS
    Jan 31 at 21:54
  • 1
    Using an unencrypted partition as swap is NOT recommended.
    – SLS
    Feb 13 at 15:57

Not the answer you're looking for? Browse other questions tagged or ask your own question.