November 25, 2013

How to Mount an USB Device in Linux

Short Version

1. Before inserting the USB, check which disks you already have.

$ ll /dev/sd*
brw-rw----. 1 root disk 8, 0 Nov 24 19:41 /dev/sda
brw-rw----. 1 root disk 8, 1 Nov 24 19:41 /dev/sda1
brw-rw----. 1 root disk 8, 2 Nov 24 19:41 /dev/sda2

2. Create a new directory under /mnt to which you will mount your USB.

$ mkdir /mnt/usb

3. Now insert the USB and mount it.

$ mount /dev/sdb <hit tab>
sdb sdb1

$ mount /dev/sdb1 /mnt/usb

4. Now you are ready to read and write to your USB.

5. To unmount.

$ umount /mnt/usb

Longer Version

In Linux a storage device is represented by a device file in /dev/.

The three letter naming convention for storage devices in Linux are:

  1. s - storage
  2. d - disc (such as SCSI, USB, SATA), cd - cd or dvd
  3. litteral order character, starting with a, then b, c, etc

Example: /dev/sda (SCSI, USB, SATA), /dev/sdb (SCSI, USB, SATA), /dev/scd (CD/DVD)

These device files represent the whole drive. Each drive is partitioned into partition. The first partition receives order number one, the next one two, etc

When a new storage device is added it will receive the last character order literal, here it is b (/dev/sdb). Another way to find out the device file is to tail the dmesg log file.

$ less /var/log/dmesg <hit enter>

...
<press shift+f (follow)>
Waiting for data... (interrupt to abort)

<Now insert USB>

sd ... [sdb] Assuming drive cache: write through
<press ctrl+c (quite)>

Here we see that the USB was allocated device name sdb. But when you mount you mount to a partition that contains a file system. And in general, most USB only have one partition, hence sdb1.

No comments: