what is the most straightforward way to lock the screen AND suspend when closing the lid? I'm using event hooks in /etc/systemd/logind.conf and successfully suspend upon closing the lid. However I'm lost when it comes to the locking part - am I supposed to enable some screensaver service and how can I trigger two events at once in logind.conf?

  • Outside of scope so I'm commenting: in i3 it's very easy to assign keybindings to commands. Since mod+L was already assigned to blurlock for me, it was easy to have mod+S be blurlock && systemctl suspend -i instead. So, not when closing the lid, but works very well to choose whether I just want to lock, or lock and suspend.
    – pzkpfw
    Dec 20, 2018 at 14:31

4 Answers 4


There are a couple of examples in the Arch Wiki.

Basically, it involves creating a service file for your screen locker and ensuring it is hooked to either the suspend, hibernate or sleep targets.

If you use a simple screen locker like slock, /etc/systemd/system/lock.service would look like this:

Description=Lock the screen on resume from suspend



Other examples on the wiki have more complex options, including shutting down and bringing up other services, etc.

  • Thanks, I saw that example but did not realize the ExecStart=/usr/bin/slock bit refers to locking at first glimpse.
    – pldimitrov
    Jul 5, 2013 at 1:24
  • 1
    This does work, but only if you are the only user of the system. A cleaner solution is to use xss-lock (available in Debian, Arch Linux; also Fedora next week) and run that as a user together with i3lock or slock. Mar 26, 2016 at 16:58
  • 2
    @MartinUeding and @jasonwryan, User is hardcoded. The assumption that this machine will only be used by jason (or someone who knows his password, like his girlfriend, for example). Ideally, we'd want User to be whoever initiated the suspend, but I haven't found a solution for that (yet).
    – Rolf
    Mar 29, 2018 at 19:51
  • 2
    Note that this will fail if the laptop unexpectedly cannot suspend (you may not notice, since you closed the lid): geoff.greer.fm/2018/01/02/linux-laptop-locking
    – Ben Ruijl
    Sep 29, 2018 at 7:37
  • 1
    user units cannot get triggered by system targets directly -- so there's no straightforward way for user units to start when locking/sleeping. systemd-lock-handler works around this by translating system events to user events.
    – WhyNotHugo
    May 24, 2021 at 21:39

While jasonwryan's reply is correct, it is incomplete. In order to safely lock after suspending, instead of before - where a non-root process may prevent the kernel from suspending, you must add a Before= instance which forces systemd to wait for the ExecStart call to slock to start before suspending. Using sleep.target covers suspend, hibernate, and hybrid sleep.




If you use openrc with elogind is there an alternative solution (which is not systemd dependent):

# /lib/elogind/system-sleep/lock.sh
# Lock before suspend integration with elogind

export XAUTHORITY="$userhome/.Xauthority"
export DISPLAY=":0.0"

case "${1}" in
            su $username -c "/usr/bin/slock" &
            sleep 1s;

ref: https://gist.github.com/ryukinix/bd0c1ddcbbafdb4149ae70e41b7c822b

I'm posting this because was very difficult to find it a proper way that it works and this thread appears on first results of google about "lock after suspend" or whatever.


All answers presented here have one fundamental flaw: They set the DISPLAY variable irespectively of what seat/session is active. And they involve writing your own systemd service file.

Thus, I suggest using the fact that logind (part of systemd, I believe) sends DBUS notifications before suspending. If you subscribe to these messages, you can start the screen locker from within you desktop environment and really lock the correct screen/session.

Additionally, there are programs like https://github.com/swaywm/swayidle that already implement this. For example, I've got this in my window manager's config:

exec swayidle -w \
    timeout 300  'swaylock -i $wallpaper' \
    timeout 600  'swaymsg "output * dpms off"' \
    resume       'swaymsg "output * dpms on"' \
    before-sleep 'swaylock -i $wallpaper' \
    lock         'swaylock -i $wallpaper'

To automagically lock the screen after some time, before suspending and whenever screen locking is requested. I've set HandleLidSwitch=lock in /etc/systemd/logind.conf to have my laptop locked when I close it.

Edit: You will also need to enable DBUS support your desktop session. I launch my window manager (sway) directly from the console, so my call became

exec dbus-run-session sway

Similarly, you could probably (I did not properly test this part) start your X window manger, i3 in the example using the following line in you ~/.xinitrc

exec dbus-launch i3 > /dev/null

Edit: Please note, that this answer is given much later. I do not if this was already possible when these answers were given and do not wish to discredit any of the other authors.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .