Getting a unique id from a unix-like system

前端 未结 15 2188
不知归路
不知归路 2020-11-27 18:22

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 wa

相关标签:
15条回答
  • 2020-11-27 19:13

    Sounds like you are looking for UUID. This is a common universally unique id (really, the same thing as a GUID)

    There are many C++ implementations of this in diffrent libs, or you could use the uuidgen command and capture the output.

    0 讨论(0)
  • 2020-11-27 19:15

    You have to consider that a lot of setups may have created a filesystem image and cloned to many machines, rather than setting them up individually. In other cases, a machine could get re-setup many times. In other words, anything the OS provided can't be trusted.

    However, the CPU does keep a unique serial number, but access to it should be different on different systems.

    0 讨论(0)
  • 2020-11-27 19:17

    The best way is, as usual, to see how other people already solved the same problem.

    FLEXlm also uses a host identifier for its node-locked licenses. The most common host identifier it uses is the ethernet MAC address for one of your network interfaces, smashed together without any separator.

    It can also use (on Windows) the volume serial number of the C: drive (again smashed together without any separators), and on Solaris the output of the hostid command (IIRC, on Sun computers, this number is actually unique, and located on a small removable EEPROM on the system board).

    While the MAC address is extremly easy to fake, it is a nearly universal identifer nowadays (almost all new computers have at least one ethernet port, and it is very common for them to be onboard), and actually intended to be globally unique (in fact, the Ethernet protocols depend on this uniqueness). The main problems you would have with this approach:

    • Some computers have several ethernet addresses; some of them are on the main board, some are on separate removable cards.
    • They are extremly easy to fake (and some protocols depend on being able to change them).
    • Some virtualized environment generate random ethernet addresses on each boot (but they usually have a way to force a fixed value).
    0 讨论(0)
提交回复
热议问题