22

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
17

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.

  • 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
0

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.

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.