Skip to content

Instantly share code, notes, and snippets.

@vkobel
Last active July 25, 2019 14:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vkobel/219bdea2ebeb092e7b2d6ed091160d3d to your computer and use it in GitHub Desktop.
Save vkobel/219bdea2ebeb092e7b2d6ed091160d3d to your computer and use it in GitHub Desktop.
YubiKey lock screen when unplugged
## /etc/udev/rules.d/21-yubikey.rules
ACTION=="remove", SUBSYSTEM=="input", ATTRS{idVendor}=="1050", ATTRS{idProduct}=="0407", RUN+="/usr/local/bin/lockscreen.sh"
#!/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