I have configured a new Ubuntu installation in a Notebook to use a swap file, instead of using a swap partition.

By default is not possible to get Ubuntu to hibernate using a swap file, so I tried this tutorial, but it is specific to grub1, and Ubuntu now uses grub2.

Does anybody know how to do this?


Hibernate with Swap file using uswusp

Although it is possible to hibernate to swap file and it supposedly works with systemd hibernate by setting kernel parameters. However, I couldn't get it to resume so instead switched to using uswsusp (userspace software suspend). Here are the steps I used on Ubuntu 17.04/17.10.

Create the Swap File

The commands to create a formatted 4GiB swap file, mounted and added to /etc/fstab:

sudo fallocate -l 4g /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

Verify Swap File Partition

sudo findmnt -no SOURCE,UUID -T /mnt/4GiB.swap
> /dev/sda1 11cc33ee-1234-abcd-1234-ddeeff112233

Install Userspace Software Suspend (uswsusp)

sudo apt install uswsusp

Configure uswsusp

To create /etc/uswsusp.conf and recreate initramfs:

sudo dpkg-reconfigure -pmedium uswsusp
  • Yes to 'Continue without a valid swap space?' (Wizard not set swap file yet.)
  • Select the partition that the swap-file resides on, cross-reference with details from findmnt above. (Note:not the swap-file itself)

Note: Any changes manually made to /etc/uswsusp.conf will require recreating initramfs afterwards with this command:

sudo update-initramfs -u

Test uswusp hibernate

sudo s2disk

There should be snapshot messages on the screen on hibernate and resume.

Use s2disk with systemd hibernate

By default systemd will use it's own hibernate commands so replace them with the uswusp commands by overriding the systemd-hibernate.service:

sudo systemctl edit systemd-hibernate.service

In the text editor that opens put the following text (the blank ExecStart is required). Then save and exit:

ExecStartPost=/bin/run-parts -a post /lib/systemd/system-sleep

This will create /etc/systemd/system/systemd-hibernate.service.d/override.conf file with override details.

Test systemd hibernate :

systemctl hibernate 

Note: To check that the systemd override.conf has been created, loaded and no errors, run:

systemctl status systemd-hibernate.service


  • This instructions seems to work but the systemd hibernation is not working for me. Have you tested this your own? Should this ExceStart=run-parts -a post /lib/systemd/system-sleep be ExceStartPost=run-parts -a post /lib/systemd/system-sleep – Joaquín Aramendía Oct 2 '17 at 14:51
  • It was tested and working but perhaps that addition is needed. I have updated my answer. – Cas Oct 2 '17 at 15:33
  • This doesn't look quite right. The enable option of systemctl symlinks from /etc/systemd/system/ to /lib/systemd/system/. You should never be copying directly into /etc/systemd/system/ – Auspex Oct 29 '17 at 19:46
  • Yes you should put put service files in /etc/systemd/system, especially if you are overriding existing services: wiki.archlinux.org/index.php/systemd#Writing_unit_files – Cas Oct 29 '17 at 22:46
  • That said there is an alternative of using a conf file as specified at the bottom of the systemd.unit docs but still either way is recommended. – Cas Oct 29 '17 at 22:51

I've given a quick read to the tutorial and, if I have understood correctly, you just need to specify the resume options to the Linux command line. With Grub2 is really simple, and your changes will be always preserved. You need to edit the /etc/default/grub file, specifically this line:

GRUB_CMDLINE_LINUX="resume=... resume_offset=..."

After that, run sudo update-grub for the changes to take effect.

Changing GRUB_CMDLINE_LINUX won't affect other Linux installations you have (because /etc/grub.d/30_os-prober does not use this variable).

About the problem you're having: is the partition of the swapfile encrypted? If so, hibernation won't work. If not, then the output of filefrag -v /swapfile may be helpful.

  • Thanks! I will try. But I think it config all linux entries in grub with this line. – tfmoraes Oct 13 '10 at 16:26
  • Don't forget: after changing /etc/default/grub you need to run update-grub. – JanC Oct 13 '10 at 16:44
  • @tfmoraes: why wouldn't you want this added to all linux entries? – JanC Oct 13 '10 at 16:45
  • @JanC: Because I may have other Linux distributions installed. – tfmoraes Oct 13 '10 at 17:21
  • 1
    @didi_X8 that comment was written 8 years ago. It's possible that things have changed since then. Thanks for sharing. – Andrea Corbellini Jul 9 '18 at 16:04

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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