Laptop Does Not Sleep When the Lid Is Closed: External Monitor in Ubuntu
A laptop with Ubuntu 24.04 goes to sleep when you close the lid. One evening an external monitor is plugged in over HDMI, the lid is closed for the night, and in the morning it turns out that the laptop has been on all night.
This page finds the cause in the system log and shows what can be done about it. Tested on Ubuntu 24.04.5 LTS with GNOME Shell 46.0 on X11, on a Lenovo V510-14IKB. The outputs are shortened and month names are shown in English. Tools used here: systemctl, grep, xrandr, man.
Short answer. While an external monitor is connected, GNOME blocks the lid switch, and no setting turns that off in GNOME 46. Unplug the monitor before you close the lid, or suspend first and close the lid after: see What to do.
1. Did the laptop sleep?
The lid switch is watched by systemd-logind, and it is the service that puts the laptop to sleep. Its lines about the lid and suspend in the current boot; the pattern uspend matches both Suspending and suspend:
journalctl -b --no-hostname -u systemd-logind -g 'Lid|uspend' | tail -10
Nov 02 13:29:54 systemd-logind[1107]: Lid closed. Nov 02 13:29:54 systemd-logind[1107]: Suspending... Nov 02 13:32:52 systemd-logind[1107]: Lid opened. Nov 02 13:32:54 systemd-logind[1107]: Operation 'suspend' finished. Nov 02 17:07:51 systemd-logind[1107]: Lid closed. Nov 02 17:07:51 systemd-logind[1107]: Suspending... Nov 02 20:57:39 systemd-logind[1107]: Lid opened. Nov 02 20:57:43 systemd-logind[1107]: Operation 'suspend' finished. Nov 02 23:46:58 systemd-logind[1107]: Lid closed. Nov 03 08:33:36 systemd-logind[1107]: Lid opened.
The first two closes are followed by Suspending.... After the third one, at 23:46:58, there is nothing until the lid opens in the morning: logind did not even try. The kernel writes PM: suspend entry each time the laptop really goes to sleep:
journalctl -b -k --no-hostname -g 'PM: suspend'
Nov 02 13:29:57 kernel: PM: suspend entry (deep) Nov 02 13:32:52 kernel: PM: suspend exit Nov 02 17:07:56 kernel: PM: suspend entry (deep) Nov 02 20:57:41 kernel: PM: suspend exit
Two sleeps, both over by 21:00. From 23:46 to 08:33 the laptop stayed on with the lid closed.
2. What changed: a second monitor
On X11 the X server writes a line each time the size of the desktop changes, for example when a monitor is plugged in or a screen is switched off:
journalctl -b --no-hostname -g 'Allocate new frame buffer'
Nov 02 20:58:19 /usr/libexec/gdm-x-session[2203]: (II) modeset(0): Allocate new frame buffer 3840x1200 stride Nov 02 23:46:58 /usr/libexec/gdm-x-session[2203]: (II) modeset(0): Allocate new frame buffer 1920x1200 stride Nov 03 08:33:36 /usr/libexec/gdm-x-session[2203]: (II) modeset(0): Allocate new frame buffer 3840x1200 stride
At 20:58:19, half a minute after the laptop woke up, the desktop grew to 3840x1200: the 1920x1080 laptop screen and a 1920x1200 monitor side by side. At 23:46:58, the second the lid closed, it shrank to 1920x1200: GNOME switched the laptop screen off and kept the desktop on the external monitor. At 08:33:36 the lid opened and the laptop screen came back. The monitors now:
xrandr --listmonitors
Monitors: 2 0: +*eDP-1 1920/309x1080/173+0+0 eDP-1 1: +HDMI-1 1920/518x1200/324+1920+0 HDMI-1
eDP-1 is the laptop screen, HDMI-1 the external monitor. Both sleeps from step 1 happened before 20:58, without the monitor.
3. Who holds the lid: gsd-power
A program can tell logind that it handles a key or the lid switch itself. It does so by taking a lock that systemd calls an inhibitor. The locks on the lid switch:
systemd-inhibit --list | grep -E 'WHO|lid'
WHO UID USER PID COMM WHAT WHY MODE andrei 1000 andrei 2568 gsd-power handle-lid-switch External monitor attached or configuration changed recently block
gsd-power, the GNOME service for power settings, holds the handle-lid-switch lock in block mode, and the reason it gives is the external monitor. While such a lock is held, logind takes no action when the lid closes; man logind.conf says that its Handle*= settings are then irrelevant.
GNOME does this on purpose: with a monitor connected, a closed lid is meant for working on the external screen. gsd-power drops the lock only when no external monitor is connected or the session is not active, as its own debug message says:
strings /usr/libexec/gsd-power | grep 'uninhibiting lid'
no external monitors or session inactive for a while; uninhibiting lid close
4. logind: docked means ignore
logind has a rule of its own for the same case. It counts the laptop as docked when it sits in a docking station or when more than one display is connected, and a docked laptop follows HandleLidSwitchDocked instead of HandleLidSwitch. The values logind uses now:
busctl get-property org.freedesktop.login1 /org/freedesktop/login1 org.freedesktop.login1.Manager Docked HandleLidSwitch HandleLidSwitchDocked
b true s "suspend" s "ignore"
Docked is true because of HDMI-1, and HandleLidSwitchDocked is ignore by default. Setting it to suspend in a file under /etc/systemd/logind.conf.d/ does not help in a GNOME session: the gsd-power lock from step 3 comes first, and logind does not look at its Handle settings at all. The setting matters only where no program holds the lock, for example on a laptop used as a server without a desktop.
5. The GNOME key that does nothing
GNOME has a setting that promises exactly what is needed here:
gsettings describe org.gnome.settings-daemon.plugins.power lid-close-suspend-with-external-monitor
With no external monitors plugged in, closing a laptop's lid will suspend the machine (as set by the lid-close-battery-action and lid-close-ac-action keys). By default, however, closing the lid when an external monitor is present will not suspend the machine, so that one can keep working on that monitor (e.g. for docking stations or media viewers). Set this key to False to keep the default behavior, or to True to suspend the laptop whenever the lid is closed and regardless of external monitors.
But no program in GNOME 46 reads this key. A program asks for a setting by its name, so the name has to be inside the program file. gsd-power does not have it:
grep -c lid-close-suspend-with-external-monitor /usr/libexec/gsd-power
0
Neither do the other 20 gsd services in /usr/libexec, GNOME Shell and the Settings app: the key is left only in the schema that describes it. The same goes for lid-close-ac-action and lid-close-battery-action. Setting any of them changes nothing on Ubuntu 24.04.
6. What to do
GNOME 46 has no switch for this, so pick the way that fits:
- Working on the external monitor with the lid closed: leave it as it is, that is what the lock is for.
- Going away for the night: unplug the monitor before closing the lid. Without an external monitor gsd-power drops the lock a short while later, and the lid suspends the laptop again, as at 13:29 and 17:07 in step 1.
- Keeping the monitor plugged in: suspend first and close the lid after, with Suspend in the power menu at the top right of the screen or with the command below.
systemctl suspend
Checklist
- Laptop on all night with the lid closed: look for Suspending... after Lid closed. in journalctl -b -u systemd-logind -g 'Lid|uspend'
- Count the monitors: xrandr --listmonitors shows more than the laptop screen.
- Find who holds the lid: systemd-inhibit --list | grep lid
- gsd-power with External monitor attached is GNOME working with the lid closed on purpose.
- Neither HandleLidSwitchDocked nor the lid-close-* GNOME keys change it under GNOME 46.
- Unplug the monitor first, or suspend first, then close the lid.
- The laptop sleeps, but the screen stays black after opening the lid: that is another problem, see Black Screen After Closing the Lid in Ubuntu 24.04.
Article author: Klavdiya Kondakova