Windows和Linux中获取设备的唯一标识

Win

计算机UUID

UUID 是在计算机系统中用于识别硬件系统的标识符,与硬件组件(如主板、处理器、内存等)的组合有关,但不代表单独的硬件组件。

wmic csproduct get uuid 命令用于从 Windows Management Instrumentation (WMI) 中查询计算机的唯一标识符(UUID,通用唯一标识符)。

UUID 是一个全球唯一的标识符,用于唯一标识计算机或设备。

具体来说,这个命令获取的是计算机硬件的 UUID,它通常由主板制造商在生产过程中分配。

Powershell

1
(Get-WmiObject -Class Win32_ComputerSystemProduct).UUID

CMD

1
wmic csproduct get uuid

主板UUID

使用CMD:

1
wmic baseboard get serialnumber

使用 PowerShell :

1
Get-WmiObject -Class Win32_BaseBoard | Select-Object -Property SerialNumber

Linux

硬件UUID

BIOS 或固件生成的,因此在更换主板或系统硬件时,UUID 可能会发生变化。

系统重装不会变化。

使用dmidecode工具:

1
sudo dmidecode -s system-uuid

系统UUID

machine-id 通常在系统安装时由系统生成,生成过程依赖于不同的 Linux 发行版和安装程序。

它通常基于某些系统硬件特性,确保唯一性。

可以手动修改。

1
cat /etc/machine-id

Java中获取

推荐获取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package cn.psvmc;


import java.io.BufferedReader;
import java.io.InputStreamReader;

public class Main {
public static void main(String[] args) {
System.out.println(getUniqueID());
}

public static String getUniqueID(){
if (System.getProperty("os.name").startsWith("Windows")) {
// Windows 系统下的唯一ID获取
return getWindowsUniqueID();
} else if (System.getProperty("os.name").startsWith("Linux")) {
// Linux 系统下的唯一ID获取
return getLinuxUniqueID();
} else {
return "";
}
}

private static String getWindowsUniqueID() {
try{
// PowerShell 命令
ProcessBuilder processBuilder = new ProcessBuilder(
"powershell",
"-Command",
"(Get-WmiObject -Class Win32_ComputerSystemProduct).UUID"
);
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();

// 读取命令输出
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
String uuid = null;

while ((line = reader.readLine()) != null) {
uuid = line.trim();
if (!uuid.isEmpty()) {
break;
}
}

reader.close();
process.waitFor();
return uuid != null ? uuid : "";
}catch (Exception e){
return "";
}
}

private static String getLinuxUniqueID() {
// 在 Linux 中,通常通过读取系统文件来获取
String command = "sudo dmidecode -s system-uuid";
try {
Process process = Runtime.getRuntime().exec(command);
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()));
return reader.readLine().trim();
} catch (Exception e) {
return "";
}
}
}

Linux中下面的这种方式也能获取,但是能够修改所以不建议

1
2
3
4
5
6
7
8
9
10
11
private static String getLinuxUniqueID() {
// 在 Linux 中,通常通过读取系统文件来获取
String command = "cat /etc/machine-id";
try {
Process process = Runtime.getRuntime().exec(command);
java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(process.getInputStream()));
return reader.readLine().trim();
} catch (Exception e) {
return "";
}
}

Windows下Java通过这种方式获取不到,但是直接执行命令行获取是可以的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private static String getWindowsUniqueID() {
try {
Process process = Runtime.getRuntime().exec("wmic csproduct get uuid");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line.trim());
if (line.trim().startsWith("UUID")) {
continue; // 跳过标题行
}
return line.trim();
}
} catch (Exception e) {
return "";
}
return "";
}