
uuidgen Command in Linux
In modern computing, the ability to generate a unique identifier is essential. Universally Unique Identifiers (UUIDs) are one such means of achieving uniqueness across different systems, networks, and sessions. Defined by standards such as RFC 4122, a UUID is a 128-bit number represented in a canonical text format (for example, 8-4-4-4-12 hex digits separated by hyphens).
Table of Contents
Here is a comprehensive guide to the options available with the uuidgen command −
How to Use uuidgen Command in Linux?
The Linux uuidgen command is a widely available utility whose sole purpose is to generate these UUIDs. It leverages the underlying libuuid library supplied with many Unix-like operating systems and produces identifiers that, in practice, are unique regardless of which machine or time they are generated.
At its simplest, invoking the command without any options −
uuidgen

It generates a new UUID using default settings, which if a high-quality random number generator is available is typically a random-based UUID (version 4). This produces a string formatted in the standard 8-4-4-4-12 representation. For example, running the command might yield −
uuidgen Command Options
The power of uuidgen comes in its ability to generate several types of UUIDs using various options. Below, we examine each option in detail.
Generating a Random-Based UUID: --random or -r
Usage −
uuidgen -random

This option forces the generation of a random-based UUID (i.e., UUID version 4). Random UUIDs rely solely on randomness or pseudo-random number generators to fill most bits. They are ideal for cases where there is no need to encode information such as time or node data. They are most widely used for session IDs, temporary data tags, or unique keys in databases.
The hyphenated structure is maintained, and the "4" in the third group indicates that it is a version 4 UUID.
Generating a Time-Based UUID: --time or -t
Usage −
uuidgen --time

Notice that certain bits in this identifier reflect the version (usually the "1" in version 1) and the time bits that encode the timestamp.
Generating Name-Based UUIDs (Hash-Based) with MD5 or SHA1
Sometimes, you might want to generate a UUID based on some unique name within a given namespace. This approach is deterministic; running the command again with the same inputs will yield the same UUID. Two methods allow this −
Using MD5: --md5 Option
For MD5-based name-driven UUIDs (version 3), the options look like this −
uuidgen --md5 --namespace @dns --name example.com

- --md5 tells the tool to use the MD5 hash algorithm.
- --namespace @dns specifies that you are using the DNS namespace. Other well-known namespaces include @url, @oid, or @x500.
- --name example.com sets the string (or name value) that, when combined with the namespace, produces the UUID.
Using SHA1: --sha1 Option
Alternatively, you can use the SHA1 algorithm (resulting in a version 5 UUID) with a command like −
uuidgen --sha1 --namespace @dns --name example.com

- --sha1 selects SHA1 as the hash function.
- The rest of the parameters are the same as in the MD5 case.
Additional Options
Other options provide support for help and version information −
Help −
uuidgen --help

This prints a brief synopsis of the available options and shows usage information.
Version Information −
uuidgen --version

It tells you the current version of the uuidgen utility and may include copyright details.
Interpreting Hexadecimal Names
When generating a name-based UUID, you might want to interpret the name as a hexadecimal string rather than plain text. To do so, you can use −
uuidgen --sha1 --namespace @dns --name 6578616d706c652e636f6d --hex

Here, if the provided name "6578616d706c652e636f6d" is intended to be understood as a hexadecimal representation (which actually spells "example.com"), the --hex option assists in proper interpretation.
Examples of uuidgen Command in Linux
Let's walk through several practical examples to illustrate how uuidgen might be used in different scenarios.
Generating a Random (Version 4) UUID
For most general-purpose uses, simply running −
uuidgen

Or,
uuidgen --random

This type of UUID is widely used for non-predictable identifiers in web applications or for session tokens.
Creating a Time-Based (Version 1) UUID
If you have a need to sort or order the generated identifiers based on creation time, use −
uuidgen --time

Here, part of the UUID encodes the time and may help in scenarios like logging or system events where ordering matters.
Name-Based UUID Generation (Version 3 or Version 5)
Imagine you have a scenario where you want every domain name to correspond to the same UUID every time it is generated. This is useful for caching mechanisms or when IDs must be reproducible. Using MD5 to generate a UUID from a DNS name −
uuidgen --md5 --namespace @dns --name example.com

Alternatively, using SHA1 −
uuidgen --sha1 --namespace @dns --name example.com

For systems that require predictable and consistent UUIDs based on names (like mapping URLs or identifiers in configuration systems), these options are essential.
Use in Shell Scripts
The uuidgen command can easily be used in shell scripts. Consider the following example script that creates a unique filename and writes some temporary data to it −
#!/bin/ # Generate a random UUID id=$(uuidgen) filename="tempfile_${id}.txt" echo "Data for process $id" > "$filename" echo "Temporary file created: $filename" # Further processing with the temporary file can be done here
In this script −
- A UUID is generated and stored in the variable id.
- The UUID is then incorporated into a filename to ensure that the file name is unique.
- Data is written to the file without risking filename collisions.
Bootstrapping and Configuration
In configuration scripts, you may need to generate tokens or secret keys that persist over time. Running a command like −
uuidgen --random

It ensures that your application reads a freshly generated unique key upon startup. If needed, this could be combined with cron jobs or system initialization routines to rotate keys periodically.
Version and Help Flags
Always check the version if you run into compatibility issues −
uuidgen --version

And refer to −
uuidgen --help
Conclusion
The Linux uuidgen command is a powerful yet simple utility that embodies the Unix philosophy: a tool designed to perform one task exceptionally well. Whether you generate a single random UUID for a session token, create reproducible name-based UUIDs for a mapping system, or need a batch of identifiers for database records, uuidgen offers a variety of options to cater to your needs.