从类 Unix 系统获取唯一 ID [英] Getting a unique id from a unix-like system
问题描述
我想从任何类 Unix 系统(如果可能的话)获得一个唯一的 ID,每次我的应用程序在同一台机器上运行时,该 ID 都将保持不变.如果可能,我想从 Linux 或 FreeBSD 或 Solaris 等获取相同的 id ......我不想为每台机器生成一个新的 id,而是获取一个已经存在的 id,我更喜欢这个 id来自操作系统,我不喜欢使用诸如 MAC 地址之类的东西.
I want to get from any Unix-like system (if this is possible) a unique id that will be persistent every time my application runs in the same machine. If it is possible, I want to get the same id from Linux or FreeBSD or Solaris, etc... I don't want to generate a new id for each machine, but get an already existent id, and I prefer this id to come from the operating system and I don't prefer to use something like the MAC address.
如果没有其他选项可用,我可以将 MAC 与其他内容结合使用,例如 id 可以是 MAC 地址与其他内容组合的 md5 哈希值.
If there is no other option available, I can use MAC in combination with something else, for example the id can be the md5 hash of the combination of the MAC address and something else.
我想听听你的建议.
如果有用,我的应用程序是用 C/C++ 编写的.
If it is useful, my application is written in C/C++.
这一切的目的是防止用户运行我的应用程序两次或多次.我只想跑一次.
The aim of all this is to prevent a user to run my application for two or more times. I want to run just once.
推荐答案
根文件系统的 UUID 怎么样?您可以从 /etc/fstab
获取根文件系统设备,方法是手动解析文件或使用 getfsent (3)
或 getfsfile (3)代码>.拥有设备后,您可以通过检查
/dev/disk/by-uuid
中的链接或从 blkid
命令获取 UUID.
How about the UUID of the root filesystem? You can get the root filesystem device from /etc/fstab
, either by manually parsing the file or by using getfsent (3)
or getfsfile (3)
. Once you have the device, you can get the UUID by either checking the links in /dev/disk/by-uuid
or from the blkid
command.
这篇关于从类 Unix 系统获取唯一 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!