
What USB device is that?
When you use Linux with a graphical desktop like GNOME or KDE, the desktop manages a lot of “behind the scenes” stuff for you. One thing that I don’t really think about is “What USB device is that?” when I plug in a USB flash drive. My desktop just gives me an icon that I can use to open the new drive in my file manager. It’s done.
But what if you’re using Linux without a graphical desktop? For example, I recently started using my Raspberry Pi entirely from the command line. It’s a lot of fun to go back to “console” mode, but I miss having the desktop manage my USB drives for me.
Identify the device
In one recent example, I wanted to copy a bunch of files from the Raspberry Pi so I could use them on my desktop Linux system. I have a 16GB USB flash drive that makes it handy to transfer files.
Before I could mount the filesystem on the USB drive, I needed to know how to “find” the device. That’s an easy two-step process:
First, I ran a command to store the kernel’s messages to a file. The dmesg command will display all messages from the kernel’s buffer, so run that command as root and store the results in a file. I called my file d1
:
# dmesg > d1
Then, I plugged in the USB drive. Give it a second, and the kernel will recognize that a new USB device has been connected to the system. This information will be stored in the kernel’s buffer. Run dmesg a second time and save the output to a new file, such as d2
:
# dmesg > d2
Now, use the diff command to print the differences between those two files. In this case, the only changes between d1
and d2
should be the messages about the new USB device:
# diff d1 d2
610a611,629
> [ 150.388971] usb 1-1.3: new high-speed USB device number 6 using dwc2
> [ 150.576172] usb 1-1.3: New USB device found, idVendor=03f0, idProduct=5307, bcdDevice=11.00
> [ 150.584344] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=3
> [ 150.591712] usb 1-1.3: Product: v165w
> [ 150.595341] usb 1-1.3: Manufacturer: HP
> [ 150.599207] usb 1-1.3: SerialNumber: AA00000000002544
> [ 150.606131] usb-storage 1-1.3:1.0: USB Mass Storage device detected
> [ 150.615222] scsi host0: usb-storage 1-1.3:1.0
> [ 151.997413] scsi 0:0:0:0: Direct-Access hp v165w 1100 PQ: 0 ANSI: 0 CCS
> [ 152.006234] scsi 0:0:0:0: alua: supports implicit and explicit TPGS
> [ 152.012283] scsi 0:0:0:0: alua: No target port descriptors found
> [ 152.019941] sd 0:0:0:0: Attached scsi generic sg0 type 0
> [ 152.021393] sd 0:0:0:0: [sda] 31690752 512-byte logical blocks: (16.2 GB/15.1 GiB)
> [ 152.033700] sd 0:0:0:0: [sda] Write Protect is off
> [ 152.038348] sd 0:0:0:0: [sda] Mode Sense: 43 00 00 00
> [ 152.039676] sd 0:0:0:0: [sda] No Caching mode page found
> [ 152.044785] sd 0:0:0:0: [sda] Assuming drive cache: write through
> [ 152.072051] sda: sda1
> [ 152.074753] sd 0:0:0:0: [sda] Attached SCSI removable disk
This tells me that the kernel recognized my HP v165W USB flash drive as sda
, which I can access as /dev/sda
.
Identify the partition
Now that I know the device name, I need to know what partition has the data. Actually, that’s easy for me, since I created the partition on this drive, and I created only one partition. But to verify, I can use the fdisk command with the -l
option to list the partition information on the /dev/sda
storage device:
# fdisk -l /dev/sda
Disk /dev/sda: 15.11 GiB, 16225665024 bytes, 31690752 sectors
Disk model: v165w
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x114afb7a
Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 31690751 31688704 15.1G 83 Linux
With that information, I can mount the USB flash drive so I can copy data to it. I used the /mnt
directory, which is otherwise empty and is meant as a generic mount point for short-term use cases like mine:
# mount /dev/sda1 /mnt
# ls /mnt
'copy from home_jhall' 'copy from laptop' home lost+found 'some music (copy)'
Living at the command line
Using the command line to identify and access storage devices may seem primitive if you’re used to a graphical desktop that does the work for you. But knowing how to identify devices like this can also be helpful as a systems administrator. Linux servers do not usually have a graphical desktop, so you need to know how to be effective at the command line. Little “tricks” like this can be a handy “tool” to add to your “sysadmin toolkit” to navigate the system.