Scripts to lock/unlock screen using usb keys (original github: mabgnu/usb-lock )
Go to file
Amin Bandali 5d0dbbf63d Update
correct the blog post url
2015-05-31 23:05:41 -04:00
.lockenabled project files added 2013-03-20 12:18:00 -04:00
91-usbkey.rules project files added 2013-03-20 12:18:00 -04:00
LICENSE add LICENSE (GPLv3) 2015-03-19 00:07:51 -04:00 Update 2015-05-31 23:05:41 -04:00 project files added 2013-03-20 12:18:00 -04:00 project files added 2013-03-20 12:18:00 -04:00

USB Lock

A very simple bash script to unlock or lock the screen if a specific USB flash drive is plugged or unplugged.

##How to The current version of script requires manual (but easy) configuration before usage. I'll try to extend the features in the next versions to make the initial configuration easier and without having to edit any files.

In the descriptions below, we first collect the required information for out udev file (idVendor, idProduct and Serial) and then copy the files to their correct locations.

Let's get started!

1) Clone the repository or download it as zip and extract it.

2) Connect your desired USB flash drive to your system. In the following command, replace Kingston with the name of the vendor of your flash disk then open a terminal and execute it:
lsusb | grep "Kingston"

The output should have a syntax similar to this:
Bus 001 Device 005: ID 0951:1643 Kingston Technology DataTraveler G3 4GB

Now we have idVendor and idProduct. In this example, idVendor is 0951 and idProduct is 1643.
We also have the Bus and Device, which are respectly 001 and 005. We are going to use them in the next step.

3) Now we need to get the unique serial number of our flash drive, so the system will only be unlocked with our flash drive, not any others. In the following command, replace 001 and 005 with the Bus and Device that you got from the output of the previous command. Then execute it:

lsusb -v -s 001:005 | awk -F " " '($1 == "iSerial") {print $3}' | grep -v ":" | grep .

The output is the serial number of your flash drive. For example 001373987CF5BA80D6210131.

4) Now we have all of the information that we need. Browse to the directory of the repository that you downloaded in the first step. Open this file: 91-usbkey.rules Initially, it looks like this:

KERNEL=="sd?1", ATTRS{idVendor}=="IDVENDORHERE", ATTRS{idProduct}=="IDPRODUCT", ATTRS{serial}=="SERIALHERE", RUN+="/usr/local/bin/"  
ACTION=="remove", ENV{ID_SERIAL_SHORT}=="SERIALHERE", RUN+="/usr/local/bin/"

We will modify it in the next step.

5) In the 91-usbkey.rules file, replace IDVENDORHERE with the idVendor that you got from the second step. Mine was 0951.
Then replace IDPRODUCTHERE with the one you got from the second step.
Replace the two SERIALHEREs with the serial number of your flash drive.

After doing the replacements, this is the new contents of my file:

KERNEL=="sd?1", ATTRS{idVendor}=="0951", ATTRS{idProduct}=="1643", ATTRS{serial}=="001373987CF5BA80D6210131", RUN+="/usr/local/bin/"
ACTION=="remove", ENV{ID_SERIAL_SHORT}=="001373987CF5BA80D6210131", RUN+="/usr/local/bin/"

6) Now we are ready to copy the file to its original location. Copy 91-usbkey.rules from the repository folder to /etc/udev/rulesd.d directory.
Note: You need root access to copy the file to the specified path.

7) Now we have to copy the bash scripts that do the actual lock and unlock; but before that, give them the execute permission:
chmod +x && chmod +x

After executing the above command, move the files to /usr/local/bin folder.
Note: You need root access to copy the files to the specified path.

8) Restart the udev service by typing sudo service udev restart.

9) Copy the .lockenabled (which is hidden) file from the repository to your home directory and give it execution permissions: chmod +x .lockenabled

10) Whenever you want to enable the lock, open a terminal and type ./.lockenabled, then unplug your flash drive.

11) Enjoy!

###Questions? If you have any questions, just leave a comment on the blog post and I'll try to help you.

##Bonus tips Bonus tip 1: You can monitor the system behaviour on plug and unplugging usb devices by executing udevadm monitor --environment --udev. Execute the command and then plug or unplug your usb device to see the logs.

Bonus tip 2: If you need information on all usb devices connect to your system, use lsusb -v.

Bonus tip 3: If you need even more information about your usb flash drive, you can use ls -l /dev/disk/by-id to get the sdX where your device is connected and then use udevadm info --query=all --path=/sys/block/sdX.