Every time I strat my Ubuntu Desktop I used to plug in my USB device for data storage. While leaving the workplace I remove USB storage device but forgot to lock the desktop. Hence in the search of utility to lock / unlock desktop using USB device I found below script.

Firstly Install xscreensaver

apt-get install xscreensaver

Then run lsusb and locate your USB Device. In my case, it was the last Device i.e Bus 004 Device 002: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash Flash Drive
Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub
Bus 004 Device 002: ID 058f:6387 Alcor Micro Corp. Transcend JetFlash Flash Drive

Create a file name autoUnlock in your home directory (Avoid using root user).

touch /home/vidyadhar/autoUnlock

Copy below lines in the /home/vidyadhar/autoUnlock

#!/bin/bash
# A simple shell to lock / unlock your Desktop
# Ubuntu
# ----------------------------------------------------------------------------
# Original Script by Echowrap http://echowarp.neomenlo.org
# Modified by Vidyadhar D S <http://www.techienote.com/>
# vidyadhards@gmail.com techienote.com@gmail.com
# ----------------------------------------------------------------------------
#Replace with the ID of your USB device
id="ID 058f:6387 Alcor Micro Corp. Transcend JetFlash Flash Drive"
 
#runs every 2 seconds
for ((i=0; i<=30; i++))
do
if [ -z "`lsusb | grep "$id"`" ]
then
 
    echo "Device is NOT plugged in"
 
    if [ -n "`DISPLAY=:0.0 gnome-screensaver-command --query | grep "is active"`" ]
    then
    if [ -e /tmp/autoUnlock.lock ]
    then
    #stop locking the screen
    rm /tmp/autoUnlock.lock
 
fi
 
elif [ -e /tmp/autoUnlock.lock ]
then
 
    DISPLAY=:0.0 notify-send -t 5000 âicon=dialog-info "Device Disconnected""Bye!"
 
    #lock the desktop
    DISPLAY=:0.0 gnome-screensaver-command --lock
 
    rm /tmp/autoUnlock.lock
 
fi
else
 
    echo "USB Plugged In"
    if [ ! -e /tmp/autoUnlock.lock ]
    then
    DISPLAY=:0.0 gnome-screensaver-command --deactivate
    DISPLAY=:0.0 notify-send -t 5000 --icon=dialog-info "Device Connected" "Welcome Back!"
    touch /tmp/autoUnlock.lock
 
    #Uncomment the 3 following lines if you would like your computer to remind you if you lock your screen without disconnecting the device
    echo "Don't forget your device!" > /tmp/usbReminder
    DISPLAY=:0 festival --tts /tmp/usbReminder
    rm /tmp/usbReminder
    fi
 
fi
sleep 2
done

Make it executable

chmod +x /home/vidyadhar/autoUnlock

Add a cron entry in user’s crontab

crontab -u vidyadhar -e
* * * * * bash /home/vidyadhar/autoUnlock & >/dev/null 2>&1

Restart the crontab

/etc/init.d/cron restart

Now remove your USB device, your desktop should be get lock now.

Lock / Unlock Ubuntu Desktop using USB drive

One thought on “Lock / Unlock Ubuntu Desktop using USB drive

  • October 11, 2015 at 12:06 pm
    Permalink

    My scenario is like this,
    whenever someone inserts a pendrive, the screen will be locked and it will ask for a password(not login password but some secret password that is known to some trusty users only).
    If correct password is entered then only the screen will be unlocked otherwise it will be locked .

    Can you help me please for solving this problem.

    Appreciate your help

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Notify me of followup comments via e-mail. You can also subscribe without commenting.

Recommend on Google