





















A universally unique identifier (UUID) is a 128-bit value standardized under RFC 4122 and commonly used to uniquely identify resources across systems. The uuidgen command generates RFC 4122-compliant UUIDs from the command line and is provided by the uuid-runtime package on Debian and Ubuntu and by util-linux on RHEL, Fedora, and CentOS Stream. This article covers UUID versions (random, time-based, and namespace-based), installation on major Linux distributions, using uuidgen in shell scripts, and alternative methods to generate UUIDs on Linux.
uuidgen with no flags produces a random (Version 4) UUID on most modern Linux systems.uuidgen -r generates a Version 4 (random) UUID using /dev/urandom via libuuid.uuidgen -t generates a Version 1 (time-based) UUID that encodes the host MAC address and a 60-bit timestamp.uuidgen --md5 --namespace @dns --name "example.com" produces a deterministic Version 3 UUID; --sha1 produces Version 5.$(uuidgen) in shell scripts for command substitution; prefer it over backticks for nesting and readability./proc/sys/kernel/random/uuid for Version 4 generation without installing a package.A UUID is a 128-bit value represented as 32 hexadecimal digits in the form 8-4-4-4-12: eight hex digits, a hyphen, four, a hyphen, four, a hyphen, four, a hyphen, and twelve. The format can be written as:
xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx
Here M is the version digit (1, 3, 4, or 5) and N encodes the variant bits per RFC 4122. In distributed systems, UUIDs avoid the coordination and single-point-of-failure issues of auto-increment integers, since each node can generate identifiers independently without contacting a central authority. On Linux, uuidgen is implemented by the libuuid library, which handles version-specific generation and RFC 4122 compliance.
sudo apt update && sudo apt install uuid-runtime
sudo dnf install util-linux
uuidgen --version
RHEL / Fedora / CentOS Stream
uuidgen from util-linux 2.39.2
Note: On most Ubuntu and Debian systems, uuidgen ships pre-installed as part of uuid-runtime. Run which uuidgen to confirm before installing. The version string format varies by distribution – systems using util-linux will show a different output than those using uuid-runtime.
Run uuidgen with no arguments to produce a single UUID:
uuidgen
The output is a Version 4 UUID by default on modern Linux systems. Each segment maps to the 8-4-4-4-12 structure defined by RFC 4122: the third group’s leading digit identifies the version (4 in this case), and the fourth group’s leading digit identifies the variant. If your environment defaults to time-based generation, use the -r flag explicitly – covered in the next section.
Output
a522f494-92ce-44e9-b1a3-f891baed8d60
↑ third group starts with 4 = Version 4
To request a random UUID explicitly, use the -r flag:
uuidgen -r
Output
e8fa8d54-641a-4d7b-9422-91474d713c62
Version 4 UUIDs are derived from random or pseudo-random data. On Linux, libuuid uses /dev/urandom as the source of entropy. The birthday problem puts the 50% collision threshold at approximately 2.71 quintillion (2.71 x 10^18) Version 4 UUIDs, far beyond any realistic single-system workload. Use -r explicitly when the default might fall back to time-based generation (for example on some legacy or minimal environments) and you need random UUIDs.
Use the -t flag to generate a time-based (Version 1) UUID:
uuidgen -t
Output
2d5a8c3e-7f1b-11ee-b4a2-4c796e3a8f01
UUID v1 field breakdown
2d5a8c3e - 7f1b - 11ee - b4a2 - 4c796e3a8f01
^ ^ ^
version variant node (MAC address)
Version 1 UUIDs encode a 60-bit timestamp and the MAC address of the host that generated the UUID. They are time-ordered and can be useful for sorting or debugging, but they reveal the generating machine’s hardware address – making them useful for time-ordered logging or debugging scenarios where correlating a UUID with its generation time has operational value.
Warning: Version 1 UUIDs expose the MAC address of the host that generated them. Avoid using uuidgen -t in contexts where the generating machine’s identity must remain private, such as public-facing APIs or anonymized datasets.
The --namespace flag accepts a predefined namespace identifier: @dns for domain names, @url for URLs, @oid for OIDs, and @x500 for X.500 distinguished names. The --name flag takes the string to hash. The same namespace and name pair always produces the same UUID on any system.
uuidgen --md5 --namespace @dns --name "example.com"
Output
9073926b-929f-31c2-abc9-fad77ae3e8eb
Run the same command a second time to confirm the deterministic behavior:
uuidgen --md5 --namespace @dns --name "example.com"
Output
9073926b-929f-31c2-abc9-fad77ae3e8eb
The output is identical. This is the property that makes Version 3 and Version 5 UUIDs useful for idempotent operations: any service, on any machine, given the same namespace and name, will derive the same identifier without coordination.
The --namespace flag accepts a predefined namespace identifier: @dns for domain names, @url for URLs, @oid for OIDs, and @x500 for X.500 distinguished names. The --name flag takes the string to hash. The same namespace and name pair always produces the same UUID on any system. Version 5 uses SHA-1 instead of MD5 and is preferred for new implementations because SHA-1 offers better collision resistance.
uuidgen --sha1 --namespace @dns --name "example.com"
Output
cfbff0d1-9375-5685-968c-48ce8b15ae17
Run the same command a second time to confirm the deterministic behavior:
uuidgen --sha1 --namespace @dns --name "example.com"
Output
cfbff0d1-9375-5685-968c-48ce8b15ae17
The output is identical. This is the property that makes Version 3 and Version 5 UUIDs useful for idempotent operations: any service, on any machine, given the same namespace and name, will derive the same identifier without coordination.
Note: The -m and -s flags are aliases for --md5 and --sha1. Both forms are accepted.
Reach for namespace UUIDs when the same logical resource must map to the same identifier across independent systems. For example, if two services both need a stable UUID for the domain api.example.com, both can derive uuidgen --sha1 --namespace @url --name "api.example.com" independently and arrive at the same value without a shared database or coordination step. This pattern is also useful for content-addressable storage, where a file’s UUID is derived from its canonical name rather than generated at upload time, and for idempotent record creation in distributed pipelines where re-running a job must not create duplicate rows.
| Version | Generation Method | Flag | Deterministic | Privacy Safe | Recommended Use Case |
|---|---|---|---|---|---|
| 1 | Time + MAC address | -t |
No | No | Time-ordered IDs where MAC exposure is acceptable |
| 3 | MD5 hash of namespace + name | --md5 / -m |
Yes | Yes | Legacy or when MD5 namespace UUIDs already exist |
| 4 | Random (e.g. /dev/urandom) |
-r or default |
No | Yes | General-purpose unique IDs, primary keys, APIs |
| 5 | SHA-1 hash of namespace + name | --sha1 / -s |
Yes | Yes | Deterministic IDs from names; preferred over v3 for new use |
MY_UUID=$(uuidgen)
echo "$MY_UUID"
Output
c7e2a041-3b8d-4f19-a536-9d1e20b74c38
for i in {1..10}; do uuidgen; done
Output
834efdb6-6044-4b44-8fcb-560710936f37
f3a1d72c-0b5e-4c91-ae34-7d2f19c80345
91c4b037-5e2a-4d08-b1f6-3c8e20a74f29
511fea83-9f5f-4606-85ec-3d769da4bf63
3bc82ef7-1138-4f97-945a-08626a42a648
5f9d1c72-8e3a-4b06-b2f4-1a7c30d85e29
2a38839e-3b0d-47f0-9e60-d6b19c0978ad
74dca5e8-c702-4e70-ad16-0a16a64d55fa
cd13d088-21cf-4286-ae61-0643d321dd9e
9aec3d5a-a339-4f24-b5a3-8419ac8542f2
for i in {1..10}; do echo "$(uuidgen),$(uuidgen)"; done
Output
63b1146f-9e7c-4e1f-82eb-3fe378e203df,ed9d6201-e5b2-4410-9ab1-35c8ca037994
8d3981b6-f112-4f21-ac4b-44791e279b2a,eb63310e-d436-44fa-80c6-65721a300a2b
0eddfe24-1c2e-43a1-b2c2-9d3af6bad837,62ef1782-76a2-4b3c-ac69-1c2d02f65789
29f18766-fc9d-46a4-a1d0-e112738edb30,b6bd303d-1148-4f46-bec7-d7e4cb6e4f03
865bcf30-6a8b-49d6-8b27-8dc51620adf7,972b0959-4270-4683-b19b-360b2605f2d0
0d82d54b-566a-45d1-b3a8-5da1a88bceb3,1c67a802-9647-46b1-bde4-3053699b27f9
778b5415-3e1f-4bc5-a349-499459ac4ab7,7e1a2081-c742-4882-9154-e5d2a4af630c
e6cc95bd-3ee1-43cb-bea1-51783de5fc57,5088d3a3-ab67-4684-8761-e48bb14596ec
a7453bc0-b5e5-41a3-9ed4-cf4d8e0908a2,957ef50f-7889-4335-9f40-17878e3d20fe
3689362d-588a-409e-bd2c-d6fdaa361574,9ffe7c8d-9afb-4b24-a5b7-b29a06f6fac7
for i in {1..10}; do echo "$(uuidgen)@mailinator.com"; done > /tmp/test-emails.txt
cat /tmp/test-emails.txt
Output
4ba50929-520b-49f7-996d-e369be5d6232@mailinator.com
16deaeae-64bd-45f0-9f73-b32d41ca1bfb@mailinator.com
743701e8-0dc5-4851-8fc4-24d155755bdc@mailinator.com
adff0015-c535-431a-970f-98ffd1fc21eb@mailinator.com
6516cfb3-e54f-4800-a6cc-11d50d756f28@mailinator.com
8a9c5252-bd0c-4c3b-a7c9-4b60ebcc4294@mailinator.com
eed94fd6-b075-493c-8d8e-3acae90d5629@mailinator.com
f4ab80d2-85ca-4722-a260-0f84c37051fd@mailinator.com
53ead1d0-cc70-410f-a91a-4a79b339fba2@mailinator.com
b208e103-d7f1-4f6d-838d-530d6339dce7@mailinator.com
The mailinator.com domain is a disposable address service often used for test data and pipelines where you need valid-looking, monitorable addresses without real mailboxes.
Note: This tutorial uses $(uuidgen) instead of `uuidgen` for command substitution. The $() form is preferred in modern shell scripting because it is easier to nest and read.
Prefixing filenames with a UUID prevents naming collisions in environments where multiple processes write output files concurrently.
FILENAME="$(uuidgen)-report.log"
touch "$FILENAME"
echo "Created: $FILENAME"
Output
Created: 2e8b4f91-7c3d-4a05-b6e2-1d9f30c87a54-report.log
cat /proc/sys/kernel/random/uuid
Output
3f7a92b1-dc4e-4b3e-9c12-1a6f2e8d0471
The Linux kernel provides a UUID generator at this path. Each read returns a new Version 4 UUID. No package installation is required. This interface is not available on macOS.
python3 -c "import uuid; print(uuid.uuid4())"
Output
b47d1c09-3f8a-4e62-a015-9d2c7f304851
Python’s standard library uuid module supports Version 1, 3, 4, and 5. Use it when uuidgen is not installed or when UUID generation must live inside a Python script.
cat /dev/urandom | tr -dc 'a-f0-9' | fold -w 32 | head -1 | \
sed 's/\(.\{8\}\)\(.\{4\}\)\(.\{4\}\)\(.\{4\}\)\(.\{12\}\)/\1-\2-\3-\4-\5/'
Output
d91c4b72-1e3a-4f08-b236-5c0e8a917f43
This command produces a hex string that looks like a UUID but is not RFC 4122-compliant in two specific ways: the version bits in the M position (third group, first digit) are not set to any valid UUID version, and the variant bits in the N position (fourth group, first digit) are not constrained to the 10xx binary pattern required by RFC 4122. Tools and libraries that validate UUID structure will reject this output.
Warning: This method produces a random hex string in UUID format but does not set the version or variant bits required by RFC 4122. It is shown for illustration only. Use uuidgen or a compliant UUID library in production.
| Method | Requires Package | UUID Version | RFC 4122 Compliant | Notes |
|---|---|---|---|---|
| uuidgen | Yes (uuid-runtime or util-linux) | 1, 3, 4, 5 | Yes | Full control via flags; scriptable |
| /proc/sys/kernel/random/uuid | No | 4 | Yes | Linux only; no version choice |
| Python uuid | Python installed | 1, 3, 4, 5 | Yes | Good for Python-based workflows |
| /dev/urandom + sed | No | None | No | Illustrative only; not for production |
UUID v1 is time-based and encodes the host MAC address and a 60-bit timestamp, so it is sortable and deterministic per machine but exposes hardware identity. UUID v4 is random (or pseudo-random) and does not encode time or MAC address, so it is privacy-safe and preferred for most applications where you do not need time-ordering or determinism.
On many Ubuntu and Debian systems, uuidgen is pre-installed as part of uuid-runtime. On RHEL, Fedora, and CentOS Stream it is part of util-linux, which is usually present. Run which uuidgen to check; if it is missing, install uuid-runtime (Debian/Ubuntu) or util-linux (RHEL/Fedora).
Theoretically yes, but for Version 4 UUIDs the probability is extremely low. The birthday problem puts the 50% collision threshold at approximately 2.71 quintillion (2.71 x 10^18) Version 4 UUIDs, far beyond any realistic single-system workload. Version 3 and 5 UUIDs are intentionally deterministic – the same namespace and name always produce the same UUID by design.
A namespace UUID is a fixed UUID used together with a name string to generate deterministic UUIDs (Version 3 with MD5 or Version 5 with SHA-1). Use namespace UUIDs when you need the same identifier for the same logical resource across systems, for content-addressable naming, or for idempotent record creation. Prefer Version 5 over Version 3 for new implementations.
Use a loop: for i in {1..N}; do uuidgen; done where N is the number of UUIDs. To assign each to a variable or write to a file, combine with command substitution and redirection as shown in the shell scripting section.
The -m (or --md5) flag generates a Version 3 UUID by hashing a namespace UUID and a name string with MD5. The -s (or --sha1) flag generates a Version 5 UUID using SHA-1 instead. Both produce deterministic UUIDs; use --namespace and --name to specify the inputs.
On Linux, read from /proc/sys/kernel/random/uuid with cat /proc/sys/kernel/random/uuid. Each read returns a new Version 4 UUID. No extra packages are required. This path is not available on macOS.
For most database primary keys, use Version 4 (random). It does not leak host or time information and has negligible collision probability for typical workloads. Use Version 5 (or 3) only when you need deterministic, reproducible IDs from a namespace and name (for example for content-addressable or merge-friendly data).
The uuidgen command produces RFC 4122-compliant UUIDs from the command line with no external dependencies beyond uuid-runtime (or util-linux on RHEL-family systems). You can generate random (v4), time-based (v1), and namespace-based (v3, v5) UUIDs and integrate them into shell scripts for variables, loops, test data, and filenames. For more command-line fundamentals, see Linux commands and How to use bash history, commands, and expansions on a Linux VPS. Run man uuidgen on your system to see the full list of flags and options for your implementation.
This work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。