Map Linux sg Devices to Disks, Optical Drives and Tapes
You will finish with a read-only way to relate generic SCSI-generic nodes such as /dev/sg0 to higher-level Linux device names such as /dev/sda and /dev/sr0. You will also know when the result is incomplete and why a newer discovery tool is usually a better choice.
The route
Jump straight to the step you need, or tick off Done means at the end.
Allow about fifteen minutes. You need a shell and the sg3-utils package. The examples below use sg_map 1.12 (20171010) from package version 1.46-3ubuntu4. The command only inspects device nodes, but opening SCSI devices may require elevated privileges on your host. It does not alter disks or persistent configuration.
1. Confirm the installed command
Check which executable will run, then ask it for its version. These are ordinary read-only commands:
$ command -v sg_map
/usr/bin/sg_map
$ sg_map -V
Version string: 1.12 20171010
The option is an uppercase -V, not the GNU-style --version. On this installation, --version is rejected as an unknown switch. That small difference is 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
Your output will reflect the hardware and device names on the current machine. A line containing only /dev/sgN means that the generic device was found but no corresponding disk, optical or tape name was identified. A scanner is one example of a SCSI device that can have no alternate SCSI node.
Privileges are host-specific. If the unprivileged command reports messages such as Permission denied, rerun it with sudo if your account is authorised:
$ 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 are looking for a particular kind of node. For disks:
$ sudo sg_map -sd
/dev/sg0 /dev/sda
/dev/sg1 /dev/sdb
Use -sr for optical nodes named /dev/srN, -scd for older /dev/scdN names, and -st for tape nodes. These options filter the names that are displayed; they do not rescan or reclassify a device.
For a quick check of the command's accepted options, use:
$ 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 is not enough to identify the physical target. The command 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 the example, both entries have type 0, which is the disk type. Treat the numbers as identifiers for investigation, not as a stable naming scheme. Device numbering can change after hardware changes, adapter removal or reboot.
If you need the device's vendor, product and revision strings as well, 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, which looks 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 has deprecated alphabetical sg node names since the 2.2 kernel series. Use -a only when you are dealing with 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 manual page says it has not been actively updated for years. On modern Linux, the project recommends using lsscsi or udev facilities instead. The older utility scans conventional names and can be misleading 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 the output to answer a focused question, such as which current /dev/sgN node corresponds to a disk. If a mapping matters to an automation job, record a persistent identifier from the appropriate udev or SCSI tooling rather than storing /dev/sg0 as if 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
- You confirmed the installed
sg_mapversion with-V. - You ran the numeric scan and checked whether permission errors hid mappings.
- You used
-sd,-sr,-scdor-stonly for the device family you needed. - You used
-xor-iwhen path names alone were ambiguous. - You treated the result as a current diagnostic, not as a persistent device identity.