Home Guides Ubuntu: Enable Hibernate (S4) with a Swapfile (plain ext4)

Ubuntu: Enable Hibernate (S4) with a Swapfile (plain ext4)

Last updated on Feb 15, 2026

This guide is written for non‑technical users. You will copy/paste a few commands into Terminal.

Easy method (recommended) — Use the one‑click “Enable Hibernate” launcher

If you were provided a folder called hibernate-launcher, you can enable hibernate by double‑clicking:

Enable Hibernate (S4).desktop

You will be asked for an administrator password.

If it does not run:

  • Right‑click the file → Allow Launching
  • If you don’t see that option: right‑click → PropertiesPermissions → enable “Allow executing file as program”

After it completes, reboot.

What this does

  • Creates a swapfile at /swapfile (used for hibernate).
  • Configures the system so it can resume from hibernation using that swapfile.
  • Optionally changes lid close / power button to use Hibernate (S4) instead of Sleep (S3).

Before you start (important)

  • Plug in power if possible.
  • Save your work and close important apps.
  • Security note: hibernation writes the contents of RAM to disk. If your disk is not encrypted, sensitive data may be recoverable from the swapfile.

Step 1 — Open Terminal

Try one of these:

  • Press Ctrl + Alt + T
  • Or press the Windows / Super key, type Terminal, press Enter

Step 2 — Create the swapfile (recommended for 64GB RAM)

The swapfile needs to be large enough to store (roughly) the contents of RAM when hibernating.

As a simple rule:

  • Pick a swapfile size that is at least your RAM size
  • If you are unsure, use RAM + 25% (rounded up)

Examples:

  • 8GB RAM → 10–12GB swapfile
  • 16GB RAM → 20GB swapfile
  • 32GB RAM → 40GB swapfile
  • 64GB RAM → 72GB swapfile

When you see a password prompt, type your password and press Enter (you won’t see the characters as you type — that’s normal).

First, check how much RAM you have:

free -h

Look at the Mem: line (the total column).

Copy/paste this whole block into Terminal:

set -euo pipefail

# Choose a swapfile size (change this if you have more/less RAM)
# Examples: 12G (8GB RAM), 20G (16GB RAM), 40G (32GB RAM), 72G (64GB RAM)
SWAPSIZE=72G

# Turn off any old swapfile (if present)
sudo swapoff /swap.img 2>/dev/null || true
sudo swapoff /swapfile 2>/dev/null || true

# Remove old /swap.img entry from /etc/fstab (if present)
sudo sed -i.bak '/^[[:space:]]*\/swap\.img[[:space:]]\+none[[:space:]]\+swap[[:space:]]/d' /etc/fstab

# Create /swapfile. This can take a minute or two.
sudo rm -f /swapfile
sudo fallocate -l "$SWAPSIZE" /swapfile

# Lock down permissions (required)
sudo chmod 600 /swapfile

# Format as swap and enable it
sudo mkswap /swapfile
sudo swapon /swapfile

# Make it permanent across reboots
grep -qE '^[[:space:]]*/swapfile[[:space:]]+none[[:space:]]+swap[[:space:]]' /etc/fstab || \
  echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab >/dev/null

# Show result
swapon --show
free -h

Expected result:

  • swapon --show lists /swapfile with a size close to what you chose (for example 20G).

Step 3 — Configure resume-from-hibernate (the important bit)

Copy/paste this whole block into Terminal:

set -euo pipefail

# Find the root filesystem device (where /swapfile lives)
ROOTDEV=$(findmnt -no SOURCE /)
ROOTUUID=$(blkid -s UUID -o value "$ROOTDEV")

# Find the swapfile's physical offset (this is required for swapfile hibernate)
OFFSET=$(sudo filefrag -v /swapfile | awk '$1 ~ /^[0-9]+:$/ {print $4; exit}' | cut -d. -f1)

echo "Root device: $ROOTDEV"
echo "Resume UUID:  $ROOTUUID"
echo "Offset:      $OFFSET"

# Tell initramfs-tools what to resume from
printf "RESUME=UUID=%s\nresume_offset=%s\n" "$ROOTUUID" "$OFFSET" | sudo tee /etc/initramfs-tools/conf.d/resume >/dev/null
sudo update-initramfs -u

# Also add resume arguments to GRUB (more reliable across boots)
sudo sed -i.bak -E 's/(^|[[:space:]])resume=[^[:space:]]+//g; s/(^|[[:space:]])resume_offset=[^[:space:]]+//g' /etc/default/grub
sudo sed -i -E "s/^(GRUB_CMDLINE_LINUX_DEFAULT=\")([^\"]*)\"/\\1\\2 resume=UUID=${ROOTUUID} resume_offset=${OFFSET}\"/" /etc/default/grub
sudo update-grub

Note:

  • You might see a warning during update-initramfs like “no matching swap device is available”. With a swapfile, that warning can be harmless (it tries to validate a swap partition). The real test is the reboot + resume test below.

Step 4 — Reboot

Reboot the computer normally.

Step 5 — Verify it’s configured correctly (after reboot)

Open Terminal again and run:

cat /proc/cmdline | tr ' ' '\n' | grep -E '^resume(=|_offset=)'
swapon --show

Expected result:

  • You see both resume=UUID=... and resume_offset=...
  • swapon --show includes /swapfile

Step 6 — Test hibernate

Run:

sudo systemctl hibernate

If everything is working, the machine should power off, then later boot and restore your session.

If it did not resume (troubleshooting)

After a failed resume (you got a fresh boot), run:

journalctl -b -1 | grep -Ei 'hibernate|resume|swsusp|swap|image' || true
dmesg | grep -Ei 'hibernate|resume|swsusp|PM:' || true

Common causes:

  • The swapfile was recreated or moved (its resume_offset changed). Re-run Step 3.
  • A “defrag” / file-moving tool moved swapfile extents. Avoid defragging /swapfile.

Optional — Make “sleep” actions use Hibernate (S4) by default

This changes what happens when you close the lid or press the power button.

Option A (graphical) — GNOME via “Dconf Editor”

On many Ubuntu installs (GNOME), the normal Settings app does not always show “Hibernate” as a choice. If you want a graphical method, use Dconf Editor:

  1. Open the app store (Ubuntu Software / App Center)
  2. Install Dconf Editor
  3. Open Dconf Editor
  4. Go to:

orggnomesettings-daemonpluginspower

  1. Set these to hibernate:
  • lid-close-ac-action
  • lid-close-battery-action
  • power-button-action

Optional (also set these to hibernate if you want “auto-sleep when inactive” to hibernate):

  • sleep-inactive-ac-type
  • sleep-inactive-battery-type

Log out and log back in (or reboot) to be safe.

Option B (system-wide) — systemd logind.conf

This works even without GNOME and applies system-wide.

Edit logind settings:

sudoedit /etc/systemd/logind.conf

Set (or uncomment and set) these lines:

HandleLidSwitch=hibernate
HandleLidSwitchExternalPower=hibernate
HandleLidSwitchDocked=ignore
HandlePowerKey=hibernate
HandleSuspendKey=hibernate

Apply changes:

sudo systemctl restart systemd-logind

Notes:

  • Desktop menus may still show “Sleep” and may still request S3 suspend. The above primarily affects lid close / keys.

Notes

  • If /swapfile is ever recreated, re-run Step 3 (offset changes).
  • Do not run “defrag” tools on /swapfile.