Professional Documents
Culture Documents
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
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.
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.
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
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:
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:
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 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).
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:
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.
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'!
(But obviously, you need to use the information for your drive and your preferred mount-
point etc!)
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!):
(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.
1. mount myflash