23

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 '18 at 14:31
18

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:

[Unit]
Description=Lock the screen on resume from suspend

[Service]
User=jason
Environment=DISPLAY=:0
ExecStart=/usr/bin/slock

[Install]
WantedBy=suspend.target

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

| improve this answer | |
  • Thanks, I saw that example but did not realize the ExecStart=/usr/bin/slock bit refers to locking at first glimpse. – pldimitrov Jul 5 '13 at 1:24
  • If I suspend and come back, my X display manager has a screen lock, that's great. But what about my ttys? What if I'm logged in, and I suspended and I forgot to exit from my ttys, people could switch to there and continue their operations. – CMCDragonkai Jan 27 '16 at 5:25
  • @CMCDragonkai you want physlock then... – jasonwryan Jan 27 '16 at 5:31
  • 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. – Martin Ueding Mar 26 '16 at 16:58
  • 1
    @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 '18 at 19:51
1

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

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

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

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

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.

| improve this answer | |
1

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.

[Unit]
 Description=Lock
+Before=sleep.target

 [Service]
 User=mustapha
 Environment=DISPLAY=:0
 ExecStart=/usr/local/bin/slock

 [Install]
-WantedBy=suspend.target
+WantedBy=sleep.target
| improve this answer | |
0

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.