V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Distributions
Ubuntu
Fedora
CentOS
中文资源站
网易开源镜像站
AmrtaShiva
V2EX  ›  Linux

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

  •  
  •   AmrtaShiva · 2021-01-12 21:59:12 +08:00 via iPhone · 2439 次点击
    这是一个创建于 1171 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    10 条回复    2021-01-13 12:13:03 +08:00
    codehz
        1
    codehz  
       2021-01-12 22:02:17 +08:00 via Android   ❤️ 1
    (现在流行读 mac 地址)
    AmrtaShiva
        2
    AmrtaShiva  
    OP
       2021-01-12 22:10:54 +08:00 via iPhone
    @codehz ....🤣
    zlowly
        3
    zlowly  
       2021-01-13 09:32:43 +08:00
    读取 /sys/class/dmi/id/product_uuid,当然也可以用主板序列号 /sys/class/dmi/id/board_serial,不过都是需要 root 权限。
    ysc3839
        4
    ysc3839  
       2021-01-13 10:06:03 +08:00
    那 Windows 下是如何实现的呢?
    AmrtaShiva
        5
    AmrtaShiva  
    OP
       2021-01-13 10:14:50 +08:00 via iPhone
    @zlowly 就是不想用 root 权限读取
    AmrtaShiva
        6
    AmrtaShiva  
    OP
       2021-01-13 10:18:30 +08:00 via iPhone
    @ysc3839 嘿嘿 网上一抓一大把
    omph
        7
    omph  
       2021-01-13 10:35:38 +08:00
    /etc/machine-id
    需要 systemd
    zlowly
        8
    zlowly  
       2021-01-13 10:47:37 +08:00   ❤️ 1
    那感觉不大可行,ls -al /sys/class/dmi/id/可以看到 product_uuid,board_serial,product_serial,chassis_serial 这几个是特意设置成只有 root 可读的,应该是 Linux 的安全特性。
    要绕过它除非象 lshal 那种方式,另外安装以 root 权限跑的 hald 服务来收集系统信息,这样才能用普通用户读取这些收集后的信息。
    用 /etc/machine-id 的话,有个问题就是这个只是在系统安装后一次性生成的,对于象在云平台基于模板克隆的系统很可能会未做处理出现重复,还真不如 MAC 比较好。
    ysc3839
        9
    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
        10
    AmrtaShiva  
    OP
       2021-01-13 12:13:03 +08:00 via iPhone
    @ysc3839 好的 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1181 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 23:06 · PVG 07:06 · LAX 16:06 · JFK 19:06
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.