How to lock screen on suspend?

Hello,

I'm sorry because I did not provide an explanation.

This little loginctl--lock-sessions.service systemd unit is only invoked when computer reached the systemd sleep.target which is called when computer goes to sleep, suspend, hibernate, and hybrid-sleep using systemd.

loginctl--lock-sessions.service is never invoked by the screensaver.
You can rename the loginctl--lock-sessions.service by using the name you want.

loginctl--lock-sessions.service rely only on systemd mechanism and just launch logger and loginctl lock-sessions. Unit is invoked by the sleep.target which is called by systemd-hibernate.service, systemd-suspend.service, systemd-hybrid-sleep.service ... without additional package ... only the base system.

As example: systemd-hibernate.service

This file is part of systemd.

systemd is free software; you can redistribute it and/or modify it
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.

[Unit]
Description=Hibernate
Documentation=man:systemd-suspend.service(8)
DefaultDependencies=no
Requires=sleep.target
After=sleep.target

[Service]
Type=oneshot
ExecStart=/lib/systemd/systemd-sleep hibernate

This unit do the following actions:
First one :

ExecStartPre=/usr/bin/logger --tag "loginctl" --priority auth.info "Session: Locking all user sessions"

This will cause logger to write to the journal log an event:

juil. 05 04:46:46 ...... loginctl[20960]: Session: Locking all user sessions

Second one:

ExecStart=/bin/loginctl lock-sessions

loginctl --help

lock-sessions Screen lock all current sessions

So after restoring your computer from suspend or hibernate you can search in journal log if loginctl--lock-sessions.service has been launched by the sleep.target and if the sleep.target has been invoked ... using journalctl

Using the logger tag:

$ journalctl -t loginctl

Using the systemd unit name:

$ journalctl -u loginctl--lock-sessions.service

This is only a little workaround to force screen to be locked on sleep.
You can replace loginctl lock-sessions with whatever you want coming from additional screen locking tools.

Regarding the mate screensaver (inactivity screensaver), i don't know what to say ... because I have troubles with it ... sometimes screen is well locked and sometimes not :frowning: .... so now WIN+L is a reflex !

Kr,

2 Likes