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.
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:
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
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
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.
Example 1: Generating a Default UUID (Version 4)
uuidgen
Example 2: Generating a Time-based UUID (Version 1)
uuidgen -r
Example 4: Bash script to generate multiple UUIDs
for i in {1..10}; do uuidgen; done
Example 5: Generate UUID on Python
uuid
module by running the command below.import uuid
uuid.uuid4()
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}")
python generate_uuid.py
Example 6: SELECT
statement using UUID
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 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
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}")
Run the Python file.
python api_key.py
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.