Is there Any program for locking and unlocking my Ubuntu machine using a flash- drive?

For instance, when I take out the usb flash drive, the computer locks automatically and when I put in the flash drive, the computer unlocks automatically.

Thank You in advance.

  • 2
    Remember that cloning usb stick is trivial task, even if you check for serial number.
    – Olli
    Commented Mar 3, 2011 at 9:04

3 Answers 3


I know it's a terribly late answer, just thought it might help future readers. I posted the answer at Locking with USB drive

Well, a module called PAM (Pluggable Authentication module) can be customized to achieve your need. A beautiful article is available on linuxconfig describing this in detail.

The steps are:

  1. Install PAM

    $ sudo apt-get install pamusb-tools libpam-usb
  2. Add USB device to PAM config

    $ sudo pamusb-conf --add-device <my-usb-stick>
  3. Select your volume and "Y" to save

  4. Define user for PAM auth

    $ sudo pamusb-conf --add-user <ubuntu-user>
  5. Select and "Y" to save

  6. Configure PAM

    $ sudo gedit /etc/pam.d/common-auth
  7. Add the line below and save

    auth    sufficient      pam_usb.so
  8. Test the PAM auth

    $ su ubuntu-user
  9. Lock when disconnected

    $ sudo gedit /etc/pamusb.conf
  10. Modify the block "user" block to look like:

    <user id="ubuntu-user"> 
          <agent event="lock">gnome-screensaver-command -l</agent> 
          <agent event="unlock">gnome-screensaver-command -d</agent> 



you can write a script

SUBSYSTEM=="usb", SYSFS{idProduct}=="PPPP", SYSFS{idVendor}=="VVVV", RUN+="/usr/sbin/usb-locking"

where PPPP and VVVV are values you can extract with lsusb.

Every identic product will match, but usb-locking could mount the device, and look at the drive itself for further legitimation - some file, some bytecode in there, the date ...

It's vulnerable if somebody else get's access to the stick, of course.

The script could further look every minute, whether the stick is still mounted, and lock if not.

  • 2
    Your rule should have ACTION==add. Also, why would you look every minute whether the stick is there? Just trigger on ACTION==remove and lock the screen whenever the stick vanishes. You should also add the stick's serial number and the partition's UUID to the rule. Commented Feb 3, 2016 at 17:12

Thanks for your suggestion.. I wrote a simple script utilizing the lsusb command and put in system -> preferences-> startup applications. The script is as follows..

# Script to automatically lock and unlock the computer when my usb pendrive is removed

LSUSB=`which lsusb`

if [ -z $LSUSB ]; then
    echo "No lsusb command found. exiting.. \n"
    exit 56

while :
    sleep 3
    echo "Running in loop"
    # Check the USB drive

    USB=`lsusb | grep Logitech`

    if [ -n "${USB}" ] ; then
        echo "USB Device: Transcend found"
        # find and kill any screensaver found.
        gnome-screensaver-command --deactivate

    # USB Device Not Found
    # Check if screensaver is running or not
    # if not running then start screensaver
    gnome-screensaver-command --activate


exit 0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .