How to Generate UUID in Linux

Generate UUID in Linux

A UUID (Universally Unique IDentifier) is a 128-bit number used to uniquely identify some object or entity on the Internet. Depending on the specific mechanisms used, a UUID is either guaranteed to be different or is, at least, extremely likely to be different from any other UUID generated until 3400 A.D. UUIDs are used in many places, from tagging objects with an extremely short lifetime, to reliably identifying very persistent objects across a network.

Prerequisites

Before we delve into the methods of generating UUIDs in Linux, it’s important to have a basic understanding of the Linux command line and access to a Linux machine, either physical or virtual.

Methods to Generate UUID

uuidgen

The simplest method to generate a UUID in Linux is by using the uuidgen command. This command is a part of the uuid-runtime package and can be installed using the apt package manager with the command sudo apt install uuid-runtime.

To generate a single UUID, run the uuidgen command without any arguments. This will generate a random UUID each time it runs. For example:

uuidgen

 You should see output similar to the following, although your UUID will be different:

a522f494-92ce-44e9-b1a3-f891baed8d60
You can also generate multiple UUIDs at once by leveraging a small bit of shell scripting using the for loop to execute the uuidgen command multiple times. For example, to generate 10 UUIDs, execute the following command:
for i in {1..10}; do uuidgen; done

This will print 10 UUIDs to the screen.

cat /proc/sys/kernel/random/uuid

Another method to generate a UUID in Linux is by leveraging the kernel’s random number generator. This method doesn’t require the uuidgen package and can be used by reading the /proc/sys/kernel/random/uuid file:

cat /proc/sys/kernel/random/uuid

This command will generate a different random UUID each time it’s executed.

OpenSSL

The OpenSSL command-line tool can also be used to generate cryptographically secure random UUIDs. Here’s how to generate a UUID with OpenSSL:

uuid=$(openssl rand -hex 16)
echo ${uuid:0:8}-${uuid:8:4}-${uuid:12:4}-${uuid:16:4}-${uuid:20:12}

This command will generate a random UUID and output it to the console.

Bash Scripting

You can also generate UUIDs in Bash scripts. This can be useful when you need to generate multiple UUIDs or integrate UUID generation into a larger script. Here’s an example of how to generate a UUID in a Bash script:

#!/bin/bash
uuid=$(uuidgen)
echo $uuid

UUID Versions

There are five versions of UUIDs, each with different characteristics:

  • Version 1: Time-based UUID
  • Version 2: DCE security UUID
  • Version 3: Name-based UUID using MD5
  • Version 4: Random UUID
  • Version 5: Name-based UUID using SHA-1

The uuidgen command can generate three types of UUIDs: time-based UUIDs, random-based UUIDs, and hash-based UUIDs. By default, uuidgen will generate a random-based UUID if a high-quality random number generator is present. Otherwise, it will choose a time-based UUID. It is possible to force the generation of one of these first two UUID types by using the --random or --time options. The third type of UUID is generated with the --md5 or --sha1 options, followed by --namespace namespace and --name name.

Use Cases

UUIDs are commonly used as database primary keys. They provide a reliable way to uniquely identify records, even across different databases. UUIDs are also used as unique identifiers in applications, where they can help track user sessions, generate unique file names, or identify resources. Additionally, UUIDs can be used for anonymization, as they don’t reveal any information about the data they represent.

Conclusion

In this article, we’ve covered several methods to generate UUIDs in Linux, including using the uuidgen command, reading the /proc/sys/kernel/random/uuid file, using the OpenSSL command-line tool, and generating UUIDs in Bash scripts. We’ve also discussed the different versions of UUIDs and their use cases. By using these methods, you can generate UUIDs for a wide variety of applications and environments.
Marshall Anthony is a professional Linux DevOps writer with a passion for technology and innovation. With over 8 years of experience in the industry, he has become a go-to expert for anyone looking to learn more about Linux.

Related Posts