Home > Bash, Linux, Ubuntu > Enable Hibernate in Ubuntu 18.04

Enable Hibernate in Ubuntu 18.04

September 12th, 2019 Leave a comment Go to comments

Ref: https://askubuntu.com/questions/6769/hibernate-and-resume-from-a-swap-file/1132154#1132154

All credits goes to https://askubuntu.com/users/29219/anthony-o !

make it work with Ubuntu 18.04.

Make your /swapfile have at least the size of your RAM (make it double the size of your RAM if you have problems using the same size as your RAM).

sudo swapoff /swapfile
sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh '[0-9]*') count=1024 conv=notrunc
sudo mkswap /swapfile
sudo swapon /swapfile

Note the UUID of the partition containing your /swapfile:

$ sudo findmnt -no SOURCE,UUID -T /swapfile
/dev/nvme0n1p5 20562a02-cfa6-42e0-bb9f-5e936ea763d0

Reconfigure the package uswsusp in order to correctly use the swapfile:

sudo dpkg-reconfigure -pmedium uswsusp
# Answer "Yes" to continue without swap space
# Select "/dev/disk/by-uuid/20562a02-cfa6-42e0-bb9f-5e936ea763d0" replace the UUID with the result from the previous findmnt command
# Encrypt: "No"

Edit the SystemD hibernate service using sudo systemctl edit systemd-hibernate.service and fill it with the following content:

ExecStartPre=-/bin/run-parts -v -a pre /lib/systemd/system-sleep
ExecStartPost=-/bin/run-parts -v --reverse -a post /lib/systemd/system-sleep

Note the resume offset of your /swapfile:

$ sudo swap-offset /swapfile
resume offset = 34818

Configure Grub to resume from the swapfile by editing /etc/default/grub and modify the following line:

GRUB_CMDLINE_LINUX_DEFAULT="resume=UUID=20562a02-cfa6-42e0-bb9f-5e936ea763d0 resume_offset=34818 quiet splash"

Update Grub:

sudo update-grub

Update initramfs: sudo update-initramfs -u -k all

Create the following /etc/initramfs-tools/conf.d/resume:

RESUME=UUID=20562a02-cfa6-42e0-bb9e-5e936ea763d0 resume_offset=34816
# Resume from /swapfile

Update initramfs:

sudo update-initramfs -u -k all

Now you can hibernate with sudo systemctl hibernate.

One can also create those scripts:

    sudo tee /usr/local/bin/gotosleep <<EOF
    dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
    sleep 2
    sudo /usr/sbin/s2both
    sudo chmod +x /usr/local/bin/gotosleep
    sudo tee /usr/local/bin/gotohibernation <<EOF
    dbus-send --type=method_call --dest=org.gnome.ScreenSaver /org/gnome/ScreenSaver org.gnome.ScreenSaver.Lock
    sleep 2
    sudo systemctl hibernate
    sudo chmod +x /usr/local/bin/gotohibernation

So you can sleep with gotosleep or hibernate with gotohibernation.

You must be able to execute sudo s2both, sudo s2ram and sudo systemctl hibernatewithout having to enter your password for the previous scripts to work.

You could do that for example by creating a powerdev group, add your current user to it, and configure the following sudoers config (edit it with sudo visudo -f /etc/sudoers.d/powerdev):

     %powerdev ALL=NOPASSWD: /usr/sbin/s2both, /usr/sbin/s2ram, /bin/systemctl hibernate

Documentation used:

Add ‘Hibernate’ Option in Power Menu in Ubuntu 18.04

Ref: http://ubuntuhandbook.org/index.php/2018/05/add-hibernate-option-ubuntu-18-04/

sudo gedit /etc/polkit-1/localauthority/50-local.d/com.ubuntu.enable-hibernate.pkla

When the file opens, paste following content and save it.

[Re-enable hibernate by default in upower]

[Re-enable hibernate by default in logind]

Install the Gnome connector:

sudo apt-get install chrome-gnome-shell

Open Firefox, press CTRL+SHIFT+a, and install the “GNOME Shell integration” Add-On. Link to the add-on: https://addons.mozilla.org/en-US/firefox/addon/gnome-shell-integration/?src=search

Go to https://extensions.gnome.org/ and search for “Hibernate Status Button“, or click this link: https://extensions.gnome.org/extension/755/hibernate-status-button/. Turn “ON” the extension in the upper right corner.

Restart your computer and enjoy the new hibernate-button in the shut-down dialog!

For those who never installed a gnome extension, see this how to tutorial for details.

Categories: Bash, Linux, Ubuntu Tags:
  1. Arash
    March 4th, 2020 at 14:33 | #1

    after reading almost 10-15 tutorials this was the best one and worked perfect.
    there is just one thing. I have swap partition instead of swap file and first steps are a little different.

    • admin
      March 4th, 2020 at 17:41 | #2

      Thank you for your input!

  2. igor
    April 6th, 2020 at 21:05 | #3

    hi, hibernation works perfect, needs a little bit of time to load all apps, but flawless.

    but still, when i enter hibernation, it doesnt turn off system, how to solve this

  3. InputOutput
    April 7th, 2020 at 14:57 | #4

    Note there’s a ‘c’ dropped from the dd command: “conv=notrun” should be “conv=notrunc”

    • admin
      April 7th, 2020 at 15:20 | #5

      Thank you, fixed now!

  4. admin
    April 7th, 2020 at 15:27 | #6


    I haven’t seen that behaviour before.

    Maybe you can try to create a file in the pm config directory:

    sudo vim /etc/pm/config.d/hibernate_mode

    that sets the variable HIBERNATE_MODE to:


  5. Prem
    June 3rd, 2020 at 19:48 | #7

    I did the configurations till the step just before “One can also create those scripts:” section
    On giving “sudo systemctl hibernate”, system is hibernating, but system does not poweroff. The screen goes off a while and the system starts automatically. Any idea ? I tried HIBERNATE_MODE=”shutdown” in /etc/pm/config.d/hibernate_mode

  6. Prem
    June 3rd, 2020 at 20:23 | #8

    I could resolve the issue. I had previously allocated huge pages in the grub file. I removed those and updated the grub, it started working fine.
    ie. in the /etc/defualt/grub
    GRUB_CMDLINE_LINUX_DEFAULT=”resume=UUID=457354-5635-7895-bb9f-3478956865e resume_offset=34818 quiet splash default_hugepagesz=1G hugepagesz=1G hugepages=32″

    i removed default_hugepagesz=1G hugepagesz=1G hugepages=32 from it.
    sudo update-grub
    sudo update-initramfs -u -k all

    Thanks for the steps

    • admin
      June 3rd, 2020 at 22:09 | #9

      ok, great you got it working! And thank you for your feedback!

  7. Evgenii
    June 17th, 2020 at 19:06 | #10

    Still have the same problem
    Failed to hibernate system via logind: Sleep verb not supported

  8. Hung Nguyen
    August 4th, 2020 at 01:10 | #11

    I got it work on my machine. Thank you.
    Note: we need to have swapsize double size of mem as mentioned above.

  9. Moe
    August 17th, 2020 at 15:56 | #12


    I wanted to try this procedure. When I excuted the 2nd command,
    “sudo dd if=/dev/zero of=/swapfile bs=$(cat /proc/meminfo | grep MemTotal | grep -oh ‘[0-9]*’) count=1024 conv=notrunc”, it took a long time and Ubuntu notified me that I am running low on space somewhere!

    I could not cancel the task and closed the terminal instead. Then one of the softwares that I needed was not running so I restarted the machine. Ubuntu never came back after that! I get just a blinking screen! Does anyone know how to reverse the damage?


    • admin
      December 18th, 2020 at 10:43 | #13

      Probably was the meminfo misread and the dd command filled your disk. You could try booting from an USB-stick with Ubuntu and then delete the /swapfile you created.

  10. Fuintur
    September 4th, 2020 at 11:26 | #14

    I followed this tutorial and I managed to get my hibernation working inf ubuntu 18.04.

    But I have recently upgraded to ubuntu 20.04 and I believe most of my config and swap file is outdated, orphaned and generally not working. Hibernation works terribly (after waking up my computer will overheat terribly, the swap file hasn’t been modified since the update, and the links to initrd.img and vmlinuz are broken) Is there a way to undo all of these changes without breaking native hibernation in ubuntu 20.04?

    Thank you!

  1. No trackbacks yet.