A Universally Unique Identifier (UUID)—also known as a Globally Unique Identifier (GUID)—is a standardised 128-bit label formatted as 32 hexadecimal digits separated by hyphens (e.g. c8f42d20-3b4e-4e6f-87d3-1a2f9c84e1b5). UUIDs are widely utilised across modern computing to uniquely identify database primary keys, microservice transaction traces, distributed message queues, API sessions, and block storage devices inside /etc/fstab.
On Linux systems, generating cryptographically sound, RFC 4122-compliant UUIDs does not require external third-party software. Modern distributions provide built-in command-line tools and kernel interfaces that generate UUIDs instantly. This guide covers how to generate random (v4) and time-based (v1) UUIDs using uuidgen, the kernel pseudo-filesystem, Python one-liners, and automated shell scripts.
Method 1: Generate UUIDs Using the uuidgen Command
The standard and most versatile utility for generating unique identifiers in Linux is uuidgen. It is part of the core util-linux package, which comes pre-installed on virtually all Linux distributions, including Ubuntu, Debian, Fedora, CentOS, and Arch Linux.
Generating a Random UUID (Version 4)
Version 4 UUIDs rely on high-entropy cryptographically secure pseudorandom numbers generated by the system. To generate a standard random UUID, run:
uuidgen -r
Alternatively, executing uuidgen without any flags also defaults to random v4 generation on modern systems:
uuidgen
Generating a Time-Based UUID (Version 1)
Version 1 UUIDs are computed from the system clock timestamp combined with the system’s network interface MAC address. To generate a time-based UUID:
uuidgen -t
Security Tip: Avoid using Version 1 UUIDs in security-sensitive contexts, public URLs, or authentication tokens. Because UUID v1 includes your network hardware MAC address and an exact microsecond timestamp, an external actor could theoretically deduce the identity of the host machine and the exact time the token was minted.
Method 2: Generate UUIDs Directly from the Linux Kernel (/proc)
If you are writing a lightweight bash script for an embedded Linux system, minimal Alpine Docker container, or bare-metal environment where uuidgen is not installed, you can read directly from the Linux kernel’s random number generator via the /proc pseudo-filesystem:
cat /proc/sys/kernel/random/uuid
Every time you read this virtual file, the Linux kernel dynamically generates a fresh, RFC 4122-compliant Version 4 UUID. Because this method requires no external binary and relies solely on shell built-ins and kernel drivers, it is exceptionally fast and guaranteed to work across any modern Linux installation.
Method 3: Generate UUIDs Using Python
Python is installed by default on almost all Linux developer workstations and servers. Python’s standard library includes a dedicated uuid module that can generate identifiers directly from the shell:
Random UUID (v4):
python3 -c 'import uuid; print(uuid.uuid4())'
Time-Based UUID (v1):
python3 -c 'import uuid; print(uuid.uuid1())'
This approach is particularly convenient when you want to pipe generated UUIDs directly into JSON payloads or Python automation pipelines.
How to Format UUIDs: Uppercase, Lowercase, and Stripped Hyphens
Different database engines and programming frameworks require specific UUID formatting conventions. You can manipulate the standard output using standard Linux text utilities:
Lowercase (Standard):
uuidgen | tr '[:upper:]' '[:lower:]'
Uppercase:
uuidgen | tr '[:lower:]' '[:upper:]'
Without Hyphens (32-character Hexadecimal String):
uuidgen | tr -d '-'
How to Batch Generate Multiple UUIDs for Test Fixtures
When populating mock databases, seed scripts, or test CSV files with thousands of unique records, you can generate a list of UUIDs in bulk using a simple Bash loop:
# Generate 10 random UUIDs and display them in the terminal
for i in {1..10}; do uuidgen; done
# Generate 500 UUIDs and save them to a file
for i in {1..500}; do cat /proc/sys/kernel/random/uuid; done > uuids.txt
Comparing UUID Version 4 vs Version 1
Choosing the appropriate UUID generation method depends on your technical use case:
- UUID Version 4 (Random): Features 122 bits of pure randomness, giving a collision probability so statistically negligible that it is virtually impossible to generate duplicate entries. It is the global standard for distributed database keys, session cookies, and public identifiers.
- UUID Version 1 (Time-Based): Best suited when sequential sorting or creation order is necessary at the database level, as timestamps are embedded directly into the structure. However, consider the privacy implications of exposing the system MAC address.
By leveraging uuidgen and the native kernel /proc/sys/kernel/random/uuid interface, you can generate secure, standards-compliant identifiers quickly in any Linux environment.