Using a USB flash drive with Linux

Extract from extremetech.com

To start off, you'll need to be logged in as root to set this up and to set permissions.

Verify that you have the needed kernel modules loaded. To find out what modules you have loaded, open a terminal window and type the following:

lsmod | more

The output of lsmod will look like this:


Module                  Size  Used by    Not tainted
nls_cp437               5116   0  (autoclean)
vfat                   13004   0  (autoclean)
fat                    38808   0  (autoclean) [vfat]
nls_iso8859-1           3516   0  (autoclean)
udf                    98400   0  (autoclean)
ide-scsi               12208   0 
soundcore               6404   6  (autoclean) [snd]
sd_mod                 13516   0  (autoclean)
lp                      8996   0  (autoclean)
parport                37056   0  (autoclean) [lp]
autofs                 13268   0  (autoclean) (unused)
e100                   60644   1 
ipt_REJECT              3928   6  (autoclean)
iptable_filter          2412   1  (autoclean)
ip_tables              15096   2  [ipt_REJECT iptable_filter]
sg                     36524   0  (autoclean)
sr_mod                 18136   0  (autoclean)
scsi_mod              107160   4  [ide-scsi sd_mod sg sr_mod]
ide-cd                 35708   0 
cdrom                  33728   0  [sr_mod ide-cd]
keybdev                 2944   0  (unused)
mousedev                5492   1 
hid                    22148   0  (unused)
input                   5856   0  [keybdev mousedev hid]
usb-uhci               26348   0  (unused)
usbcore                78784   1  [hid usb-uhci]
ext3                   70784   2 
jbd                    51892   2  [ext3]

By default, Red Hat loads usb-uhci and usbcore on startup. But you'll need to load an additional module called usb-storage in order to get a flash drive working. To do this, simply type:

modprobe usb-storage

Next, we'll need to define a mount point for the USB flash drive, which includes a directory for the mount point. So go to the /mnt sub-directory and create this sub-directory.

cd /mnt
mkdir /usbstick

Now we need to edit a file called fstab, which lives in the /etc directory. This file defines storage devices and the location of their mount-points.

Open the file using gedit, emacs or your text editor of choice. Its contents will look like this:


LABEL=/        /         ext3    defaults        1 1
LABEL=/boot    /boot     ext3    defaults        1 2
none           /dev/pts  devpts  gid=5,mode=620  0 0
none           /proc     proc    defaults        0 0
none           /dev/shm  tmpfs   defaults        0 0
/dev/hda3      swap      swap    defaults        0 0
/dev/cdrom     /mnt/cdrom  udf,iso9660 noauto,owner,kudzu,ro 0 0
/dev/fd0       /mnt/floppy     auto    noauto,owner,kudzu 0 0

We need to add a line to this file that reads:

/dev/sda1 /mnt/usbstick vfat user,noauto,umask=0   0 0

You can copy/paste the above line directly into your fstab file.

The "sda1" represents the device name that the kernel gives the USB flash drive when it gets plugged in.

Once you've added this line to the fstab file, save it and close your text editor.

Now we're almost ready to plug in your USB flash drive. Open a second terminal window and type:

tail -s 3 -f /var/log/messages

This command will poll the kernel's message log every three seconds, and displays the latest messages the kernel has spat out. This is a useful debug tool to make sure the USB flash drive has been enumerated, and assigned a device name. Generally, the device name will be:

/dev/sda1

Now, go ahead and plug your flash drive into the USB port.

Up and Running

Once you've plugged the drive in, look at the terminal window where you're monitoring the kernel's event messages and verify that it has enumerated the USB device. You should see something like this:

Aug 26 17:06:09 localhost kernel: hub.c: new USB device 00:1f.2-1, assigned address 4
Aug 26 17:06:13 localhost /etc/hotplug/usb.agent: Setup usb-storage for USB product d7d/100/100
Aug 26 17:06:13 localhost /etc/hotplug/usb.agent: Setup nomadjukebox for USB product d7d/100/100
Aug 26 17:06:13 localhost /etc/hotplug/usb.agent: Module setup nomadjukebox for USB product d7d/100/100
Aug 26 17:06:13 localhost kernel: SCSI device sda: 121856 512-byte hdwr sectors (62 MB) Aug 26 17:06:13 localhost kernel: sda: Write Protect is off
Aug 26 17:06:13 localhost kernel: sda: sda1 Aug 26 17:06:13 localhost devlabel: devlabel service started/restarted

The key event here is that the device was assigned as /dev/sda1. You can now mount the volume by typing:

cd /mnt
mount usbstick

If all has gone well, a disk icon will appear on your KDE/Gnome desktop and double-clicking on it will open a window that reveals the contents of your USB flash drive.

There's also a way to automate this process, where you can mount your USB flash drive without having to type anything at a command line. In Gnome, when you right-click anywhere on the desktop, one of the menu choices you have is Scripts, which is a quick and easy way to execute Bash scripts without having to open a terminal window. By default, there are no scripts in the folder that this menu points to, but there is an option to open that folder. Once in the folder, create a new text file and open it in your favorite text editor (we use gedit) to write the following script.

You can simply copy/paste what we have here into your Bash script:

#!/bin/bash

modprobe usb-storage
cd /mnt
mount usbstick

We run the modprobe command just to make sure that the usb-storage module is loaded. If it's already loaded, there's no harm done, and if it wasn't already loaded, now it is.

Now save the script as something like mount usbstick, and copy it into the /root/.gnome2/nautilus-scripts sub-directory.

From Gnome/KDE, right-click on this script and go to the Permissions tab dialog. Set the script as executable by the appropriate groups/users, and click OK.

You'll want this script to be available to non-root users, so be sure to copy it to their respective sub-directories:

/home/username/.gnome2/nautilus-scripts

Now when you right-click on the desktop and go down to the Scripts menu choice, in the Scripts sub-menu you should see your mount usbstick script.

If you have your USB flash drive mounted as a volume, right-click on it, and the bottom menu choice should be Unmount Volume. Go ahead and unmount the volume and physically remove the USB flash drive.

Now go ahead and re-insert the flash drive into an available USB port. Next, right-click on the desktop, go into the Scripts sub-menu and execute your mount usbstick script. The drive icon for your flash drive should appear on your desktop, and you're ready to pull bits off of it or write bits to it to carry home.