You are on page 1of 16

Find USB flash drive device in Linux

Recently I have written a quick “how to” on restoring, formatting a USB flash drive.
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."
"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
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 3Dec 5 14:53:19 your-hostname kernel: [81585.456757] usb 4-
1: not running at top speed; connect to a high speed hubDec 5 14:53:19 your-hostname
kernel: [81585.484884] usb 4-1: configuration #1 chosen from 1 choiceDec 5 14:53:19 your-
hostname kernel: [81585.498817] scsi6 : SCSI emulation for USB Mass Storage devicesDec
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: 2Dec 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 offDec 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
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)


[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


[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/


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 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!
1. Find what the type of USB drive is?
You will need to know what kind of drive is called to mount. To find this, just run fdisk
command as below.

sudo fdisk -l
It will print all the partition table attached to your Linux (Ubuntu) system.
For USB, you’re looking for a partition that should look something like: /dev/sdb1.
Remember what it’s called.
Note: On Ubuntu, Linux Mint or other Ubuntu-derived distributions, you just need to use
a prefixed ‘sudo’ to run any ‘fdisk’ command. Some Linux distributions don’t support
sudo prefix. In that case, use the ‘su’ – command first and then enter your root
password. After that, you will get a root shell. Now you can run any command without
any ‘sudo’ prefix.
With this step, you know the type of USB flash drive you have connected.
2. How to Create a Mount Point?
For ease of understanding, consider everything in Linux is structured as files and
directories. So even if you are connecting a USB drive, you have to mount it to your
system directory.
The best way is to create a new directory in /media using ‘mkdir’ command.

sudo mkdir /media/usb


You can create a directory with any name (preferably create a directory inside media).
We are using this newly created directory to mount our connected USB drive.
3. How to Mount USB drive in Linux?
Following is the command with that you can mount the drive onto the Linux file system:

sudo mount /dev/sdb1 /media/usb


‘/media/usb’ is a newly created directory in the second step.
Once you have mounted the USB drive, you can see all the mounted file system on
your Linux with the simple mount command.

mount
You can see the USB flash drive mounted there.
Now you can easily access the USB mounted files in ‘/media/usb’ directory.
To find the disk usage of your mounted file system, use du command. It is useful when
you want to know how much data is there on the connected USB drive and how much
space is free to use.
Now you can do whatever you want with data in your USB flash drive.
Never forget to unmount a USB drive after using it. Removing the USB device without
unmounting, may damage your data. So follow the fourth step.
4. umount USB drive in Linux:
Once you’re done with using data from mounted USB drive, you need to unmount it.

sudo umount /media/usb


Note: Kindly don’t take a risk to remove a USB device without unmounting, it can
corrupt your data in the USB drive.
5. Umount USB drive when it is busy:
Sometimes, you might have seen the message “Linux cannot umount as the target is
busy”.
This is mainly because some other applications might be using this device location. To
umount the device, you have to force the system to umount this device.
This can be achieved by providing ‘-l’ option.

sudo umount /media/usb -l


We have seen How to Mount USB Drive in Linux. But again, you may have GUI access
where you can access all connected devices. Then why do I need mounting USB drive
through terminal?
I am sharing my experience where these commands saved my data. Saving my
precious data can never be less than saving my life. It might be true for you.
So here it is…
Where do I find these commands useful?
Last week my Linux crashed, and I was not able to log in through GUI. So the only
option I had to log in through shell terminal. (To log in through shell terminal, keep
pressing ctrl + alt+ f1 while powering on.) With these commands, I took a backup in my
USB flash drive before resolving the issue with crashed Linux.
So the technique – how to mount USB drive in Linux, comes as a bulletproof for me to
save myself from data loss.
Note:
 All these commands are tested and working fine on a Ubuntu system and it will
work on any Linux-based system.
 If you are log in as system administration, you can skip using ‘sudo’ keyword in
command. Run all above command without ‘sudo’.
Here is a pro tip for you. This will make your learning easy with commands.
Using a USB flash drive with Linux

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


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.
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.
The device description for the partition on the USB drive is

/dev/sdxn
where x is the drive letter and n is the partition number, In your case it
seems to be /dev/sdb1. But you should not write directly to the device. Instead
you should mount it and write to the file system at the mountpoint. First you
should create a mountpoint, or use one that already exists. Text after # is a
comment (not used as a command).
sudo mkdir /mnt/sdn # only the first time
sudo mount /dev/sdxn /mnt/sdn
or in your case

sudo mkdir /mnt/sd1


sudo mount /dev/sdb1 /mnt/sd1
You may want to make sure that you are allowed to write to the USB
pendrive from a regular user by the following method,
sudo mkdir -p /mnt/sd1 # only if you want a new mountpoint
sudo umount /dev/sdxn # general: only if already mounted (with bad permissions).
sudo umount /dev/sdb1 # example

sudo mount -o rw,users,umask=000 /dev/sdxn /mnt/sd1 # general: mount


sudo mount -o rw,users,umask=000 /dev/sdb1 /mnt/sd1 # example

ls -ld /mnt/sd1 # check permissions

sudo bash -c "echo 'Hello World' > /mnt/sd1/hello.txt" # test writing with sudo
cat /mnt/sd1/hello.txt # test reading (as user)
ls -l /mnt/sd1 # check permissions of the content
rm /mnt/sd1/hello.txt # test removing (as user)
echo 'I am a user' > /mnt/sd1/user.txt # test writing (as user)
Edit 1: Sometimes (I would even say often) the partition on the USB drive
will be mounted automatically. You will find it with the following commands,
df -h
sudo lsblk -f
sudo lsblk -m
The automatic mounting may or may not make it read-write for the regular
user, but it will usually be possible to write with superuser privileges, with
sudo.
You can inspect how it is mounted with the command

mount
but it will display a lot of information (about everything that is mounted).

Edit 2: copying command


After finding out that the pendrive is automatically mounted on /data, the
following command line should work, if [the partition in] the USB drive is
mounted read/write and with permissions for your regular user ID.
cp -r /opt/biweb/app /data
It should create a directory /data/app on the USB drive with the content (the
directory tree and the files). If it does not work, you can try the special
mounting method, that I showed above, but modified for the current
mountpoint,
sudo umount /data # unmount
sudo mount -o rw,users,umask=000 /dev/sdb1 /data # mount with 'full' permissions
You may have to manually mount your usb drive
NOTES: Before we start, This is completely off the top of my head, I've not got a *nix
box in front of me to test any of this atm, but it should work. Apologies if there are any
errors though! Also you should be aware that I tend to use debian based distros, so I've
included use of sudo to get root access in the following guide.

To manually mount, you'll need to do the following:

1. After plugging a USB drive into your machine, you need to use the lsusb command to
see what devices are connected via USB and search the output for anything that refers
to your USB stick.

If you can see something which refers to your USB stick (manufacturer, capacity etc.),
you'll know for certain that your system has recognised the device and you can continue
on to mount it.
But if you don't see anything, there's no point attempting to mount the device, as the OS
has not detected/recognised it!

2. Assuming you found your device, next you'll need to get a list of connected drives
using:

1. dmesg | grep -i "SCSI device"

Again take a look in the output for something that refers to your USB stick. So if your
stick is 1GB and you see a drive listed that has 1GB capacity, then it's most likely to be
your device (provided there aren't more than one 1Gb drives plugged in!)
Once you've identified your device in the list (it could be /dev/sda, /dev/sdb,
/dev/sdb1, /dev/sdc etc.), you need to make a note of it.

For this example, lets assume that I've identified /dev/sdb1 as my USB drive.

3. Next we'll need to create a directory to act as a mount point.


Where you choose to put this directory is up to you. You could put it in a directory in
/media/, which is where some distros auto mount drives to. Or you could create a folder
in your home directory, or wherever.
Going with /home/ for my example:

1. mkdir /home/jason/Desktop/myflash

This creates a folder on my desktop called 'myflash', which I'll use as a mount-point for
my USB drive.

4. OK, So now we've identified our USB device and created a mount point, we can
finally mount it. To do this, we'll need use the mount command. The syntax for which will
look something like this:
"mount -t TYPE -o uid=YOUR_USERNAME,gid=users /dev/YOUR_DRIVE
MOUNT_POINT"

Where TYPE is the filesystem in use on the drive. For a typical USB drive I'd assume
'vfat' would most likely be used. But if your USB stick has been formatted in ext3, ext4
or some other format, then it should be specified here. Anyway, for the purposes of this
example, I'll use 'vfat'!

Substitute YOUR_USERNAME with your username, in my case it would be 'jason'.


Substitute YOUR_DRIVE with the drive you identified as yours (for this example, I'll use
'sdb1')
Finally substitute MOUNT_POINT with the directory you created to mount the drive (in
the case of this example, '/home/jason/Desktop/myflash')

So putting it all together; for my USB drive, I'd need to use:

1. mount -t vfat -o uid=jason,gid=users /dev/sdb1 /home/jason/Desktop/myflash

(But obviously, you need to use the information for your drive and your preferred mount-
point etc!)

All being well, my drive should now be mounted at /home/jason/Desktop/myflash.


If you use cd to navigate to your mount-point, you should now be able to list/view the
contents of the drive using ls.
e.g. for my drive:
1. cd /home/jason/Desktop/myflash && ls -a -l

To copy a file to the drive, you can simply use cp to copy to the mount point:
e.g.

1. cp ~/somefile.txt /home/jason/Desktop/myflash/

ALTERNATIVELY:
The previous example is OK if you only use the USB drive occasionally, but if you plan
to use the USB stick often; you could make an entry in etc/fstab, which will prepare your
computer to mount the USB stick every time it is booted. This will simplify the mounting
process for you somewhat.

BTW: In case you are worried the system might try to mount the drive even when it is
not plugged in, you should know that this doesn't try to mount the drive at all! It simply
makes it quicker and easier to mount for when the device IS plugged in!

Anyway, to do this, you need to gather some information about your drive (as per the
previous example) by using lsusb, dmesg and grep (see the previous example for the
exact commands!)

Next, create a back-up of the current version of /etc/fstab (in case of emergency!):

1. sudo cp /etc/fstab /etc/fstab.bak

You then need to open etc/fstab for edit like this:

1. sudo vim /etc/fstab

(In my case I've used vim, you could substitute that for whatever command-line text
editor you prefer!)

With the file open; simply add the following line, save the file and quit the editor:
1. /dev/sdb1 /home/jason/Desktop/myflash vfat users,noauto,uid=jason,gid=users 0
0

Obviously, I've used my example data there, but again, you need to substitute the
information you gathered about your USB drive and your mount point etc.

Once that's done, whenever you plug in your USB stick you can simply use:

1. mount MOUNT_POINT

And the code added to /etc/fstab will allow the USB stick to be mounted at your
specified mount-point.

So in my example. Where I'd used /home/jason/Desktop/myflash as a mount point, I


could plug my drive in and use:

1. mount myflash

And thanks to the entry in /etc/fstab, my USB drive would be mounted to


/home/jason/Desktop/myflash

Hope this is of some help!

Mount -o remount,rw /dev/sdb1

You might also like