Sign In
Sign In

How To Generate Universally Unique Identifiers (UUIDs) with uuidgen

How To Generate Universally Unique Identifiers (UUIDs) with uuidgen
JC Brian Refugia
Technical writer
Linux
25.07.2024
Reading time: 4 min

Ensuring the uniqueness of identifiers is crucial in the computing industry, particularly when working with distributed systems, databases, and large-scale applications. UUIDs, or universally unique identifiers, are a commonly used fix for this issue. These 128-bit integers offer a dependable means of identifying data without collision because they are made to be globally unique.

The Linux uuidgen command is a simple but effective way to generate UUIDs. uuidgen provides a simple and effective approach to generate unique IDs, whether the user is a system administrator, a developer, or simply needs a reliable method. Acquiring knowledge with uuidgen will allow users to manage unique identification requirements in a range of situations and applications while ensuring data consistency and integrity.

Generating UUIDs with uuidgen

On Unix operating systems like Linux (Ubuntu, Debian, Redhat and others), the uuidgen command is typically pre-installed. If it has not already been installed, it can be installed using the package manager. To install the UUID-runtime package, execute the following commands: 

For Debian and Ubuntu based OS

  • Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below: 

sudo apt update && sudo apt upgrade -y
  • Install the uuid-runtime. Run the command below: 

sudo apt install uuid-runtime -y

For OS using RPM package (CentOS/RHEL/Fedora)

  • Update the package lists to ensure to have the most recent information on package versions and dependencies. Run the command below: 

sudo yum -y update
  • Install the uuid. Run the command below: 

sudo yum -y install uuid

Options, Parameters and Examples of Generating UUIDs

The uuidgen command mainly facilitates the creation of two types of UUIDs. These are: 

  • UUID Version 1 - Time-based, includes timestamp and node (MAC address).

  • UUID Version 4 – Randomly generated. 

Here are some examples of creating UUIDs with the uuidgen command on various platforms and with various parameters.

Generating UUIDs on Unix-based Systems (Linux)

Example 1: Generating a Default UUID (Version 4)

uuidgen

Image9

Example 2: Generating a Time-based UUID (Version 1)

uuidgen -r

Image5

Using UUID in Scripts

Example 4: Bash script to generate multiple UUIDs

for i in {1..10}; do uuidgen; done

Image17

Example 5: Generate UUID on Python

  • On the Python environment, import the uuid module by running the command below.
import uuid

Image10

  • Run the command below to generate UUID.
uuid.uuid4()

Image13

  • To generate uuid on Python script, create a Python script and name it generate_uuid.py and add the following lines then save and exit.
nano generate_uuid.py
import uuid
# Generate a random UUID (version 4) random_uuid = uuid.uuid4() print(f"Generated UUID: {random_uuid}")
# Generate a time-based UUID (version 1) time_uuid = uuid.uuid1() print(f"Time-based UUID: {time_uuid}")

Image14

  • Run the script to generate UUID using Python.
python generate_uuid.py

Image7

Using UUIDs in Database Operations

Example 6: SELECT statement using UUID

Image15

Use Cases for UUIDs

Since UUIDs generated by uuidgen are unique, they have a variety of use cases. Below are some of the several typical situations where UUIDs are beneficial:

  • Assigning unique identifiers to database

create table users(id varchar(36), name varchar(200));

insert into users values(uuid(), 'TESTUser');

Image3

  • Creating unique file names

Let’s see how to create a log file using the command generated by uuidgen

    • Declare a variable with file name (file_log) using the uuidgen command output.

file_log=$(uuidgen).log
    • Create the file under /tmp using the touch command.

touch /tmp/$file_log
    • Validate the output. The highlighted in yellow is the output generated by the command uuidgen.

ls -lrt /tmp/$file_log

Image12

  • Creating unique tokens or API keys for authentication using Python

    • Create a Python file with content below using any virtual editor.

nano api_key.py
import uuid

api_key = uuid.uuid4()
print(f"API Key: {api_key}")

Image6

    • Run the Python file.

python api_key.py

Image2

Conclusion

In summary, the uuidgen command is a straightforward and efficient tool for generating UUIDs, with primary options for generating random (Version 4) and time-based (Version 1) UUIDs. While the command itself has limited options, it can be combined with scripting techniques to generate multiple UUIDs or to redirect the output to files. Although the command itself doesn't have many arguments, it can be used in conjunction with scripting techniques to redirect the output to files or generate numerous UUIDs. UUIDs are widely used due to its benefits in scenarios where unique identifiers are required. 

Linux
25.07.2024
Reading time: 4 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us