在 Linux 上获取机器 ID 的最佳方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10152762/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to get machine id on Linux?
提问by
What is the best-practiced way to get an unique machine ID in GNU/Linux
for i386
architecture?
什么是最好的实践方式得到一个唯一的机器IDGNU/Linux
的i386
架构?
Are there any good ways except the mac
address?
除了mac
地址还有什么好办法吗?
采纳答案by c00kiemon5ter
Depending on your kernel, the DMI information may be available via sysfs. Try those:
根据您的内核,DMI 信息可以通过 sysfs 获得。试试那些:
# cat /sys/class/dmi/id/board_serial
xxxxxxxxxxxxxxx
# cat /sys/class/dmi/id/product_uuid
xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
or using a tool
或使用工具
# dmidecode -s baseboard-serial-number
...
# dmidecode -s system-uuid
...
回答by adnan kamili
You can use lshal. This needs hal (apt-get install hal or yum install hal) to be installed first. This way you can access all the info of dmidecode without root permissions.
您可以使用 lshal。这需要先安装 hal(apt-get install hal 或 yum install hal)。这样您就可以在没有 root 权限的情况下访问 dmidecode 的所有信息。
A non-root equivalent of
非根等价于
# dmidecode | grep -i uuid
will be
将会
$ lshal |grep -i system.hardware.uuid
And similarly other info as per your needs.
以及根据您的需要类似的其他信息。
回答by andrelsm
A simple and portable way of computing your own sysid may be to serialize uname(), gethostid() and some inodes like /home or your application homedir (obtained with stat()) etc. in a string and hash It.
计算您自己的 sysid 的一种简单且可移植的方法可能是将 uname()、gethostid() 和一些 inode 像 /home 或您的应用程序 homedir(通过 stat() 获得)等序列化为字符串并散列它。