If I connect a physical device, how can I ever know which device file belongs to it?
Solution 1
Using udev:
You can get useful information querying udev
(on systems that use it - almost all desktop-type Linuxes for sure). For instance, if you want to know which attached drive is associated with /dev/sdb
, you can use:
udevadm info --query=property --name=sdb
It will show you a list of properties of that device, including the serial (ID_SERIAL_SHORT). Having that information, you can look at the output of lsusb -v
and find out things like the manufacturer and product name.
A shorter path to do this would be
udevadm info --query=property --name=sdb | grep "\(MODEL_ID\|VENDOR_ID\)"
and see the line with matching $ID_VENDOR_ID:$ID_MODEL_ID
in the much shorter output of lsusb
.
Another useful option is udevadm monitor
. Use it if you'd like know which device node is created at the point of attaching the device. So first run
udevadm monitor --udev --subsystem-match=block
And then connect the device. You'll see the device names of the detected block devices (disks/partitions) printed at the end of each output line.
A practical example shell function:
Here's a function you can put in your .bashrc
(or .zshrc
) :
listusbdisks () { [[ "x$1" == "x-v" ]] && shift && local VERBOSE=-v for dsk in ${@-/dev/sd?} do /sbin/udevadm info --query=path --name="$dsk" | grep --colour=auto -q usb || continue echo "===== device $dsk is:" ( eval $(/sbin/udevadm info --query=property --name="$dsk" | grep "\(MODEL\|VENDOR\)_ID") [ "$ID_VENDOR_ID:$ID_MODEL_ID" == ":" ] && echo "Unknown" || \ lsusb $VERBOSE -d "$ID_VENDOR_ID:$ID_MODEL_ID" ) grep -q "$dsk" /proc/mounts && echo "----- DEVICE IS MOUNTED ----" echo done }
Use it like this :
listusbdisks
- to recognize all/dev/sdx
devices;listusbdisks sdb
orlistusbdisks /dev/sdb
orlistusbdisks sdb sdc
- to get info about certain devices only;listusbdisks -v [optional devices as above]
- to show verbose outputs oflsusb
[Edit]: Added some functionality like querying many devices, checking mounts and control verbosity of lsusb
.
Solution 2
There is no universal answer. On Linux the simplest way is probably to just fire up the gnome disk utility and it will show all detected disks, whether they are mounted or not. From the command line, you can consult the output of blkid
or udevadm info --export-db
.
Solution 3
In Mac OS X you can try to use diskutil list
.
Solution 4
With respect to all Unixes, I don't think this is feasible.
The usual external buses today do not use a fixed numbering scheme for their ports, like IDE did. So, the only remaining data sources for the mapping you need are the drives' manufacturer names, their serial numbers or the partitions' UIDs.
Manufacturer names are not necessarily unique on a given machine (suppose you connect two external drives from the same series - they will identify identically). Serial numbers are not "visible from the outside", as are partition UIDs.
So, about the only universal solution I can think of is to label the physical drives with the serial number and use the udevadm command given in rozcietrzewiacz's answer, if UDEV is available.
Admin
Updated on September 18, 2022Comments
-
Admin over 1 year
Say I plug in several USB drives which don't get automatically mounted. How can I find out which device file belongs to which physical device, so I can mount it for example?
I'm running Mac OS X but I rather like an answer that works on all (or at least the most popular) Unix systems. I had this problem with Linux in the past.
-
Admin over 12 yearsOn Linux, the answer would tend to involve udev. So you try looking around udev equivalents on OSX; this leads in particular to Alternative to udev functionality on OSX and OSX USB Device Interface Guide.
-
Admin about 9 yearsOn MacOSX the simplest is to identify the volumes by their labels. But Gilles has right about the perfect solution.
-
-
tcoolspy almost 13 yearsTrue enough as far as it goes, but not all systems use
udev
. -
rozcietrzewiacz almost 13 years@Caleb True... Actually,
lsusb
is also an optional program. But both are extremely common. -
Gilles 'SO- stop being evil' almost 13 years@rozcietrzewiacz The question is primarily about OSX, but udev is intrinsically specific to Linux.
-
rozcietrzewiacz almost 13 years@Gil Well, true. I've no experience with OSX, so I just updated my answer not to be misleading.
-
Abhishek A over 12 yearsWhat I generally do in such case is, I plug the device in and immediately run
dmesg
. But this won't be helpful if device is already plugged in and if there are more devices plugged after the first one. :) -
rozcietrzewiacz over 12 years@green Yep,
dmesg
is the most common way (usually with| tail
at the end). Another similar istail -f /var/log/messages
or withgrep
or just switching totty12
. But udev tools are definitely more useful for scripts. -
slm about 10 years@rozcietrzewiacz - you should probably either refine this answer so that it's OSX specific given the Q is about that or delete it. You could always post an actual Q to go w/ this A and move it over to it. It's a good A, just not for OSX as far as I know.