惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

A
Arctic Wolf
博客园 - 聂微东
F
Fortinet All Blogs
云风的 BLOG
云风的 BLOG
小众软件
小众软件
V
Visual Studio Blog
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Apple Machine Learning Research
Apple Machine Learning Research
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
The Cloudflare Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
The GitHub Blog
The GitHub Blog
Y
Y Combinator Blog
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
博客园_首页
L
LangChain Blog
A
About on SuperTechFans
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
T
The Blog of Author Tim Ferriss
Security Latest
Security Latest
C
CXSECURITY Database RSS Feed - CXSecurity.com
Know Your Adversary
Know Your Adversary
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
P
Palo Alto Networks Blog
Scott Helme
Scott Helme
S
Secure Thoughts
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
Attack and Defense Labs
Attack and Defense Labs
P
Privacy & Cybersecurity Law Blog
O
OpenAI News
H
Heimdal Security Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Help Net Security
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Blog — PlanetScale
Blog — PlanetScale
GbyAI
GbyAI
G
Google Developers Blog
博客园 - Franky
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
K
Kaspersky official blog
Recent Commits to openclaw:main
Recent Commits to openclaw:main
T
Tor Project blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
T
Tenable Blog
Google Online Security Blog
Google Online Security Blog
PCI Perspectives
PCI Perspectives

DigitalOcean Community Tutorials

Mastering grep with Regular Expressions for Efficient Text Search It's Time to Break Up with Your Cloud: Why AI Teams are Switching We Built a Private-Document AI App to Test Platform Security. Here Is What We Could Actually Verify. PostgreSQL Explained: A Complete Beginner-to-Advanced Guide How To Install and Configure Postfix on Ubuntu How To Build a Web Application Using Flask in Python 3 Build AI Reading List with DigitalOcean Functions and Mistral How To Concatenate Strings in Python How to Allow MySQL Remote Access Securely How To Install and Use Docker on Rocky Linux How To Build a Multi-Agent AI System with Docker Agent DSPy Use Cases: Build Optimized LLM Pipelines How To Submit AJAX Forms with jQuery Build an AI-Powered GPU Fleet Optimizer with the DigitalOcean AI Platform ADK Monitor GPU Utilization in Real Time: A Complete Guide Reduce File Size of Images in Linux - CLI and GUI methods Reduce PDF File Size in Linux: Tools and Methods How To Set Up a Private Docker Registry on Ubuntu How To Troubleshoot Terraform: Errors and Fixes How to Use Go Modules Python Multiprocessing Example: Process, Pool & Queue Convert Class Components to Functional Components with React Hooks How To Install and Configure Ansible on Ubuntu LLM Tokenizers Simplified: BPE, SentencePiece, and More How To Monitor System Authentication Logs on Ubuntu How to Use Traceroute and MTR to Diagnose Network Issues How to Deploy Postgres to Kubernetes Cluster Importing Packages in Go: A Complete Guide Create RAID Arrays with mdadm on Ubuntu How To Make an HTTP Server in Go How To Set Up Time Synchronization on Ubuntu How To Use Struct Tags in Go apt-key Deprecation: Add Repositories with GPG on Ubuntu Linux ps Command: 20 Real-World Examples Python struct.pack and struct.unpack for Binary Data Deadlock in Java: Examples, Detection, and Prevention How To Use Find and Locate to Search for Files on Linux Structured Resume Skill Extraction Using Mistral-7B Inference How to Use the Python Main Function How to Set Up NemoClaw on a DigitalOcean Droplet with 1-Click Build an End-to-End RAG Pipeline for LLM Applications From Single to Multi-Agent Systems: Key Infrastructure Needs Back Up Data to Object Storage Using Restic How to Generate Videos with LTX-2.3 on DigitalOcean GPU Droplets How To Install LAMP Stack (Apache, MySQL, PHP) on Ubuntu How to Download Files with cURL How To Use Variadic Functions in Go How To Use EJS to Template Your Node Application How to Install Node.js on Ubuntu (Step-by-Step Guide) MongoDB Indexes: Improve Query Performance with Node.js LLM Tool Calling with DigitalOcean AI Platform and Databases What are Text Diffusion Models? - An Overview Crafting a Game from Scratch with GPT-5.4 Building Long-Term Memory in AI Agents with LangGraph and Mem0 How To Install PHP 7.4 and Set Up a Local Development Environment on Ubuntu 20.04 Build a GraphQL API in Go to Upload Files to Spaces How To Lint and Format Code with ESLint in Visual Studio Code Train YOLO26 for Retail Object Detection on DigitalOcean GPUs How To Work with JSON in MySQL How to Use the JavaScript .map() Method Building a Scalable App with MongoDB Using DigitalOcean's MCP Server How to Create an SSH Key in Linux: Easy Step-by-Step Guide Measure MySQL Query Performance with mysqlslap How To Use *args and **kwargs in Python 3 Nemotron 3 helped me find the perfect dish rack? A2A vs MCP - How These AI Agent Protocols Actually Differ How To Install and Manage Supervisor Docker Container Images with Watchtower on Ubuntu Getting Started with Qwen3.5 Vision-Language Models How To Create a New Sudo-Enabled User on Ubuntu How to Use Ansible to Install and Set Up Docker on Ubuntu How To Enable Remote Desktop Protocol Using xrdp on Ubuntu 22.04 How To Convert a String to a List in Python How To Check If a String Contains Another String in Python How to Read a Properties File in Python Python Command Line Arguments: sys.argv, argparse, getopt Mastering Grep command in Linux/Unix: A Beginner's Tutorial Understanding Python Data Types How to Implement a Stack in C With Code Examples Python os.system() vs subprocess: Run System Commands How To Install and Use Docker Compose on Ubuntu How to Add and Delete Users on Ubuntu How To Order Query Results in Laravel Eloquent How To Define and Use Handlers in Ansible Playbooks How To Install and Use SQLite on Ubuntu How To Install and Use Homebrew on macOS How To Manage DateTime with Carbon in Laravel and PHP How To Install Git on Ubuntu How To Install and Secure Redis on Ubuntu How To Build and Install Go Programs on Linux Using ldflags to Set Version Information for Go Applications How To Build a Node.js Application with Docker How To Add JavaScript to HTML How To Reset Your MySQL or MariaDB Root Password How To Add Images in Markdown How To Set Up a Production Elasticsearch Cluster with Ansible How To Set Up a Firewall Using firewalld on CentOS Understanding Systemd Units and Unit Files How To Set Up Replication in MySQL How To Use the .htaccess File
Generate UUIDs with uuidgen on Linux
2026-03-18 · via DigitalOcean Community Tutorials

Introduction

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.

Key Takeaways

  • A UUID is a 128-bit identifier in 8-4-4-4-12 hexadecimal format, defined by RFC 4122.
  • 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.
  • Use $(uuidgen) in shell scripts for command substitution; prefer it over backticks for nesting and readability.
  • The Linux kernel exposes a UUID at /proc/sys/kernel/random/uuid for Version 4 generation without installing a package.
  • For database primary keys and most applications, Version 4 (random) is the recommended choice when determinism is not required.

What Is a UUID?

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.

Prerequisites

  • A Linux system (Ubuntu, Debian, RHEL, Fedora, CentOS Stream, or compatible distribution).
  • Access to install packages (sudo or root).
  • Basic familiarity with the Linux terminal (see our guide: Introduction to the Linux Terminal).

Installing uuidgen on Linux

Debian and Ubuntu

sudo apt update && sudo apt install uuid-runtime

RHEL, Fedora, and CentOS Stream

sudo dnf install util-linux

Verify the installation

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.

Generating a Basic UUID with uuidgen

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

Generating a Random UUID (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.

Generating a Time-Based UUID (Version 1)

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.

Generating Namespace-Based UUIDs (Version 3 and Version 5)

Version 3 (MD5 namespace hash) with -m

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.

Version 5 (SHA-1 namespace hash) with -s

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.

When to use namespace UUIDs

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.

UUID Version Comparison Table

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

Using uuidgen in Shell Scripts

Assigning a UUID to a variable

MY_UUID=$(uuidgen)
echo "$MY_UUID"

Output

c7e2a041-3b8d-4f19-a536-9d1e20b74c38

Generating multiple UUIDs in a loop

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

Generating UUID-based test data (CSV format)

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

Generating UUID-based email addresses and saving to a file

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.

Using uuidgen output as a filename prefix

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

Alternative Methods to Generate UUIDs on Linux

/proc/sys/kernel/random/uuid (kernel-native, no package required)

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.

Python uuid module

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.

/dev/urandom with tr and sed (manual construction, illustrative only)

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

Frequently Asked Questions

What is the difference between UUID v1 and UUID v4?

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.

Is uuidgen installed by default on Linux?

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).

Can two UUIDs ever be the same?

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.

What is a namespace UUID and when should I use it?

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.

How do I generate multiple UUIDs at once using uuidgen?

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.

What do the -m and -s flags do in uuidgen?

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.

Is there a way to generate UUIDs without installing any packages?

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.

What UUID version should I use for a database primary key?

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).

Conclusion

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.

Creative CommonsThis work is licensed under a Creative Commons Attribution-NonCommercial- ShareAlike 4.0 International License.