Alex Collins – Blog

Things I'm learning; lessons or otherwise…

Automatically lock your Linux machine via bluetooth

by atc on April 2, 2013, 7 comments

I had a requirement to lock my laptop at work automatically without having to touch it. The way devised by a friend was to enable the screensaver and its locking mechanism every time my phone’s bluetooth was out of range i.e. could not be seen via hcitool scan. It works *really well*! Now my laptop locks whenever I leave the room.

Update: It seems this was popular with reddit.com/r/linux, so I’ve uploaded the script to github and added unlocking support!

Here’s the code:

#!/bin/bash
DEVICE=the bluetooth MAC id
DEV_NAME="The actual device's alias/name"
INTERVAL=5 # in seconds
# Start xscreensaver if it's not already running
pgrep xscreensaver
if [ $? -eq 1 ]; then
echo "Starting xscreensaver..."
xscreensaver &
fi
# Assumes you've already paired and trusted the device
while [ 1 ]; do
opt=`hcitool name $DEVICE`
echo $opt
if [ "$opt" = "$DEV_NAME" ]; then
echo "Device found. Not locking"
else
echo "Can't find device $DEVICE ($DEV_NAME); locking!"
xscreensaver-command -lock
fi
sleep $INTERVAL
done

7 thoughts on “Automatically lock your Linux machine via bluetooth

  1. Does this have an impact on your phone’s battery life? I assume every time your PC initiates a scan, your phone has to respond with a bluetooth transmission. Plus possibly keep supplying power to the bluetooth interface (but irrelevant if you keep bluetooth switched on anyway).

    • When I get to work I always have bluetooth switched on via a Samsung Tectile. I’ve not noticed any increased usage but I’ve always had bluetooth on and it’s therefore always sucked additional battery juice.

  2. Pingback: Auto-lock via Bluetooth | 0ddn1x: tricks with *nix

  3. check out:

    * sys-auth/pam_blue
    Available versions: ~0.9.0
    Homepage: http://pam.0xdef.net/
    Description: Linux PAM module providing ability to authenticate via a bluetooth compatible device

  4. Hello would you mind stating which blog platform
    you’re working with? I’m planning to start my own blog in the near
    future but I’m having a difficult time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking
    for something completely unique. P.S Apologies for getting off-topic but I had to ask!

Comments are closed.