Home / Alt manpages / hostid(1)

  • hostid(1)
  • User command
  • linux

Use hostid to Record and Compare a Linux Host Identifier

Need a quick, host-specific tag for a script without pulling in the hostname or an IP address? hostid prints one number and nothing else. This guide finishes with a repeatable check that prints the machine's host identifier in hexadecimal, captures it cleanly for a script, and compares later output without changing the system. The examples use GNU coreutils 9.4, installed here as package version 9.4-3ubuntu6.3.

Allow about ten minutes. You need a shell and the hostid command. This is an ordinary, read-only workflow: it does not need sudo, edit a file, restart a service or touch a network setting.

1. Check the installed command

Start by confirming which executable your shell will run and checking its version:

$ command -v hostid
/usr/bin/hostid
$ hostid --version
hostid (GNU coreutils) 9.4

Your path may differ on a non-standard installation. The version matters when you are documenting a result for an incident or a reproducible test. The local manual describes this command as printing the numeric identifier for the current host, in hexadecimal.

Checkpoint

The command is installed and you know which implementation is in use.

2. Print the identifier

Run hostid with no option:

$ hostid
f38817ab

The value is host-specific, so your output will normally differ. The command writes the identifier followed by a newline, convenient to read in a terminal. It does not print the hostname, an IP address or any human-readable description.

Warning

Do not treat this hexadecimal text as a hostname or as a secret. It is an identifier for the current host, not an authentication factor. If a script uses it to label local data, keep the label's purpose clear and never use it as proof that a remote client is trusted.

3. Capture output for a script

Command substitution strips the trailing newline, usually what you want when building a filename, log field or comparison:

$ current_hostid=$(hostid)
$ printf 'hostid=%s\n' "$current_hostid"
hostid=f38817ab

Quote the variable when you reuse it. The normal output is a compact hexadecimal value, but quoting still protects you if a later implementation or surrounding shell code turns an unexpected value into multiple arguments.

Check that the command actually succeeded before relying on the value in a larger script:

$ if current_hostid=$(hostid); then
>     printf 'current hostid: %s\n' "$current_hostid"
> else
>     printf 'hostid failed\n' >&2
>     exit 1
> fi
current hostid: f38817ab

Checkpoint

The value sits in a shell variable, and the script stops instead of quietly recording a blank result if the command fails.

4. Compare two observations

To compare the current value against one supplied by an operator or an earlier record, use a shell variable rather than parsing terminal formatting:

$ expected_hostid='f38817ab'
$ actual_hostid=$(hostid) || exit 1
$ if [ "$actual_hostid" = "$expected_hostid" ]; then
>     echo 'hostid matches'
> else
>     echo 'hostid differs'
> fi
hostid matches
  • Replace the placeholder. Swap f38817ab for the value you deliberately recorded for the machine you are checking.
  • The comparison is exact and case-sensitive. Hex letters are commonly lower case from this command; only normalise both sides if your surrounding data format demands it.
  • Do not trim blindly. Silently trimming arbitrary command output can hide a real error.

A mismatch is a prompt to investigate, not automatic evidence a machine has been replaced. Check that you are on the intended host, that the stored value belongs to the same environment, and that the command implementation is comparable: container, virtual machine and host boundaries can make this label less useful than it looks.

5. Use the two supported options

The interface is deliberately small. --help displays usage information and exits:

$ hostid --help
Usage: hostid [OPTION]
Print the numeric identifier (in hexadecimal) for the current host.

      --help        display this help and exit
      --version     output version information and exit

--version prints version information and exits. There is no documented option for selecting a different host, changing the output format or writing the result to a file, so redirect it yourself when needed:

$ hostid > /tmp/current-hostid.txt
$ test -s /tmp/current-hostid.txt && echo 'identifier captured'
identifier captured

That example writes under /tmp, but redirection can overwrite an existing file. If the destination matters, choose a new name or protect the old record before replacing it. There is no system change to undo in hostid itself; remove a disposable capture only once you are sure it is no longer needed.

Common traps

An extra operand is an error. This command does not accept a hostname as an argument:

$ hostid example-host
hostid: extra operand 'example-host'
Try 'hostid --help' for more information.
  • Do not copy the quote marks. The installed diagnostic uses typographic quotation marks in this terminal output; the reliable checks are the non-zero exit status and the suggestion to read the help.
  • Missing from your PATH? Check the package installation through your normal distribution tools, or use the absolute path if you have already identified one. Do not add a guessed path to a service configuration just to make a one-off check pass.
  • Two readings differ? Preserve both values with timestamps and host context before drawing conclusions. hostid reports one numeric identifier; it does not explain why an environment produced it and does not validate a machine's broader identity.

Done means

  • Version recorded. hostid is installed and its GNU coreutils version is noted where reproducibility matters.
  • Output understood. You can print the current host identifier and recognise it as hexadecimal, host-specific output.
  • Script-safe capture. A script captures the value with command substitution and checks the command status.
  • Comparisons explicit. Comparisons use a stated expected value and never treat a match as authentication.
  • No system change. You know --help and --version are the only documented options, and nothing persistent was changed.