Coder Social home page Coder Social logo

usb-lock's Introduction

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/onusbplug.sh"  
ACTION=="remove", ENV{ID_SERIAL_SHORT}=="SERIALHERE", RUN+="/usr/local/bin/onusbunplug.sh"

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/onusbplug.sh"
ACTION=="remove", ENV{ID_SERIAL_SHORT}=="001373987CF5BA80D6210131", RUN+="/usr/local/bin/onusbunplug.sh"

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 onusbplug.sh && chmod +x onusbunplug.sh

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.

usb-lock's People

Stargazers

cedricfarinazzo avatar  avatar Zenedith avatar Mateusz "Serafin" Gajewski avatar Piotr Wittchen avatar Wojtek Gawroński avatar Desmond avatar Ryan E avatar Michael Zhan avatar Wang Kang avatar Rahul avatar

Watchers

Julien Vitard avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.