25

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

node.id=ffffffff-ffff-ffff-ffff-ffffffffffff

First I tried this

$ rndnum=` echo $RANDOM"-"echo $RANDOM"-"echo $RANDOM"-"echo $RANDOM"-"echo $RANDOM`

$ echo $rndnum
30380-echo 21875-echo 14791-echo 32193-echo 11503

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

ffffffff-ffff-ffff-ffff-ffffffffffff
Jeff Schaller
  • 66,199
  • 35
  • 114
  • 250
yael
  • 12,598
  • 51
  • 169
  • 303
  • 5
    Random numbers are not necessarily unique. – JdeBP Feb 14 '19 at 09:39
  • yes , seems , this number is from presto configuration ( http://prestodb.github.io/docs/current/installation/deployment.html ) – yael Feb 14 '19 at 09:41
  • 11
    Note that UUIDs are not just random numbers stringed together. – Kusalananda Feb 14 '19 at 09:44
  • 9
    I've spent too many hours debugging scripts which assume "unlikely" is the same as "won't happen". If this is supposed to be a UUID then please do use an actual UUID, there are strict constraints on the way they are generated. If you could elaborate the reason you need this we might be able to help more. – Philip Couling Feb 14 '19 at 09:51

4 Answers4

61

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

$ uuidgen
5528f550-6559-4d61-9054-efb5a16a4de0

To quote the manual:

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.


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:

$ cat /proc/sys/kernel/random/uuid
00db2531-365c-415c-86f7-503a35fafa58

The UUID is re-generated on each request.

Haxiel
  • 8,201
  • 1
  • 20
  • 30
4

As @frostschutz mentioned, you can use a dependency-free solution:

cat /proc/sys/kernel/random/uuid

or save variable

UUID=$(cat /proc/sys/kernel/random/uuid)

In Bash you could simplify to:

UUID=$(< /proc/sys/kernel/random/uuid)
insign
  • 158
  • 6
1

On a raspberry PI, and probably others, the command is simply:

uuid

It should be preinstalled, but if not, this should do it: sudo apt install uuid

Greg oooo
  • 111
  • 3
  • 1
    I think you perhaps mean Raspberry PI OS (AKA Raspbian). Remember that a Raspberry Pi can run many different operating systems. – Philip Couling Dec 18 '20 at 14:02
-1

Below is bash script to generate UUID version 4 according to RFC 4122.

UUID version 4 means it is based on Truly Random or Pseudo-Random Numbers.

#! /bin/bash

printf "%04x%04x-%04x-%04x-%04x-%04x%04x%04x\n" \
$RANDOM $RANDOM \
$RANDOM \
$(($RANDOM & 0x0fff | 0x4000)) \
$(($RANDOM & 0x3fff | 0x8000)) \
$RANDOM $RANDOM $RANDOM

$(($RANDOM & 0x0fff | 0x4000)) - replaces the left four random bits with 0100 as defined by parts 4.4 and 4.1.3 of RFC.

$(($RANDOM & 0x3fff | 0x8000)) - replaces the left two random bits with 10 as defined by part 4.4 of RFC.

Pavel_H
  • 107
  • 2
  • I have to assume you did not read any of the answers where it is documented that a UUID is not a set of random numbers strung together and that give pointers to interfaces specifically designed to generate unique UUIDs. – doneal24 Jul 25 '22 at 15:50
  • You are right. I adjusted script to fix problem. – Pavel_H Jul 25 '22 at 18:58
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 31 '22 at 08:08
  • @mashuptwice I have checked once more and don't see any mistakes. I also added bit operations explanations. Furthermore I looked into source code of libuuid and they use identical operations: https://github.com/util-linux/util-linux/blob/3cfba7d39b66eff4307218fefd8bb34bb1621f83/libuuid/src/gen_uuid.c#L608 If you still see a problem please say what exactly is wrong. – Pavel_H Jul 31 '22 at 14:52