How to generate uuid linux

How to Generate UUID in Linux

UUID (Universally Unique identifier) is a 128-bit unique number standardized by the Open Software Foundation. This number is universally unique and really impossible for a user to guess.

This tutorial shows how to generate UUID in Linux using uuidgen command line utility tool.

Generate UUID in Linux

Most of the Linux/Unix system supports the command line utility tool uuidgen to generate the UUID. If the package is unavailable install using apt install uuid-runtime command.

Uuidgen can generate random-based, time-based and hash-based UUIDs. Random based UUIDs is sufficient in most cases.

Simply executing uuidgen generates a random UUID.

You can use the -t flag along with uuidgen to generate a UUID based on system time. In the same way uuidgen command with option -r generates the random UUID based mostly on random bits.

Followings are the some example to generate UUIDs.

To create a random UUID, use the following command:

To generate time based UUID, run:

Use the command uuidgen with the option -r to generate random UUID.

To generate hash based UUID use —md5 or —shal with —namespace .

Conclusion

In this tutorial, we learned how to generate UUID in Linux. Finding UUID in Linux pretty easy, which is commonly required when it to comes to mount storage devices in fstab file.

Источник

How To Generate Universally Unique Identifiers (UUIDs) with uuidgen

Last Validated on October 16, 2020 Originally Published on July 19, 2019

Introduction

Universally unique identifiers (UUIDs) are 128-bit numbers that are accepted as being unique on the local system they are created on as well as among the UUIDs created on other systems in the past as well as the future. Because of their uniqueness, they come in handy in situations where an auto incremented primary key can fall short.

Because of their uniqueness, UUIDs are well suited for generating test data. Need a random string? A UUID is fine. What about an email? UUID@UUID.com is great. Need a bunch of random string? UUIDs will be unique, making them easy to track down as they move through a system.

To generate universally unique identifiers from the command-line interface, you can use the uuidgen utility,

In this tutorial you’ll use uuidgen and some shell scripting to generate UUIDs and some sample data.

Generating a UUID

The uuidgen command is often already installed on Unix-like operating systems like Linux and macOS. If it’s not, you can install it through your package manager. On Ubuntu and Debian systems, install the uuid-runtime package.

Читайте также:  Hkey current user software microsoft windows currentversion explorer fileexts lnk

First, update your system’s list of available packages using the apt update command:

Next install the uuid-runtime package using the apt package manager:

To generate a single UUID, run the uuidgen command without any arguments. Out of the box, uuidgen will generate a random UUID each time it runs.

Execute the following command in your terminal:

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

Note: The macOS version of uuidgen does function a bit differently than that of the Linux version in that is returns UUIDs in all capital letters.

You can also generate time-based and hash-based UUIDs, but generally speaking, the random values are sufficient for most cases.

You may want to generate multiple UUIDs at once, which you’ll explore next.

Generating Multiple UUIDs

To generate a bunch of UUIDs at once, you’ll leverage 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:

You’ll see 10 UUIDs printed to the screen:

You can swap the 10 out for the number you’d like.

Based on the unique nature of UUIDs, you don’t have to worry about any duplicates in your generated data. Now let’s look at using UUIDs in different ways.

Using UUIDs in Test Data

If you wanted to generate a list of comma-separated values (CSV) with 2 UUIDs per line, you would use the echo command to print two UUIDs during each iteration of the for loop.
Execute the following command:

You’ll get this output:

Using the same approach, you can generate data that looks like email addresses by making a small tweak to the echo statement:

You’ll receive this output:

These aren’t real email addresses that you can validate, but you can tweak the output one more time, and swap the second uuidgen for a disposable email address domain, like [mailinator.com](https://mailinator.com], you will not only have a list of realistic-looking data, but it will be a list email addresses you could actually use or monitor in tests. Try the following command:

This time you’ll see output like this:

Finally, to save the output of this command to a file, you can append > /path/to/some/file to pipe the output:

Then use the cat command to view the file you just created:

The file is displayed on your screen:

You can use the same approach to save any of the other examples in this tutorial to files.

Conclusion

Universally unique identifiers are more robust than a random number. Their uniqueness makes them quite powerful. Combined with some light shell scripting on the command-line interface, you’re able to generate useful data, without needing to load up your favorite programming language’s package repository.

Next time you’re in need of a UUID, save yourself the search for “online UUID generator” and use your system’s uuidgen command. To learn more about your system’s particular implementation of uuidgen , type man uuidgen in your terminal to view its documentation..

Читайте также:  Linux обзор дистрибутивов 2021

Источник

Linux command to generate new GUID?

Sometimes in bash scripting, i need to generate new GUID(Global Unique Identifier) .

I already done that by a simple python script that generates a new guid: see here

But i need to copy this script into any new system that i works on.

My question is: can anybody introduce a command or package that contains similar command ?

6 Answers 6

Assuming you don’t have uuidgen , you don’t need a script:

You can use command uuidgen . Simply executing uuidgen will give you time-based UUID:

Since you wanted a random UUID, you want to use Type 4 instead of Type 1:

This Wikipedia article explains the different types of UUIDs. You want «Type 4 (Random)».

I wrote a little Bash function using Python to generate an arbitrary number of Type 4 UUIDs in bulk:

If you prefer lowercase, change:

In Python 3, no cast to str is required:

If you just want to generate a pseudo-random string with some dashes at locations 8, 12, 16, and 20, you can use apg .

The apg clause generates 32 symbols from [0-9a-f] (lower-case). The series of sed commands add the — tokens and can very likely be shortened.

Note that often an UUID has a particular format:

Here M and N fields encode the version/format of the UUID.

Not the answer you’re looking for? Browse other questions tagged linux guid or ask your own question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.10.8.40416

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

Create unique random numbers (UUIDs) in bash

I want to create random unique numbers (UUIDs) as the following

First I tried this

What is the right way to create the following (where f is any number)?

2 Answers 2

On Linux, the util-linux / util-linux-ng package offers a command to generate UUIDs: uuidgen .

The uuidgen program creates (and prints) a new universally unique identifier (UUID) using the libuuid (3) library. The new UUID can reasonably be considered unique among all UUIDs created on the local system, and among UUIDs created on other systems in the past and in the future.

There are two types of UUIDs which uuidgen can generate: time-based UUIDs and random-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 two UUID types by using the -r or -t options.

Addendum: The OP had provided a link in the comments to the documentation for Presto DB. After a bit of searching, I found this related discussion where it is explicitly mentioned that the node.id property is indeed a UUID.

Читайте также:  Установка windows 10 msi a320m

Adding the information provided by frostschutz in a comment:

As an alternative to the uuidgen / libuuid approach, you can make use of an interface exposed by the Linux kernel itself to generate UUIDs:

Источник

Generating random UUIDs in Linux

I am stuck in a strange predicament. I need to generate UUIDs in my Linux program (which I distribute using RPMs). I do not want to add another dependency to my application by requiring the user to install libuuid (seems like libuuid isn’t included in most Linux distros, like CentOS).

Isn’t there a standard Linux system call which generates UUIDs (like say, in Windows there CoCreateGuid)? What does the command uuidgen use?

8 Answers 8

Am I missing something? Can’t you:

Thanks for all your comments!

I went through each one, and here’s what suited my requirement the best:

What I needed was just plain time-based UUIDs which were generated from random numbers once for every user who installed the application. UUID version 4 as specified in RFC 4122 was exactly it. I went through a the algorithm suggested, and came up with a pretty simple solution which would work in Linux as well as Windows (Maybe its too simplistic, but it does satisfy the need!):

A good way I found (for linux dev) is to #include . Then you have a few functions you can call:

Is there any reason why you can’t just link statically to libuuid?

No system call exists in POSIX to generate UUID, but I guess you can find somewhere a BSD/MIT code to generate the UUID. ooid is released under the Boost software license, which according to wikipedia, is a permissive license in the style of BSD/MIT. Then you can just paste it into your application, without any need to add dependencies.

uuidgen on my ubuntu linux dist works just fine.

I can’t post comments on answers yet, but I’d just like to point out that the accepted answer’s use of rand() won’t work well at all on Windows, where RAND_MAX is defined as 0x7fff, giving just 15 bits of randomness. On Linux it’s higher, but looks like it’s 0x7fffffff, giving 31 bits of randomness, still missing 1 bit to full 32 bit length.

The safest way to use rand() would be to rely on the fact that the lowest guaranteed RAND_MAX value anywhere is going to be 0x7fff — call it multiple times, shifting left by 15 bits and OR’ing with a new value, until the required amount of random bits is produced.

Also, the format string in sprintf should have width specified, so leading zeros are taken into account (the widths are based on the mentioned bits in the respective argument comment):

Furthermore — the code produces 256 bit values, rather than 128 bit values compatible with RFC 4122 UUIDs.

And final comment:

due to the modulo operator, the actual range produced is [0x8000, 0xbffe]. The % should have been & (or 0x3fff should have been 0x4000 if using % , but division is always a more expensive operation than bitwise AND).

Источник

Оцените статью