Last active
July 25, 2019 14:38
-
-
Save vkobel/219bdea2ebeb092e7b2d6ed091160d3d to your computer and use it in GitHub Desktop.
YubiKey lock screen when unplugged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## /etc/udev/rules.d/21-yubikey.rules | |
ACTION=="remove", SUBSYSTEM=="input", ATTRS{idVendor}=="1050", ATTRS{idProduct}=="0407", RUN+="/usr/local/bin/lockscreen.sh" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
## /usr/local/bin/lockscreen.sh | |
SESSIONS="$(loginctl list-sessions --no-legend)" | |
# if '/tmp/.noyubilock' file exists, disable autolock at usb unplug | |
if [ -f /tmp/.noyubilock ]; then | |
exit 0 | |
fi | |
while read -r line; do | |
read sess_id _ _ _ tty <<< "$line" | |
if [ -z "${tty}" ]; then | |
# if X11 session: just lock it | |
loginctl lock-session "$sess_id" | |
else | |
# if TTY session: terminate it (logout) | |
loginctl terminate-session "$sess_id" | |
fi | |
done <<< "$SESSIONS" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment