dotkam.com stats

Find USB flash drive device in Linux

Find USB drive in Linux / Unix

Recently I have written a quick “how to” on restoring, formatting a USB flash drive. The “how to” had a lot of hits from different places since then, mostly from GOOGLE, of course, but some from independent bloggers, like my blogging self.

One of such places was www.newlinuxuser.com. Although the guide “saved them” (welcome :) ) they had a very constructive critique that one thing the guide missed was how to actually find which device to restore / format.

Hence I decided to write a new little 2 step “how to” that will teach you just that.

Step 1. System Log is your friend, listen to what it has to say…

Imagine you close your eyes, and someone puts an apple in your mouth – would you be able to identify what the heck was put into your mouth? For most people the answer would be “yes”. ( If you’d like to experiment, feel free :) . The thing is once you bite on that apple, your brain goes through many lines of code (given that we are written in C), finds that match, and reports:

"The object in your mouth was identified as an Apple - we've had that before.
  I just talked to the stomach, it knows how to digest it."

You would think that your Linux/Unix system is any different? Well, not really.

Right after you insert a USB device into a slot, Linux/Unix will try to read, and identify it. While doing that, it will also assign it to a particular device as “/dev/particular-device”. This device is exactly the information we need, so we can talk to it, and mount it.

Although most people would approach it with running a dmesg, and look at the output, I prefer a more natural OS way to look at things – we’ll look directly in the eye of a System Log!

Let’s use “tail -f”, so we can see real time system log updates:

tail -f /var/log/messages

Now insert your USB drive into a slot and you should see the output similar to:

Dec  5 14:53:19 your-hostname kernel: [81585.308993] usb 4-1: new full speed USB device using uhci_hcd and address 3
Dec  5 14:53:19 your-hostname kernel: [81585.456757] usb 4-1: not running at top speed; connect to a high speed hub
Dec  5 14:53:19 your-hostname kernel: [81585.484884] usb 4-1: configuration #1 chosen from 1 choice
Dec  5 14:53:19 your-hostname kernel: [81585.498817] scsi6 : SCSI emulation for USB Mass Storage devices
Dec  5 14:53:24 your-hostname kernel: [81590.514870] scsi 6:0:0:0: Direct-Access     USB 2.0  USB Flash Drive  0.00 PQ: 0 ANSI: 2
Dec  5 14:53:24 your-hostname kernel: [81590.519874] sd 6:0:0:0: [sdb] 15794175 512-byte hardware sectors (8087 MB)
Dec  5 14:53:24 your-hostname kernel: [81590.522834] sd 6:0:0:0: [sdb] Write Protect is off
Dec  5 14:53:24 your-hostname kernel: [81590.534817] sd 6:0:0:0: [sdb] 15794175 512-byte hardware sectors (8087 MB)
Dec  5 14:53:24 your-hostname kernel: [81590.537814] sd 6:0:0:0: [sdb] Write Protect is off
 
>>>>  Dec  5 14:53:25 your-hostname kernel: [81590.537888]  sdb: sdb1  <---- GOT YOU!
 
Dec  5 14:53:25 your-hostname kernel: [81590.654848] sd 6:0:0:0: [sdb] Attached SCSI removable disk

Note that the USB drive was “connected”, or associated with sdb device

[81590.654848] sd 6:0:0:0: [sdb] 15794175 512-byte hardware sectors (8087 MB)

and more precisely, with sdb1 device

[81590.537888]  sdb: sdb1

And that means we can talk to it! The full name of the guy would be “/dev/sdb1″.

Now let’s greet our friend. Say: “Hi /dev/sdb1″! :)

Step 2. Mount USB drive’s device to the File System.

Just an extra step, in case you need to mount it. If you can’t, and would like to format it, so you can mount it afterwards, read this.

To mount the drive enter this:

sudo mount -t vfat /dev/sdb1 /media/usbdrive/

where “/dev/sdb1″ is the name of the device, we found in the step above. “/media/usbdrive/” is the directory that we are going to mount it to. Make sure this directory exists (otherwise create it “sudo mkdir /media/usbdrive/”). And “-t vfat” is asking your Linux/Unix OS to mount this device as a “vfat” (FAT16, FAT32) device.

Many, if not most, USB devices are VFAT, however if you have an NTFS USB hard drive, for example, you can mount it by entering:

sudo mount -t ntfs-3g /dev/sdb1 /media/usbdrive/ -o force

“sudo” in above couple commands comes from mostly Ubuntu way to “run command as a super user”. If you have any other flavor of Linux/Unix, you may want to just run it as a “root” user.

Eat more apples, and good luck!

12 comments

  1. You also might looking into mounting the stick by UUID or label, which saves you the hassle of finding the device (all but the first time, anyway).

  2. @Matt,

    Although it seems to me to be a bit easier for non “sysadmin” :) people to check the syslog – thanks for the tip.

    – Toly

  3. Thanks for the update. This is helpful :)

  4. Useful tip! Ubuntu can do that automatically, but I love the complicated way… Thanks!

  5. @Esteban,

    Ubuntu does it “automatically” most of the time. However there are times (mostly hardware related) where Ubuntu can’t/don’t mount the USB drive.

    – Toly

  6. Great series of posts, this saved several flash drives I had inadvertently screwed up. Thanks much!

  7. You’re right Toly! …even when Ubuntu can sometimes do it “automatically” I prefer the manual way, that’s why I liked your tip… very useful tip indeed! Thanks so much! :)

  8. David J. Heinrich

    I have one that says sdb:sdb1 when I plug it in; but i have no /dev/sdb1 device:

    # mount -t vfat /dev/sdb1 /media/”USB Drive”
    mount: special device /dev/sdb1 does not exist

    Navigating to /dev and using MAKEDEV doesn’t help:

    # cd /dev
    # sudo ./MAKEDEV sdb1
    .udevdb or .udev presence implies active udev. Aborting MAKEDEV invocation.

    What do I do?

  9. @David,

    Can you inject your usb drive, run “dmesg”, and post 10-20 lines where it identifies your drive? This might help to resolve your problem.

    Thank you,
    – Toly

  10. I use the command
    fdisk -l (small L)
    Shows all storage devices.

  11. useful article. i have a fileserver with no desktop and this helped me identify a couple of dead usb connections. eventually found a live one at the rear of the server. much quicker than moving big files across the network. thanks

tell me something...
  1. (required)
  2. (valid email - optional)
  3. Captcha
  4. (required)