How To Generate Universally Unique Identifiers (UUIDs) with uuidgen
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 Copy link
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 -yFor 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 uuidOptions, Parameters and Examples of Generating UUIDs Copy link
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) Copy link
Example 1: Generating a Default UUID (Version 4)
uuidgenExample 2: Generating a Time-based UUID (Version 1)
uuidgen -rUsing UUID in Scripts Copy link
Example 4: Bash script to generate multiple UUIDs
for i in {1..10}; do uuidgen; doneExample 5: Generate UUID on Python
- On the Python environment, import the
uuidmodule by running the command below.
import uuid- Run the command below to generate UUID.
uuid.uuid4()- To generate uuid on Python script, create a Python script and name it
generate_uuid.pyand add the following lines then save and exit.
nano generate_uuid.pyimport 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}")
- Run the script to generate UUID using Python.
python generate_uuid.pyUsing UUIDs in Database Operations Copy link
Example 6: SELECT statement using UUID
Use Cases for UUIDs Copy link
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');-
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 theuuidgencommand output.
-
file_log=$(uuidgen).log-
-
Create the file under
/tmpusing thetouchcommand.
-
touch /tmp/$file_log-
-
Validate the output. The highlighted in yellow is the output generated by the command
uuidgen.
-
ls -lrt /tmp/$file_log-
Creating unique tokens or API keys for authentication using Python
-
-
Create a Python file with content below using any virtual editor.
-
nano api_key.pyimport uuid
api_key = uuid.uuid4()
print(f"API Key: {api_key}")
-
-
Run the Python file.
-
python api_key.pyConclusion Copy link
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.
You can try our reliable Linux VPS hosting for your projects.