On Ubuntu systems, you can distinguish between suspend and hibernate by checking the /sys/power/state file for available modes and examining /sys/power/mem_sleep to see which is currently default. Additionally, systemd journal logs provide definitive proof of the last sleep state entered via specific log entries.
Understanding System Power States
Before diving into detection methods, it is crucial to understand what these states actually do at the hardware level. Suspend, often referred to as RAM sleep or S3 state, keeps the system's memory powered while shutting down most other components. This allows for a very fast resume time but requires continuous power; if the battery dies, all unsaved work is lost. Hibernate, on the other hand, writes the contents of the RAM to the swap partition or a dedicated hibernation file on disk before powering off completely. Because no power is needed to retain state, the battery drain is negligible during hibernation, but the resume process is significantly slower due to disk I/O operations. Ubuntu uses systemd as its init system, which manages these power transitions through standardized interfaces. The kernel exposes these capabilities through sysfs, making them easily queryable without needing complex external tools. Understanding this distinction helps in troubleshooting why a laptop might behave unexpectedly when plugged in versus running on battery, or why certain services fail to restart correctly after waking up from sleep.Checking Available States via Sysfs
The most direct way to verify if your system supports hibernation is to inspect the kernel's power management interface. You can do this by reading the content of the/sys/power/state file. Run the following command in your terminal:
cat /sys/power/state
If the output includes mem, your system supports suspend-to-RAM. If it includes disk, it supports suspend-to-disk (hibernation). On many modern laptops, especially those with UEFI firmware and secure boot enabled, the disk option may be absent because the kernel cannot safely write the memory image to disk without compromising security or encountering driver issues. In such cases, hibernation is effectively disabled at the kernel level, and any attempt to trigger it will fall back to suspend or fail entirely.
It is also worth noting that some systems support s2idle (Suspend-to-Idle), which is a lighter form of suspend where the CPU remains mostly active but interrupts are minimized. This is different from the traditional mem state. You can check the current preferred mem-sleep state by looking at /sys/power/mem_sleep:
cat /sys/power/mem_sleep
The output will look something like [s2idle] deep. The value inside the brackets indicates the current default. If you see only [s2idle], your system is configured to use the lighter suspend mode, which might explain why hibernation feels sluggish or unavailable in the GUI.
Using Journalctl to Verify Last Sleep State
While sysfs tells you what *can* happen, the systemd journal tells you what *did* happen. To determine if your machine recently suspended or hibernated, you can search the system logs for specific messages generated by the systemd-logind service. Use thejournalctl command with the -b -1 flag to view logs from the previous boot session:
journalctl -b -1 | grep -i "systemd-logind"
Look for lines containing "Going to sleep" or "Resume from". A typical suspend entry looks like:
Jan 01 12:00:00 hostname systemd-logind[1234]: Suspending...
A hibernation entry will explicitly mention "Hibernate" or "Disk":
Jan 01 12:00:00 hostname systemd-logind[1234]: Hibernate...
This method is particularly useful for debugging scripts that need to run different actions depending on whether the system woke up from a short nap or a long rest. For instance, network managers might need to re-authenticate after hibernation but not after suspend.
Configuring Suspend-Then-Hibernate
Many users prefer a hybrid approach known as "suspend-then-hibernate." In this mode, the system enters suspend immediately to allow for quick wake-ups, but if the battery reaches a critical threshold while suspended, it automatically hibernates to preserve data. This feature is managed by systemd timers and targets. To enable this behavior, you typically need to create or edit a configuration file in/etc/systemd/sleep.conf. Add the following line under the [Sleep] section:
SuspendMode=suspend-then-hibernate
You may also want to set SuspendDelaySec=15m to specify how long the system should stay in suspend before transitioning to hibernate. After saving the file, reload the systemd daemon:
sudo systemctl daemon-reload
This configuration ensures that your data is safe even if the battery dies during a long period of inactivity, combining the speed of suspend with the safety of hibernate. This is particularly relevant for mobile users who rely on Ubuntu for daily tasks and cannot afford to lose work due to unexpected power loss. For more information on securing your Ubuntu environment against various threats, including power-related vulnerabilities, you might find Securing an Ubuntu Server for Development Purposes helpful.
Troubleshooting Common Issues
If you detect that hibernation is not working despitedisk being present in /sys/power/state, check your swap space. Hibernation requires swap space equal to or larger than your used RAM. If your swap is too small, the kernel will refuse to hibernate. You can check swap size with swapon --show.
Additionally, ensure that your GRUB configuration allows for proper resume. Sometimes, the resume parameter needs to be added to GRUB_CMDLINE_LINUX_DEFAULT in /etc/default/grub if the system fails to wake up correctly. After modifying GRUB, always run sudo update-grub to apply changes.
For those managing virtual machines, detecting sleep states can be tricky since the guest OS does not control the physical hardware directly. If you are running Ubuntu in a VM, refer to Ubuntu 22.04 VM on KVM/virt-manager Not Connecting to Internet for related infrastructure insights, though note that VMs generally do not support true hibernation in the same way bare-metal systems do.
Frequently Asked Questions
Can I hibernate if my swap is smaller than my RAM?
No, hibernation requires enough swap space to store the entire contents of RAM. If your swap is insufficient, the kernel will block the hibernation process to prevent data corruption. You must increase your swap size or disable hibernation.Why does my laptop only show s2idle in mem_sleep?
This usually indicates that the kernel has determined the deeperdeep sleep state is incompatible with your hardware, often due to driver issues or BIOS limitations. You may need to update your kernel or adjust BIOS settings to enable deeper sleep states.
Does enabling suspend-then-hibernate affect performance?
There is no noticeable performance impact on daily usage. The transition from suspend to hibernate happens in the background only when the battery is low, so normal operation remains unaffected.How do I force a hibernate command manually?
You can use the commandsudo systemctl hibernate. However, if the system falls back to suspend, it means hibernation is not properly configured or supported on your hardware.
Is hibernation supported on Ubuntu 22.04 out of the box?
It depends on the hardware. While the software stack supports it, many manufacturers disable it in firmware or require specific kernel parameters. Check/sys/power/state to confirm availability on your specific device.