Linux下如何在没有root权限的情况下获取CPU序列号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5045450/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:54:43  来源:igfitidea点击:

How to get CPU serial under Linux without root permissions

linuxcpuserial-number

提问by Alexey

How can I get CPU serial number under Linux (Ubuntu) without root permissions?

如何在没有root权限的Linux(Ubuntu)下获取CPU序列号?

I tried cpuid command, it works without root permissions, but appears to return all zeros (I believe because something needs to be changed in BIOS).

我尝试了 cpuid 命令,它在没有 root 权限的情况下工作,但似乎返回全零(我相信是因为需要在 BIOS 中更改某些内容)。

Can you please suggest me another way to retrieve CPU serial from a program without root permissions and without having to modify BIOS?

您能否建议我另一种无需root权限且无需修改BIOS即可从程序中检索CPU序列号的方法?

回答by Foo Bah

Have you checked dmesg? Its in /bin

你检查了dmesg吗?在里面/bin

回答by Marc B

Processor serial numbers were basically only in Pentium III processors. Intel removed it from later models due to the privacy concerns that were raised. As such, unless you're on a PIII ANDyour BIOS settings let you read the serial number, all you'll get are 0's.

处理器序列号基本上只出现在奔腾 III 处理器中。由于提出的隐私问题,英特尔将其从后来的模型中删除。因此,除非您使用的是 PIII并且您的 BIOS 设置允许您读取序列号,否则您将得到的只是 0。

回答by CanSpice

cpuidreturns the same serial number for me regardless of my use of sudo:

cpuid无论我使用什么,都会为我返回相同的序列号sudo

 % cpuid | grep serial
Processor serial: 0002-0652-0000-0000-0000-0000
 % sudo cpuid | grep serial
Processor serial: 0002-0652-0000-0000-0000-0000

Unless there's some other serial number that you're referring to...?

除非你指的是其他一些序列号......?

回答by Quinn Bailey

As suggested when this question was asked before, if you are trying to use this for licensing (since you used the licensing tag) you may want to try the MAC address: CPU serial number

正如之前问这个问题时所建议的那样,如果您尝试将其用于许可(因为您使用了许可标签),您可能需要尝试 MAC 地址: CPU 序列号

回答by Kaz

Tie the license to the inode numbers that its executable files get when they are installed into the user's filesystem. If they are moved somewhere else, they will change.

将许可证与其可执行文件安装到用户文件系统时获得的 inode 编号联系起来。如果它们被移动到其他地方,它们就会改变。

The downside is that the numbers may not be preserved if the program has to be restored from a backup.

缺点是如果必须从备份中恢复程序,则可能无法保留数字。

I've done this sort of thing before. You have to be very generous about letting genuine users activate the license on changing hardware.

我以前做过这样的事情。您必须非常慷慨地让真正的用户在更改硬件时激活许可证。

回答by ETech

Root permissions required. The answer is dmidecode.
If you need CPU ID:

需要root权限。答案是 dmidecode。
如果您需要 CPU ID:

dmidecode | grep -w ID | sed "s/^.ID\: //g"

This will get CPU ID, remove 'ID: ' from output
If you need to receive a computer ID:

这将获得 CPU ID,从输出中删除 'ID:'
如果您需要接收计算机 ID:

dmidecode | grep -w UUID | sed "s/^.UUID\: //g"

If you wish to get kernel uuid without root permissions, then:

如果您希望在没有 root 权限的情况下获得内核 uuid,则:

dmesg | grep UUID | grep "Kernel" | sed "s/.*UUID=//g" | sed "s/\ ro\ quiet.*//g"

回答by delverdl

CPUs has no serial number; maybe that you want DMI basic info without root privilege (This will only show you a persistent id of your motherboard manufacturer and model, but no serial number):

CPU 没有序列号;也许您想要没有 root 权限的 DMI 基本信息(这只会显示您的主板制造商和型号的持久 ID,但没有序列号):

dmesg | grep -i dmi: | cut -d ":" -f 2-

Otherwise you could "tell" dmidecodeto run from unprivileged user:

否则,您可以“告诉” dmidecode从非特权用户运行:

sudo chmod +s /usr/sbin/dmidecode

Then you could run for instance:

然后你可以运行例如:

dmidecode -s system-serial-number

In most cases "system-serial-number" is like either "chassis-serial-number" or "baseboard-serial-number". Remember that not all distros have this program installed, for instance, Debian based systems have a package named after it.

在大多数情况下,“系统序列号”类似于“机箱序列号”或“基板序列号”。请记住,并非所有发行版都安装了这个程序,例如,基于 Debian 的系统有一个以它命名的包。

Otherwise you can find a unique and persistent, thro' installs, system ID via your system's disk; to do that you may run the following:

否则,您可以通过系统磁盘找到唯一且持久的系统 ID;为此,您可以运行以下命令:

mount | grep "on / type" | awk '{print }'

The former will give you device's path where your system is mounted (for my OS it returned /dev/sda7), and then you can find an ID for it with the following:

前者将为您提供安装系统的设备路径(对于我的操作系统,它返回/dev/sda7),然后您可以使用以下内容找到它的 ID:

find /dev/disk/by-id/ -lname "*sda" ! -name "wwn*"

So the complete command to find a unique ID from your system's hard disk could be:

因此,从系统硬盘中查找唯一 ID 的完整命令可能是:

find /dev/disk/by-id/ -lname "*`mount | grep " / " | awk '{print }' | cut -b 6-8`" ! -name "wwn*" -printf "%f\n"

I hope this may fit your needs or someone else's in here. Command cut -b 6-8may not be portable, because I'm assuming block devices names to be three chars long; moreover, /dev/disk/by-id/path is only filled by UDEV managed systems and not all Linux distros use it, but I ensure you the former will work in Ubuntu.

我希望这可能适合您或其他人的需求。命令cut -b 6-8可能不可移植,因为我假设块设备名称为三个字符长;此外,/dev/disk/by-id/路径仅由 UDEV 管理系统填充,并非所有 Linux 发行版都使用它,但我保证前者将在 Ubuntu 中工作。