Linux 系统如何获取 像 Windows 系统下所谓的机器码?

2021-01-12 21:59:12 +08:00
 AmrtaShiva

如题 知道个 dmidecode |grep -i uuid 然而这没法写到代码里面....而且还需要 root 权限....

2459 次点击
所在节点    Linux
10 条回复
codehz
2021-01-12 22:02:17 +08:00
(现在流行读 mac 地址)
AmrtaShiva
2021-01-12 22:10:54 +08:00
@codehz ....🤣
zlowly
2021-01-13 09:32:43 +08:00
读取 /sys/class/dmi/id/product_uuid,当然也可以用主板序列号 /sys/class/dmi/id/board_serial,不过都是需要 root 权限。
ysc3839
2021-01-13 10:06:03 +08:00
那 Windows 下是如何实现的呢?
AmrtaShiva
2021-01-13 10:14:50 +08:00
@zlowly 就是不想用 root 权限读取
AmrtaShiva
2021-01-13 10:18:30 +08:00
@ysc3839 嘿嘿 网上一抓一大把
omph
2021-01-13 10:35:38 +08:00
/etc/machine-id
需要 systemd
zlowly
2021-01-13 10:47:37 +08:00
那感觉不大可行,ls -al /sys/class/dmi/id/可以看到 product_uuid,board_serial,product_serial,chassis_serial 这几个是特意设置成只有 root 可读的,应该是 Linux 的安全特性。
要绕过它除非象 lshal 那种方式,另外安装以 root 权限跑的 hald 服务来收集系统信息,这样才能用普通用户读取这些收集后的信息。
用 /etc/machine-id 的话,有个问题就是这个只是在系统安装后一次性生成的,对于象在云平台基于模板克隆的系统很可能会未做处理出现重复,还真不如 MAC 比较好。
ysc3839
2021-01-13 10:56:09 +08:00
@AmrtaShiva 随手搜到了一篇文章 https://blog.csdn.net/qq_29542611/article/details/88321102
按照这里面的方法:
1. 猜测对应的是 /sys/class/dmi/id/product_uuid
2. 帮你找到了这个 property 的 description:
The ProcessorId property contains processor-specific information that describes the processor's features. For x86 class CPUs, the field's format depends on the processor's support of the CPUID instruction. If the instruction is supported, the ProcessorId property contains two DWORD-formatted values. The first (offsets 08h-0Bh) is the EAX value returned by a CPUID instruction with input EAX set to 1. The second (offsets 0Ch-0Fh) is the EDX value returned by that instruction. Only the first two bytes of the ProcessorID property are significant (all others are set to 0) and contain (in WORD-format) the contents of the DX register at CPU reset.
3. 猜测对应的是 /sys/class/dmi/id/board_serial
4. https://unix.stackexchange.com/questions/121757/harddisk-serial-number-from-terminal https://unix.stackexchange.com/questions/364456/getting-hard-disk-drive-serial-number-at-the-terminal-not-the-wwn 参考对应工具的源代码
5. 参考 iproute2 的源代码
AmrtaShiva
2021-01-13 12:13:03 +08:00
@ysc3839 好的 谢谢

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/744346

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX