Getting a unique id from a unix-like system

前端 未结 15 2186
不知归路
不知归路 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 18:55

    The answers from Jason Day and A.Danischewski seem to be on the right track but don't meet your criteria of any "Unix-like system" since /sbin/blkid and /etc/fstab don't exist on OSX.

    The only 100% portable approach would be to choose a standard location for a file that your own application will create, e.g. /etc/YOURAPP.cfg and store a UUID there if one doesn't already exist.

    Far from ideal, since another person or application could delete the file or change it, or if the user changed out the root file system you could lose the ID from the current machine, or it could come into being on another machine. Not to mention issues with read and write permissions and so on.

    But in the end there is no such thing as "the same machine". Any computer is no more and no less than its components + current configuration. I don't think you can do any better than this, portably.

    0 讨论(0)
  • 2020-11-27 18:55

    As someone else stated, the dmidecode command is an option.

    [root@sri-0000-0003 WebGui]# dmidecode -s system-uuid
    03001234-1234-1234-1234-000700012345
    

    I edited the output to hide the UUID of the system I tested on.

    There are other things that you can get from dmidecode. dmidecode -t will tell you the categories.

    [root@sri-0000-0003 WebGui]# dmidecode -t
    dmidecode: option requires an argument -- 't'
    Type number or keyword expected
    Valid type keywords are:
      bios
      system
      baseboard
      chassis
      processor
      memory
      cache
      connect
    

    If you're using actual hardware instead of a virtual machine then dmidecode -t processor would be a good option.

    [root@sri-0000-0003 WebGui]# dmidecode -t processor
    # dmidecode 3.1
    Getting SMBIOS data from sysfs.
    SMBIOS 3.0.0 present.
    
    Handle 0x0041, DMI type 4, 48 bytes
    Processor Information
            Socket Designation: U3E1
            Type: Central Processor
            Family: Core i3
            Manufacturer: Intel(R) Corporation
            ID: E3 00 00 00 11 22 33 44
    

    Given that the number of processor manufactures is small, this seems like a good alternate to dmidecode -s system-uuid. However, under virtualbox, dmidecode -t processor won't give you anything useful. I don't know about any other virtual platforms.

    I'm willing to bet that dmidecode -s system-uuid will also work inside a docker container too but, I can't verify that.

    0 讨论(0)
  • 2020-11-27 18:56

    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.

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

    You don't mention how stable the unique identifier needs to be -- do you always want the same host to produce the same ID each time your code is run?

    If no, then fuzzymonk's suggestion of uuidgen is what you want.

    If yes, then you need to decide what constitutes "same" as far as the host as concerned. One way would be as you suggest, the MD5 sum of the MAC of the first ethernet interface and "something". For "something" in that case I would consider the FQDN, unless your notion of "same host" includes the FQDN changing...

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

    I don't think it's possible. The closest you can get is to create a very long random string (like MS do with GUIDs) and store it somewhere on your system.

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

    You can get the UUID of the root filesystem /, that is fairly reliable, yet it won't differentiate between chroots and possibly vms running on the same disk.

    If you are mainly dealing with internal or static HDD's that are dedicated to running a particular OS then you should be able to use the UUID of the root filesystem to detect the system.

    You can get the UUID of the root fs with something like this: alias sys_guid='sudo /sbin/blkid | grep "$(df -h / | sed -n 2p | cut -d" " -f1):" | grep -o "UUID=\"[^\"]*\" " | sed "s/UUID=\"//;s/\"//"'

    If you need to further differentiate between kernel versions of the same OS, or different OS's running on the same disk you can use data from uname and/or combine them with the root fs UUID.

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