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

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.

Done means