How to Generate UUIDs in Linux

UUIDs (or GUIDs as they’re called in Windows parlance) are 128-bit codes that are used as unique identifiers for objects and resources. The acronym stands for Universally Unique IDentifier (or Globally Unique IDentifier in Windows). The purpose of UUIDs is to provide a system of identification where there is some assurance that the ID you’re using for a particular object is unique. This assurance is made through the extreme unlikelihood of generating the same UUID twice. In this tutorial I will explain how to generate UUIDs in the Unix/Linux environment, either from the command line, or in C programs.

The way you generate a UUID from the command line is very simple. Just use the following command:


$ uuidgen
bbb42605-ae65-4355-8027-4eb3c248e91f

As you can see, a 128-bit number (32 hex-digits) has been generated in the standard 12-4-4-4-8 format used by UUIDs.

Next we’re going to write a C program that implements this same command. We’re going to need two things for our program: the uuid_t data type (which is basically just an array of 16 chars) and the function uuid_generate(). These are both declared in the header file uuid/uuid.h.


 1 #include <stdio.h>
 2 #include <uuid/uuid.h>
 3 
 4 #define print_digits( start, end )\
 5 forint i = start; i < end; i++ ){\
 6         printf"%s%x", (uuid[i] < 0x10)?"0":"", uuid[i] );\
 7 }\
 8 if( end < 16 ) putchar'-' );\
 9 else putchar'\n' )
10 
11 int mainint argc, char **argv ){
12         uuid_t uuid;
13         uuid_generate( uuid );
14         print_digits06 );
15         print_digits68 );
16         print_digits810 );
17         print_digits1012 );
18         print_digits1216 );
19         return 0;
20 }

Since the UUID functions are not part of libc, but rather part of libuuid, we will need to link in libuuid when we run gcc. The command to compile this program looks like this:


$ gcc -o uuid uuid.c -luuid

Now to test this program:


$ ./uuid
3a87fe9793c9-4ebd-95d4-4ce2-a80c054b

As we can see, our UUID program works as intended.

For most applications this is all you really need to know. You can probably stop reading here and do perfectly fine. But I want to explore UUIDs a bit further and talk about how they’re actually generated in libuuid. There are basically two ways of generating a UUID: one is with a timestamp and the other is through random number generation. In the latter case libuuid uses the file /dev/urandom to generate a random number.

The function uuid_generate() is actually a frontend for two more primitive functions: uuid_generate_random() and uuid_generate_time(). Only one of these functions is used. The default in most modern implementations is uuid_generate_random(). If /dev/urandom is not available, the frontend function falls back on uuid_generate_time(). As you may have guessed, uuid_generate_random() generates the UUID based on a value pulled from /dev/urandom and uuid_generate_time() generates a UUID based on the current timestamp. Both of these result in a low likelihood of collisions and are thus ideal for generating UUIDs. There is also a fourth function – uuid_generate_time_safe(), which returns an integer value to indicate whether the UUID was generated in a safe manner (using certain synchronization mechanisms provided by the operating system).

The prototypes for all of these functions as taken from uuid/uuid.h are as follows:


void uuid_generate(uuid_t out);
void uuid_generate_random(uuid_t out);
void uuid_generate_time(uuid_t out);
int uuid_generate_time_safe(uuid_t out);

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s