Home / Alt manpages / sg_rmsn(8)

  • sg_rmsn(8)
  • Admin command
  • linux

Read a SCSI Media Serial Number Safely with sg_rmsn

sg_rmsn requests a SCSI READ MEDIA SERIAL NUMBER response from a chosen device and saves a readable result without touching the device contents. Allow about ten minutes, plus time to identify the correct SCSI device. You need the sg3-utils package, a device that accepts this SCSI command, and permission to open its device node.

This guide describes the installed sg3-utils package version 1.46-3ubuntu4. The local program reports utility version 1.18 from 28 June 2018. The command is a narrow diagnostic tool, not a general-purpose way to identify every disk or optical disc you own.

1. Check the installed command

Start with read-only checks. They do not open a storage device or send it a SCSI command, so they do not need elevated privileges:

$ command -v sg_rmsn
/usr/bin/sg_rmsn
$ dpkg-query -W -f='${Package} ${Version}\n' sg3-utils
sg3-utils 1.46-3ubuntu4
$ sg_rmsn --version
version: 1.18 20180628

Checkpoint

Confirm the package and binary are the ones you intend to use. The long options documented by this installation are --help, --raw, --readonly, --verbose and --version. Each has a short form, but the long forms make a script easier to review later.

2. Identify the device before opening it

sg_rmsn takes exactly one positional argument, DEVICE. Use the device node for the SCSI logical unit you mean to query, such as /dev/sg3 or a suitable block device exposed by your system. Do not guess from a changing /dev/sdX name, and do not substitute a mounted device merely because it is convenient.

List the devices and their model information with tools already on your host, then check the selected path:

$ ls -l /dev/sg3
$ readlink -f /dev/sg3
/dev/sg3

The path in this example is a placeholder. Replace it with a real device you have positively identified. Reading the node's metadata is an ordinary command. Running sg_rmsn against it sends a SCSI command, and should be treated as an elevated hardware operation even though the command itself is intended to read information.

3. Request the serial number in the default format

Run the command with the real device path:

$ sg_rmsn /dev/sg3

The exact bytes and text will come from the device. The default output is ASCII hexadecimal with an ASCII rendering to the right: it is the safer format for a terminal transcript, because padding bytes stay visible rather than silently turning into control characters. A device that does not support the command reports a failure instead, as described below.

If access is denied, repeat the same command with sudo only when your local device permissions require it:

$ sudo sg_rmsn /dev/sg3

Do not make a broad permission change to /dev/sg* just to get one result. Fix the account's group membership or the host's device policy through your normal system administration process instead. A successful exit status means the utility completed successfully; it does not mean the returned identifier is globally unique or suitable as an asset-management identity.

4. Use raw output only when a program needs the bytes

--raw sends the serial number to standard output. The response can contain non-printable characters, including null padding added to make its length a multiple of four. Never send raw output straight to a terminal, and never capture it in a text variable:

$ sg_rmsn --raw /dev/sg3 > media-serial.bin
$ wc -c media-serial.bin
$ od -An -tx1 -c media-serial.bin

The byte count and bytes will be device-specific. Treat the file as binary, and let the program consuming it strip padding only according to the SCSI specification and your device's own contract. If you only need to read the answer yourself, omit --raw entirely.

5. Understand the read-only switch

The command opens DEVICE read-write by default. --readonly requests a read-only open instead:

$ sg_rmsn --readonly /dev/sg3

This option is not a promise that the SCSI operation works through a read-only file descriptor everywhere. The installed manual says the Linux SCSI generic driver needs read-write access for this command, while other access methods may only need read-only access. On Linux, expect --readonly to fail on paths handled by that driver, even though the command itself is a read request. Remove the option and use the normal default only after confirming the device is the intended target and that your change control permits the open mode.

There is no persistent state to undo in these examples. The command does not write a serial number, and it does not alter a service configuration. Close the output file and stop using the device path if you picked the wrong unit; do not retry blindly against another disk.

6. Handle unsupported media and confusing results

READ MEDIA SERIAL NUMBER is not a mandatory SCSI command. The local manual records that its author had not seen a SCSI device supporting it. An unsupported command, a transport error, or a device-specific response is therefore a normal possibility, not proof that sg_rmsn is broken.

Turn up diagnostics only when you actually need them:

$ sg_rmsn --verbose /dev/sg3

Keep diagnostic output separate from a machine-readable raw result. Error messages go to standard error, while --raw writes the serial bytes to standard output, so a shell redirection can preserve that separation:

$ sg_rmsn --raw /dev/sg3 > media-serial.bin 2> media-serial.err
$ test "$?" -eq 0 && echo "request succeeded" || cat media-serial.err

If ordinary device identification is really what you want, rather than this particular media command, check the alternatives the manual names. sg_vpd can read the device identification page (VPD page 0x83) or unit serial number page (VPD page 0x80). For MMC optical drives, sg_get_config can inspect the media serial number feature. Those identifiers describe different SCSI facilities, so do not silently substitute one for another in an inventory system.

Done means

  • Version and device confirmed. You verified the installed sg_rmsn version and selected the intended device node.
  • Output format chosen. You ran the command with default ASCII-hex output, or deliberately captured --raw output as a binary file.
  • Privilege used only when needed. You used elevated privileges only when the device permissions required them.
  • Unsupported command handled calmly. You treated an unsupported-command response as an expected hardware compatibility boundary.
  • Identifiers kept distinct. You kept alternative identifiers from sg_vpd and sg_get_config separate from the media serial number.