DEV Community

giveitatry
giveitatry

Posted on

Prevent Ubuntu Server from Hibernating When the Laptop Lid Is Closed

Ubuntu Server is often installed on laptops for portability, testing, or low‑power home servers. One common issue is that the system hibernates or suspends when the laptop lid is closed, even at the login screen. This behavior is problematic for servers that must stay running.

This article explains why this happens and provides a reliable, step‑by‑step solution to ensure Ubuntu Server keeps running when the lid is closed — even when no user is logged in.


Why Ubuntu Server Hibernates When the Lid Is Closed

Ubuntu Server does not use a desktop environment. This means:

  • systemd‑logind is fully responsible for handling lid‑close events
  • Desktop power settings do not exist
  • If the system hibernates at the login screen, it is always due to systemd-logind

By default, logind may suspend or hibernate the system when it detects the lid closing.


Step 1: Configure systemd‑logind to Ignore Lid Events

Edit the logind configuration file:

sudo nano /etc/systemd/logind.conf
Enter fullscreen mode Exit fullscreen mode

Set all lid‑related options explicitly:

[Login]
HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
LidSwitchIgnoreInhibited=no
Enter fullscreen mode Exit fullscreen mode

⚠️ Do not leave these lines commented. Explicit values are required.

Save the file and exit the editor.

Explanation of systemd-logind Lid Settings

The following options in /etc/systemd/logind.conf control how Ubuntu Server reacts when the laptop lid is closed. These settings apply system-wide, including before any user logs in.


HandleLidSwitch=ignore

Purpose:
Controls what happens when the laptop lid is closed while running on battery power.

Common values:

  • ignore – do nothing
  • suspend – suspend the system
  • hibernate – hibernate the system
  • poweroff – shut down the system

What ignore does:
Closing the lid triggers no action at all.

Why this matters:
This is the primary setting that prevents Ubuntu Server from suspending or hibernating when the lid is closed at the login screen or during normal operation.


HandleLidSwitchExternalPower=ignore

Purpose:
Controls lid-close behavior when the system is connected to AC power.

What ignore does:
Closing the lid does nothing, even when the laptop is plugged in.

Why this matters:
Many systems apply different power policies on AC vs battery. For server use, the system is often plugged in permanently, so this setting ensures consistent behavior.


HandleLidSwitchDocked=ignore

Purpose:
Controls what happens when the lid is closed while the system is considered docked.

What “docked” means:
systemd-logind treats the system as docked when:

  • An external monitor is connected, or
  • A docking station is detected

What ignore does:
Closing the lid does nothing, even if an external display or dock is present.

Why this matters:
Without this setting, the system may still suspend when docked, despite the other lid settings being configured.


LidSwitchIgnoreInhibited=no

Purpose:
Controls whether inhibitors are allowed to override lid-close behavior.

What inhibitors are:
Inhibitors are requests made by services or applications to delay or control power actions (for example, during shutdown or suspend).

What no means:
Lid events are not ignored just because an inhibitor exists. The lid behavior defined above is always applied.

Why this matters:
This ensures predictable behavior on Ubuntu Server, where no desktop session exists to manage power inhibitors.


Step 2: Restart systemd‑logind (or Reboot)

Apply the changes immediately:

sudo systemctl restart systemd-logind
Enter fullscreen mode Exit fullscreen mode

For maximum reliability, reboot the system:

sudo reboot
Enter fullscreen mode Exit fullscreen mode

Step 3: Disable Sleep and Hibernate Targets (Recommended)

Even with logind configured correctly, systemd sleep targets can still trigger suspend or hibernate.

Disable them completely:

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target
Enter fullscreen mode Exit fullscreen mode

This ensures no service can force the system to sleep.


Step 4: Verify That the Configuration Is Active

Check what systemd‑logind is actually using:

loginctl show-logind | grep HandleLid
Enter fullscreen mode Exit fullscreen mode

Expected output:

HandleLidSwitch=ignore
HandleLidSwitchExternalPower=ignore
HandleLidSwitchDocked=ignore
Enter fullscreen mode Exit fullscreen mode

If you see different values, the configuration is not being applied correctly.


Step 5: Test the Behavior (Important)

  1. Reboot the system
  2. Do not log in
  3. Close the laptop lid

✅ The system should continue running with no suspend or hibernate.


Troubleshooting: If the System Still Hibernates

Check the previous boot logs to see what triggered the action:

journalctl -b -1 | grep -i lid
Enter fullscreen mode Exit fullscreen mode

or:

journalctl -b -1 | grep -i hibernate
Enter fullscreen mode Exit fullscreen mode

Some laptops have firmware (ACPI) quirks that incorrectly report lid events.


Nuclear Option: Ignore Lid Events at the Hardware Level

If all else fails, you can block lid events using a udev rule.

Create the rule file:

sudo nano /etc/udev/rules.d/99-ignore-lid.rules
Enter fullscreen mode Exit fullscreen mode

Add the following line:

ACTION=="add|change", SUBSYSTEM=="power_supply", ATTR{type}=="Battery", ATTR{status}=="*", RUN+="/bin/true"
Enter fullscreen mode Exit fullscreen mode

Reload udev rules and reboot:

sudo udevadm control --reload
sudo reboot
Enter fullscreen mode Exit fullscreen mode

This method works even on systems with problematic firmware.


Summary

Component Role
systemd‑logind Handles lid events (pre‑login and post‑login)
Sleep targets Can force suspend or hibernate
Solution Configure logind and disable sleep targets

After applying these steps, Ubuntu Server will continue running normally when the lid is closed, making it suitable for server workloads on laptop hardware.


Applies to: Ubuntu Server 20.04, 22.04, 24.04 and newer

Top comments (0)