Daniel López Azaña

Theme

Social Media

Blog

GNU/Linux, Open Source, Cloud Computing, DevOps and more...

Automatically lock/unlock your screen by Bluetooth device proximity

Logo Blueproximity

One of the most basic and common security measures that you can adopt in your working environment is to protect your computer by automatically locking the screen after a certain period of inactivity, so that it is necessary to enter a password to unlock. But there is a more effective way, and that is to configure your Linux machine so that the distance or proximity of a Bluetooth device you own, for example your cell phone or a smartwatch, automatically causes locking or unlocking the screen without having to enter any password on keyboard.

In an ideal world you would always remember to lock the screen when you leave your workplace to prevent another person from manipulating your computer during your absence. However, sometimes it will happen that you will neglect and forget to do so , compromising your information security. But even though you always remember to lock the screen, unlocking it regularly over and over again in the presence of other people or in uncontrolled environments is also a major security risk, as you will be exposing your password every time you type it , either because someone next to you sees you writing it or because a surveillance camera records the password you enter on your keyboard.

Thus, with the solution I propose here you will minimize both risks: forgetting about manually lock the screen and exposing your password every time you type it.

Installing and configuring Blueproximity

Smartphone Bluetooth

Smartphone Bluetooth

Blueproximity is an application that constantly monitors signal strength of a Bluetooth device linked to your computer, so that if you move away from your workplace carrying your smartphone and the signal strength falls below a given threshold for a given period of time (both parameters can be configured as you like), a command of your choice will run , normally one whose purpose is to lock the screen. Otherwise, when the signal level recovers because you get closer to the computer, the screen unlock command will be executed without you having to do anything.

Installing Blueproximity application is very easy because there are packages available for main Linux distributions and they can be installed in the usual way:

Debian/Ubuntu:

~$ sudo apt-get install blueproximity

Fedora/Red Hat/Centos:

~$ sudo yum install blueproximity

Arch Linux:

~$ sudo yaourt -S blueproximity –noconfirm

Common locking/unlocking commands

Once installed the blueproximity package you will have to proceed to link your Bluetooth device to your computer , configure the thresholds for the signal strength level and set the locking and unlocking commands that best fit your desktop environment and the goals you want to achieve.

Next follow up the most common locking/unlocking commands according to the graphical environment you work with. The third command in the configuration dialog box (Proximity command) runs periodically according to the selected time interval as long as your Bluetooth device is within range. This can be used to run a command that simulates user activity so that the session will never be locked even if there is no user activity but you are nearby. You can use here just the same command as for unlocking.

Gnome

Blueproximity is originally a Gnome application, so by default it will be configured with the following commands:

Locking:

gnome-screensaver-command -l

Unlocking:

gnome-screensaver-command -d

Cinnamon

Locking:

cinnamon-screensaver-command -l

Unlocking:

cinnamon-screensaver-command -d

Mate

Locking:

mate-screensaver-command -l

Unlocking:

mate-screensaver-command -d

KDE

In this case there are two commands because the second one corresponds to older versions of KDE prior to version 4.13.

Locking:

dbus-send –type=method_call –dest=org.freedesktop.ScreenSaver /ScreenSaver <span class="skimlinks-unlinked">org.freedesktop.ScreenSaver.Lock

qdbus org.freedesktop.ScreenSaver /ScreenSaver Lock</span>

Unlocking:

qdbus | perl -ne ‘qx/kquitapp $1/ if /(kscreenlocker_greet-\d+)/’

killall -9 kscreenlocker

Plasma Desktop 5

Locking:

loginctl lock-session

Unlocking:

loginctl unlock-session

Other

Locking:

xscreensaver-command -lock

Unlocking:

pkill xscreensaver

Caveats

It is important to note that the blueproximity package has several dependencies that must be satisfied if your Linux distribution doesn’t bring them by default:

bluez-utils python-gtk2 python-glade2 python-configobj python-bluez

It may happen that although the application is properly installed and configured and all dependencies issues are resolved, the Bluetooth driver may appear as Soft blocked in your system:

~# rfkill list 
1: phy0: Wireless LAN 
        Soft blocked: no 
        Hard blocked: no 
2: asus-wlan: Wireless LAN 
        Soft blocked: no 
        Hard blocked: no 
3: asus-bluetooth: Bluetooth 
        <strong>Soft blocked: yes</strong> 
        Hard blocked: no 
5: hci0: Bluetooth 
        <strong>Soft blocked: yes</strong> 
        Hard blocked: no

If your computer has a physical button to enable or disable Bluetooth then it is the Hard blocked parameter which could be set to yes.

To unblock the Bluetooth driver, use the following command:

~# rfkill unblock bluetooth

In other cases, even if you unblock the driver, it remains off (power off). This happens for example if you are working on a laptop that is not plugged into its power adapter and is running on battery. It may happen that the power-saving settings that your Linux distribution brings by default cause the Bluetooth driver to power off automatically when the system boots and you have to power it on by hand:

~# bluetoothctl  
[bluetooth]# power on
Changing power on succeeded

Since the above command is interactive, you might better prefer the following one that will not require any user intervention so it can be used within a script:

~# hciconfig hci0 up

Other interesting things that can be done automatically in addition to locking/unlocking the screen

There are other interesting things you can do when your linked Bluetooth device is approaching or moving away from your box other than locking or unlocking the screen. You can configure Bluproximity to run more sophisticated locking and unlocking commands, or even create your own scripts to do many things at once. Here are some ideas that may be useful:

Turn the screen on and off

Apart from performing locking or unlocking, the screen can also be turned off when you move away and turned on again when you approach in order to save energy. To do this, you will add the on/off command to the lock/unlock commands we saw above. For example:

Locking:

loginctl lock-session && xset dpms force off

Unlocking:

xset dpms force on && loginctl unlock-session

Send a push notification to your smartphone

For this example the pushbullet-cli library is used to send push notifications to your mobile phone from the command line through the Pushbullet service:

Locking:

loginctl lock-session && pb push "Screen locked at $(date +%H:%M:%S) on $(date +%d/%m/%Y)"

Unlocking:

pb push "Screen unlocked at $(date +%H:%M:%S) on $(date +%m/%d/%Y)" && loginctl unlock-session

Stop/start playing Spotify songs

Locking:

loginctl lock-session && dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Pause

Unlocking:

dbus-send --print-reply --dest=org.mpris.MediaPlayer2.spotify /org/mpris/MediaPlayer2 org.mpris.MediaPlayer2.Player.Play && loginctl unlock-session

Change your chat client status to Not available/Available

Locking:

loginctl lock-session && purple-remote "setstatus?status=away&message=No disponible"

Unlocking:

purple-remote "setstatus?status=available&message=" && loginctl unlock-session

Make your webcam start/stop recording video

Locking:

loginctl lock-session && streamer -q -c /dev/video0 -f rgb24 -r 3 -o ~/outfile.avi
loginctl lock-session && ffmpeg -f oss -i /dev/dsp -f video4linux2 -s 320x240 -i /dev/video0 out.mpg

Unlocking:

pkill streamer && loginctl unlock-session
pkill ffmpeg && loginctl unlock-session

If you can think of more examples do not hesitate to make your contribution by writing a comment!

Automation Bluetooth
Daniel López Azaña

About the author

Daniel López Azaña

Tech entrepreneur and cloud architect with over 20 years of experience transforming infrastructures and automating processes.

Specialist in AI/LLM integration, Rust and Python development, and AWS & GCP architecture. Restless mind, idea generator, and passionate about technological innovation and AI.

Related articles

hot-button-ssh-command-widget-icon

Linux remote control from your smartphone via SSH button widgets

In this post I will tell you about an Android app that is extremely useful to run commands remotely on a Linux computer: Hot Button SSH Command Widget. This application allows you to launch conveniently any command you want on a remote computer through SSH only with the push of a button on the screen of your mobile phone or tablet. This not only will facilitate automation of repetitive tasks, but also is very interesting from the perspective of security for the same reasons I exposed in my Automatically lock/unlock your screen by Bluetooth device proximity post. It will allow you for example to lock and unlock the screen without having to type your password again and again in sight of other people.

July 15, 2017
keepassx

1-click automatically open a Keepassx database

Those who use the tool KeePassX (KeePass clone for Linux and Mac OS X) to safely save passwords and login details at one single place will have seen many times on the need for rapid access to their repository of passwords with a single click. However, by default when accessing KeePassX none repository is open, or one is open that does not correspond to the one we want to check, so often it’s necessary to browse the file system to locate and select the corresponding file. If in addition to a master password we use a key file to improve security, we have to repeat the same process over and over again to locate and select the key file.

November 2, 2012
ASLR concept

Differences between ASLR, KASLR and KARL

Following the release of the Linux Kernel 4.12, which for the first time brings the KASLR feature enabled by default, and almost simultaneously the announcement of a feature called KARL in OpenBSD, I found it interesting to clarify the differences between these security techniques, since I think that the combination of both will be very important in the future of system security as they will prevent exploiting vulnerabilities related to memory corruption (buffer-overflow).

July 7, 2017

Comments

Dr. Udo Junker February 3, 2019
Sounds nice and easy, will try it immediately. My problem, I have two-three persons (nurses) working on the same workstation. Is there a way of unlocking the system if any of a (certain number of) smartphones or other BT devices are present at the WS?. Thanks a lot!
Daniel February 10, 2019
Sure! That's the point of applications like Blueproximity.
Kesslar April 8, 2019
Does this use bluetooth LE or just regular bluetooth?
Daniel April 10, 2019
Both of them, Blueproximity is only a piece of software that works independently on top of hardware devices connected to your system, which can be LE or not.
Adam December 3, 2019
can you share a link where to download blueproximity sw?
Daniel December 3, 2019
You will find it as part of the package repository of your Linux distribution.
Murat January 20, 2020
Thanks, I'm planing to use it for media-center to lock/unlock.
Mr.B June 1, 2020
Hi, I'm currently using Ubuntu 18.04 LTS while launching the program, there was an error. tail -f /var/log/syslog blueproximity.desktop[28838]: File "/usr/share/blueproximity/proximity.py", line 82 blueproximity.desktop[28838]: print _("The program cannot import the module gobject.") blueproximity.desktop[28838]: ^ blueproximity.desktop[28838]: SyntaxError: invalid syntax
Joao June 17, 2020
Is it possible to use a wearable (mi band)?
Daniel June 18, 2020
Of course! Any Bluetooth device that can be paired to your computer can be used.
Kesslar October 13, 2020
What repository is this kept in? It doesn't show up in any of the repositories with Linux-mint 20

Submit comment