Hardcore Linux

Anything about Ubuntu, Centos, openSuSe and Fedora

Toggle USB Flash Disk access on/off on Ubuntu

In a series of test I’ve made on the built-in access permission control regarding the USB Flash Drives in Ubuntu, in all of my test it fails. The “Access External Storage Devices” control in Users & Groups Management, for me is not working (or I haven’t discovered how it really works). Then I just came up with this script:

#!/bin/bash
# Toggle on/off for USB Storage
# by https://hardc0l2e.wordpress.com
# KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", IMPORT{program}="usb_id --export %p"
# ------------------------------

TARGET_NAME="/etc/udev/rules.d/10-disable-usb.rules"
PATTERN='SUBSYSTEMS=="usb"'
CHECK_ENABLED=`grep "$PATTERN" /etc/udev/rules.d/* | grep "OPTIONS"`
ENV_CHECK="no"

if [ "$ENV_CHECK" = "yes" ]; then
 if [ "x$CHECK_ENABLED" = "x" ]; then
 echo 'KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", IMPORT{program}="usb_id --export %p", OPTIONS+="ignore_device"' > $TARGET_NAME
 fi
else
 if [ "x$CHECK_ENABLED" != "x" ]; then
 echo 'KERNEL=="sd*[!0-9]|sr*", ENV{ID_SERIAL}!="?*", SUBSYSTEMS=="usb", IMPORT{program}="usb_id --export %p"' > $TARGET_NAME
 fi
fi

To toggle ON/OFF just replace the variable ENV_CHECK=”yes” to disable and ENV_CHECK=”no” to enable USB Flash Disk access.

Done.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: