Sane screenlocking without a desktop environment
If you’re anything like me you probably lock your computer’s screen when you’re away from it. That is, you lock it both when you leave it while it’s running and you expect it to be locked when you suspend your system. And that’s where things got tricky when systemd started to replace virtually everything. Eventually it made the problem really simple, yet documentation was hard to find so here it is.
You’ve probably got a favourite screenlocker that can be run from the commandline and have it lock your screen. If you don’t, there are some options like i3lock and xlock. These programs work like this: Whenever you decide to lock the screen you run them and they lock the screen until you’ve entered your password. In most cases, you probably do not want to run them manually but instead have some shortcut and have it run automatically whenever the system goes to sleep.
Yesterday, I discovered xss-lock. That’s a tiny utility, that will run a custom screenlocker and listen to various events to actually lock the screen. Try this:
xss-lock -- i3lock
And then put your system to sleep. If your session is registered with logind, it’ll lock your screen. That’s pretty nice. Yet, how does one manually lock the screen? Until I’ve found the solution I tried locking my session using loginctl. This does work, but requires special privileges and didn’t seem very clean. Well, the solution couldn’t be much simpler. Just run:
xset s activate
xss-lock taps into the Xscreensaver API so it can be activated through it. That’s pretty nice.
To make changes permanent, I’ve added
xss-lock -- i3lock & to my
.Xsession and instructed Xmonad to run
xset s activate whenever I press the “lock screen”-key. It’s working flawlessy.