Map Linux sg Devices to Disks, Optical Drives and Tapes
A drive fails inside a multi-bay enclosure, and sg_map is how you find the matching /dev/sgN node behind /dev/sdd in seconds. It is entirely read-only: no writes, no config changes, just a lookup between generic SCSI nodes such as /dev/sg0 and higher-level names such as /dev/sda or /dev/sr0. You will also learn when the result is incomplete, and why a newer discovery tool is usually the better choice today.
The route
Jump straight to the step you need, or tick off Done means at the end.
- Time: about fifteen minutes.
- Needs: a shell and the
sg3-utilspackage. The examples below usesg_map1.12 (20171010) from package version1.46-3ubuntu4. - Touches nothing: the command only inspects device nodes, though opening SCSI devices may need elevated privileges on your host. It never alters disks or persistent configuration.
1. Confirm the installed command
Check which executable will run, then ask it for its version. Both are ordinary read-only commands:
$ command -v sg_map
/usr/bin/sg_map
$ sg_map -V
Version string: 1.12 20171010
- Note the uppercase
-V. This is not the GNU-style--version: on this installation,--versionis rejected as an unknown switch, an easy scripting trap.
Checkpoint
If command -v finds nothing, install sg3-utils through your normal package-management process before continuing. Do not copy a binary from another host just to obtain this old utility.
2. Run the default numeric mapping
With no selection option, sg_map scans numeric generic nodes and reports mappings for disks, optical drives and tapes:
$ sudo sg_map
/dev/sg0 /dev/sda
/dev/sg1 /dev/sdb
- A lone
/dev/sgNline means the generic device was found but no matching disk, optical or tape name was identified. A scanner is one example of a SCSI device with no alternate SCSI node. - Permission denied means try sudo. Privileges are host-specific, and an unprivileged run can fail like this:
$ sg_map
Error opening /dev/sg0 : Permission denied
Stopping because no sg devices found
On the installed command, even this no-device result returned status 0 during the check. Do not use a zero status alone as proof that every mapping was read successfully.
3. Limit the result to one device family
Use one of the -s options when you want a particular kind of node. For disks:
$ sudo sg_map -sd
/dev/sg0 /dev/sda
/dev/sg1 /dev/sdb
-srfor optical nodes named/dev/srN.-scdfor older/dev/scdNnames.-stfor tape nodes.
These options filter the names that are displayed; they do not rescan or reclassify a device. For a quick check of everything the command accepts:
$ sg_map -h
Usage: sg_map [-a] [-h] [-i] [-n] [-sd] [-scd or -sr] [-st] [-V] [-x]
Checkpoint
Compare the device family you requested with the path printed. A disk query should not be treated as evidence about an optical or tape mapping.
4. Add SCSI address and type details
Use -x when a path alone is not enough to identify the physical target. It adds host, bus, SCSI ID, LUN and SCSI type fields after each active generic node:
$ sudo sg_map -x
/dev/sg0 1 0 0 0 0 /dev/sda
/dev/sg1 2 0 0 0 0 /dev/sdb
The five numeric fields are, in order, host number, bus, SCSI ID, LUN and SCSI type. In this example both entries have type 0, the disk type. Treat these numbers as identifiers for investigation, not as a stable naming scheme: they can change after hardware changes, adapter removal or a reboot.
Need the vendor, product and revision strings too? Add -i:
$ sudo sg_map -i -x
/dev/sg0 1 0 0 0 0 /dev/sda ATA DEVICE-NAME REVISION
The inquiry text is hardware-dependent, so do not write a parser that assumes one fixed spacing or product length. Check the actual output on the host you are documenting.
5. Avoid the obsolete alphabetical scan
The default is the numeric scan, equivalent to -n, looking for names such as /dev/sg0. The -a option instead assumes alphabetical names such as /dev/sga and /dev/sgb:
$ sudo sg_map -n
$ sudo sg_map -a
Linux deprecated alphabetical sg node names back in the 2.2 kernel series. Use -a only for a deliberately old environment that actually has those nodes. It is not a compatibility switch for modern /dev/sg0 devices.
6. Treat the result as a diagnostic snapshot
sg_map is deprecated, and its own manual page admits it has not been actively updated for years. On modern Linux, the project recommends lsscsi or udev facilities instead.
- It can mislead. The older utility scans conventional names and gets confused when devices have been added or removed, when naming is sparse, or when udev assigns names differently from old major and minor number assumptions.
- Use it for a snapshot, not a record. If a mapping matters to an automation job, pull a persistent identifier from the appropriate udev or SCSI tooling rather than storing
/dev/sg0as though it were permanent.
There is nothing to undo after these examples: the command reads device information and exits. If you used sudo, it granted access only to that invocation. It did not change ownership, permissions, udev rules or device configuration.
Done means
- Confirmed the version of the installed
sg_mapwith-V. - Ran the numeric scan and checked whether permission errors hid mappings.
- Filtered by family with
-sd,-sr,-scdor-stonly when you needed one. - Added detail with
-xor-iwhen path names alone were ambiguous. - Treated the result as current, not as a permanent device identity.