Why Not?

Generate a random UUID in Linux

There are many ways to generate an Universally Unique Identifier or UUID in Linux. Not long after I wrote my post about my discoveries on DRBD with Corosync and iSCSI, I discovered that I have to define SCSI serial numbers for each of my LUNs.

I decided to have them randomly generated in order to make sure they’re unique. The easiest way is to generate a random UUID. In Linux, you have usually three ways of doing so.

The kernel random way

I chose to use the generator provided by the Linux kernel by using the /proc/sys/kernel/random/uuid file. The contents of the file are a simple UUID :

$ cat /proc/sys/kernel/random/uuid

The uuidgen way

Another method of generating UUIDs is to use the uuidgen  utility, provided by default in most distributions. This utility is able to generate either random identifiers, or use time as a basis. It can be used as follows :

# Generate a totally random UUID
$ uuidgen

# Generate a time-based UUID
$ uuidgen -t