Lock/Unlock with USB Drive


Script to setup key and udev rules to lock and unlock a linux machine based on USB device insertion and removal.

Basic Usage

Copy the script to a folder on a USB drive. Run script as root. Select the USB drive identifier. Indicate if you want a key pair. It can work without, key pair just increases security. When installation completed, just plug and unplug drive to lock. Note: the system must be already logged in to the user in order for locking and unlocking to work.


  1. Choose a USB drive for usage. Size of installation after keys is less than 10k. Filesystem format only needs to be something the installation can read.

  2. Create a subfolder for the the script file. You can put it in any folder including root of the usb drive. I create a folder USBKEY to keep things organized.

  3. Copy script file to USB drive. It must run from the drive for proper path setups.

  4. Run script as root from the drive. My drive is mounted at /run/media/jtoebes/JToebes

  5. Select the entry that corresponds to the drive. Cursor Up/Down then press enter.

  6. Confirm selection.

  7. Decide if you want a keypair. Without a keypair any USB matching the ID will be able to unlock. With a keypair after plugin, the keys are checked to match.

  8. If a rule file already exist, you will be asked to append info. Current rules will be shown.

  9. If you have run install before for the same drive, it may ask if you want to replace the lock script.


Script can be found at