Getting a unique id from a unix-like system

前端 未结 15 2402
不知归路
不知归路 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 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.

提交回复
热议问题