如何在Linux下生成系统范围的唯一ID

时间:2023-01-31 20:14:43

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).

我正在研究多进程Linux系统,需要生成唯一的ID。安全性不是一个考虑因素,所以从零开始并计数的ID生成器就可以了。它也只是在本地机器内,不涉及网络。显然,实现这一点并不难,但我只是想知道是否已经提供了任何东西(最好是轻量级的)。

3 个解决方案

#1


16  

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

这听起来像是...... uuidgen的工作:

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

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

如果要将其构建到自己的应用程序或服务中,则需要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;
}

#2


1  

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.

有一个名为uuid的命令行工具可以完全按照你的意愿执行。我不确定它是否默认安装在各种发行版中,所以你可能必须自己做。

#3


0  

Also useful..

也很有用..

 cat /etc/machine-id

The /etc/machine-id file contains the unique machine ID of the local system that is set during installation. The machine ID is a single newline-terminated, hexadecimal, 32-character, lowercase machine ID string. When decoded from hexadecimal, this corresponds with a 16-byte/128-bit string.

/ etc / machine-id文件包含安装期间设置的本地系统的唯一计算机ID。计算机ID是一个以换行符结尾的十六进制,32个字符的小写机器ID字符串。从十六进制解码时,这对应于16字节/ 128位字符串。

#1


16  

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

这听起来像是...... uuidgen的工作:

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

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

如果要将其构建到自己的应用程序或服务中,则需要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;
}

#2


1  

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.

有一个名为uuid的命令行工具可以完全按照你的意愿执行。我不确定它是否默认安装在各种发行版中,所以你可能必须自己做。

#3


0  

Also useful..

也很有用..

 cat /etc/machine-id

The /etc/machine-id file contains the unique machine ID of the local system that is set during installation. The machine ID is a single newline-terminated, hexadecimal, 32-character, lowercase machine ID string. When decoded from hexadecimal, this corresponds with a 16-byte/128-bit string.

/ etc / machine-id文件包含安装期间设置的本地系统的唯一计算机ID。计算机ID是一个以换行符结尾的十六进制,32个字符的小写机器ID字符串。从十六进制解码时,这对应于16字节/ 128位字符串。