in Articles

Lock and unlock your Gnome screensaver using your Bluetooth phone

I have created a small shell-script which monitors the Bluetooth space around you for a phone or other bluetooth device. If you step outside the perimeter with the device, your computer will automatically be locked. When you later walk up to your computer, it will unlock automatically.

You need bluez-utils installed. Ubuntu users can install it like this:

sudo apt-get install bluez-utils

You of course need a supported bluetooth device on your computer as well. You will need to set your bluetooth address in the script. It can be found by running hcitool scan with your phone in discovery mode. Start the script using your session manager, or manually by running a command like this:

sh bluelock.sh &

If there is demand, I will make it a proper Python application, but this works fine for me.

Note that you may want to disable the normal screensaver which usually kicks in when the computer is idle, since the script will unlock the screensaver if your phone is in proximity after a couple of seconds anyhow.

A Python version can probably be made smarter, but the real problem is that hcitool needs to be run as root to make some really cool things happen. Suid root on hcitool – acceptable?

Leave a Reply

33 Comments

  1. [quote comment="42300"]That is seriously awesome, Johnny. A simple shell script serving the utmost in security. I applaud thee.[/quote]

    Thanks! Shell-scripts can do useful things sometimes.

  2. Neat-o-job Johnny. Looked at this stuff a while back but your script is a very nifty wrapper. Added it as a gnome session start-up application the job is done.

    Thanks again,

    Mark.

  3. [quote comment="42327"]Neat-o-job Johnny. Looked at this stuff a while back but your script is a very nifty wrapper. Added it as a gnome session start-up application the job is done.

    Thanks again,

    Mark.[/quote]

    Thanks! There are of course lots of things that could be better but it’s just a hack, and I don’t want to rely on having root privileges.

  4. Thank you for this script…
    Good job :)
    I just have changed “$SCREEN_CMD -a” to “$SCREEN_CMD -l” to lock
    Probably due to a change into “gnome-screensaver” package ? (mine is 2.18.1-0ubuntu)

  5. [quote comment="44160"]Thank you for this script…
    Good job :)
    I just have changed “$SCREEN_CMD -a” to “$SCREEN_CMD -l” to lock
    Probably due to a change into “gnome-screensaver” package ? (mine is 2.18.1-0ubuntu)[/quote]
    Thanks!

    -l, –lock Tells the running screensaver process to lock the screen immediately
    -a, –activate Turn the screensaver on (blank the screen)

    Not sure what the difference is, but they both do the same thing on my computer. Thanks for mentioning this.

  6. Hi Johnny,
    On my computer -a just activate a “black blank screen” but the screen is not locked and moving the mouse bring back the desktop.
    With -l I have the “black blank screen” and moving the mouse ask me for the password to unlock which is of course we want :)

  7. [quote comment="44247"]Hi Johnny,
    On my computer -a just activate a “black blank screen” but the screen is not locked and moving the mouse bring back the desktop.
    With -l I have the “black blank screen” and moving the mouse ask me for the password to unlock which is of course we want :)[/quote]
    Thanks! Then maybe -l should be default in the script.

  8. [quote comment="45046"]Damn! That’s a nice script.
    No probs at all with gutsy and my trusty Nokia E61.

    Regards,
    Per[/quote]
    Great, and thanks!

    [quote comment="45372"]Please, oh, please tell me this is going to work with KDE as is or in the near future.[/quote]
    It will not work with KDE right now, but I could probably add support for it. I just have to find a running KDE system to test it. :)

  9. Awesome script, thanks!

    I changed RETVAL=`$BT_CMD` to RETVAL=`timeout 3 $BT_CMD` (requires ‘timeout’ on debian/ubuntu: sudo apt-get install timeout) to shorten the amount of time it takes to lock (hcitool returns in about 3 seconds when the device is in range, but takes about 18 to time out when it’s not found)

    As an added bonus, it seems to have shortened (slightly) the lock/unlock threshold range.

  10. Hi there,
    Could really use some help on how to replace the “gnome-screensaver-command” since I use KDE instead of GNOME (GUTSY-GIBBON). Would appreciate any help!!
    Regards..

  11. [quote comment="47476"]Awesome script, thanks!

    I changed RETVAL=`$BT_CMD` to RETVAL=`timeout 3 $BT_CMD` (requires ‘timeout’ on debian/ubuntu: sudo apt-get install timeout) to shorten the amount of time it takes to lock (hcitool returns in about 3 seconds when the device is in range, but takes about 18 to time out when it’s not found)

    As an added bonus, it seems to have shortened (slightly) the lock/unlock threshold range.[/quote]
    Great! I didn’t know about the timeout utility, but it seems perfect for this. :)

    [quote comment="47726"]Hi there,
    Could really use some help on how to replace the “gnome-screensaver-command” since I use KDE instead of GNOME (GUTSY-GIBBON). Would appreciate any help!!
    Regards..[/quote]
    Check the arguments for the kde screensaver and just replace the commands with the KDE ones. I don’t have a KDE machine nearby to try, but I can find one if you can’t get it working.

  12. I just downloaded this simple and great tool. I’m using KDE 3.x and the provided kbluelock is just to buggy to be used. so i just changed some lines in the script to fit to the kde-desktop:

    replace the corresponding lines with following:

    SCREEN_CMD=”dcop”
    SCREEN_LOCK=”$SCREEN_CMD kdesktop KScreensaverIface lock”
    SCREEN_UNLOCK=””

    unfortunately i didn’t find the command to unlock again. so IT JUST WILL LOCK YOUR SCREEN, NOT UNLOCK ON REENTERING WITH YOUR PHONE. maybe someone knows how to do this ?

  13. with that screen unlock command it bypasses pam :/ I wonder if there is any way to just send the command to attempt unlocking. You might think this sounds dumb but for example I also have pam_usb so if a specific usb key is inserted I can use that as authentication for pam and essentially bypass the need for a password. It would be nice when I walk up to my workstation with my phone that it only unlocks if it passes pam auth. That way if I leave my key in my workstation and walk away it will lock and when i come back it will unlock. And if I leave my phone somewhere but have my key with me and someone else takes my phone to my desk it does not unlock my workstation as it did not pass the authentication. ….. mmmm i may have thought of a way to take care of that.

  14. I wouldn’t want an auto-UNlock, because anyone “borrowing” my phone would have access to my system..

    Mega-thanks to all who contributed to the whole thread, as I work mainly in KDE the KDE variant is the one I need.

    Personally I think this should become a default part of GNOME and KDE because it’s simple as well as effective!

  15. Yes, the develloper looks to be active on the project :)

    BTW, does one of you is aware of a small bluethooth device that could be used like the cell phone to “broadcast bluethooth” ..? I mean something small like http://www.dansdata.com/pclock.htm ? <- this one is not bluethooth but is smaller than a cell phone.. Any ideas ?

  16. Hi guys,

    hm, the smallest device I’ve heard from my users so far is an in-ear bluetooth headset.
    Btw, I find it interesting how many people got the same idea about bluetooth proximity detection around the same time. (My idea came when I surfed on thinkgeek.com and saw the PC lock which is about the same but a lame overlay application on the windows desktop (does not use the screensaver) and it required special hardware (and would probably not work on Linux). So I decided to learn python and be creative with bluetooth to see where I get. BlueProximity is the result and as I just finished my bachelor thesis I will get back to development next week…)

    Bye
    Lars

  17. Good idea for the “bluetooth headset”…
    I will take a look on that…

    Happy to hear that you will be back to the development :)
    Thank you

  18. Is this still available anywhere? The filedropper link doesn't seem to work.

    I understand that you are suggesting using Blue Proximity instead, but I'd like to have a look at your shell script approach if it's still available.

    Thanks.