Getting a unique id from a unix-like system

前端 未结 15 2187
不知归路
不知归路 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:06

    You mentioned that on Windows you use some GUID... Do you have some details about how it is created?

    Apart from that, you could try something like CPU ID, or hard disk ID... I suppose those cannot be changed (but you will run into trouble if a faulty hard disk is replaced).

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

    Another option is to use information derived from dmidecode, a command present on linux. This information is decoded from /dev/mem, therefore requiring root access.

    The information dmidecode reads is known to be flawed, as some motherboard manufacturers lie or fake some of the fields.

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

    There is no general and reliable way to get what you want.

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

    Most unix-like machines have a random number generator accessible through /dev/random. You will need something like a MAC address and a time to give a genuine uniqueness to the GUID generator (this is what the GUID generator on Windows does). On top of this, getting something from /dev/random will get you a reasonably good GUID type construct. In practice, the UUID libraries do this sort of thing behind the scenes.

    If you just need one number per machine, than a MAC address will probably be sufficient. These are administered by a central body and one can reasonably assume that no two MAC addresses will be the same. However, if you are trying to use this to tie a software installation to a MAC address be aware that some components have programmable MAC addresses or programmable components of the MAC address. Unix-like Operating systems, particularly open-source ones tend not to have hard-wired serial numbers. This approach may also cause issues with running multiple instances of the software in VM's.

    One option might be a USB dongle, which can be obtained from several manufacturers. Another option might be a license server, where the unique code is supplied to the server. Again, several canned solutions for this are available from different sources.

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

    You can use a lockfile in places like:

    • /var/run/yourapp.pid (if program run by root)
    • $HOME/.yourapp.pid (if run by user and local filesystem)
    • $HOME/.yourapp.$(hostname -f).pid (home on nfs)

    When your program is run, it shall do something like:

    lock = open(filename, O_CREAT | O_EXCL);
    dprintf(lock, "%u", getpid());
    

    If the open fails, check if the process is still running and if not: delete the file and try again.

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

    Both Solaris and Linux provide the hostid(1) utility

    0 讨论(0)
提交回复
热议问题