Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate system-wide unique IDs under Linux

Tags:

linux

unique

I'm working on a multiprocess Linux system and need to generate unique IDs. Security is not a consideration, so an ID generator that starts at zero and counts up would be fine. Also it's just within a local machine, no network involved. Obviously it's not hard to implement this, but I was just wondering if there was anything already provided (preferably lightweight).

like image 780
gimmeamilk Avatar asked Mar 07 '12 18:03

gimmeamilk


3 Answers

This sounds like a job for... ...uuidgen:

% uuidgen 
975DA04B-9A5A-4816-8780-C051E37D1414

If you want to build it into your own application or service, you'll need libuuid:

#include <uuid/uuid.h>
#include <iostream>

int main()
{
    uuid_t uu;
    uuid_generate(uu);
    char uuid[37];
    uuid_unparse(uu, uuid);
    std::cout << uuid << std::endl;
}
like image 70
Johnsyweb Avatar answered Nov 11 '22 14:11

Johnsyweb


There is a command line tool called uuid that will do exactly what you want. I'm not sure if it gets installed by default in various distributions though, so you may have to do that yourself.

like image 25
Alex Howansky Avatar answered Nov 11 '22 14:11

Alex Howansky


In cases where uuidgen is not installed you can use mktemp. For example, for 16 characters (should be enough to achieve system-wide unique IDs) ...

mktemp -u XXXXXXXXXXXXXXXX

like image 1
fredk Avatar answered Nov 11 '22 13:11

fredk