RDP Monster

如何通过命令行查找操作系统版本:Windows、Mac、Linux

如何通过命令行查找操作系统版本:Windows、Mac、Linux

引言

确定操作系统的版本对于系统管理、软件兼容性验证以及故障排查都至关重要。图形界面虽然也能提供这些信息,但命令行的优势更明显:可远程访问、可脚本化、信息更详细。无论你管理的是一台机器还是几百台服务器,能够通过命令行获取操作系统版本都是必备技能。

本完整指南讲解了如何用命令行识别 Windows、Linux 和 macOS 上的操作系统版本。你将学到每个平台特定的命令、如何解读其输出,以及如何把这些工具用于系统管理与自动化。

 

什么是操作系统版本信息?

操作系统版本数据的组成

Major 版本: Indicates significant release (Windows 10, Ubuntu 20.04)

Minor 版本: Indicates incremental improvements

构建号: Identifies specific compilation of OS

Kernel 版本: Underlying core (particularly relevant for Linux)

Service Pack Level: Maintenance update level (Windows)

Architecture: 32-bit or 64-bit

了解这些组件可实现准确的系统识别与故障排查。

 

如何查询 Windows 操作系统版本

使用 cmd 命令

systeminfo 命令

systeminfo


输出包括:

  • 操作系统名称(Windows 10 Pro、Windows Server 2019)

  • 系统制造商

  • 操作系统版本(版本号)

  • 操作系统构建(构建号)

  • 系统启动时间

  • 网络配置

  • Windows Update 信息


过滤后的输出(特定信息)

systeminfo | find "OS Name"
systeminfo | find "OS 版本"
systeminfo | find "系统启动时间"


使用 PowerShell 命令

Get-ComputerInfo
Get-ComputerInfo | Select-Object OSName, OS版本, OsBuildNumber

Windows Management Instrumentation (WMI)
Get-WmiObject -Class Win32_OperatingSystem | Select-Object Caption, 版本, BuildNumber

Windows Registry Query
Get-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\Windows NT\当前版本' | Select-Object 当前版本, ReleaseId, BuildLabEx

如何解读 Windows 版本输出

版本 String Breakdown:

  • 21H2 版 = Windows 11(发行 21H2)

  • 20H2 版 = Windows 10(发行 20H2)

  • Build 19045 = 最新的 Windows 10

  • Build 22000 = 初版 Windows 11

OS Names Identification:
Windows 10 Home
Windows 10 Pro
Windows 10 Enterprise
Windows 11 Home
Windows 11 Pro
Windows Server 2016
Windows Server 2019
Windows Server 2022

Windows 版本历史速查

版本 发行名称 构建范围
Windows 11 当前 22000+
Windows 10 最新 (21H2) 19044+
Windows Server 2022 当前 20348+
Windows Server 2019 主流支持 17763+
Windows Server 2016 扩展支持 14393+

 

如何查询 Linux 操作系统版本

使用 /etc/os-release

Viewing File Content
cat /etc/os-release

Output Example (Ubuntu):
NAME="Ubuntu"
VERSION="20.04.3 LTS"
ID=ubuntu
ID_LIKE=debian
VERSION_ID=20.04
HOME_URL="https://www.ubuntu.com/"

Filtering Specific Information
grep "PRETTY_NAME" /etc/os-release

发行版特定命令

Ubuntu/Debian
lsb_release -a # Complete information lsb_release -ds # Description

Red Hat/CentOS
cat /etc/redhat-release

Fedora
cat /etc/fedora-release

SUSE
cat /etc/SuSE-release

Kernel 版本 Information

uname -a # All information
uname -r # Kernel release
uname -s # Kernel name
uname -m # Hardware platform
uname -p # Processor type

 

Output Interpretation
Linux hostname 5.15.0-56-generic #62-Ubuntu SMP Fri Oct 7 10:50:38 UTC 2022 x86_64 GNU/Linux
| | | | | |
OS Host Kernel 版本 Build/Date Arch Platform

完整系统信息

hostnamectl # Hostname and OS information
 

如何查询 macOS 版本

使用 system_profiler

system_profiler SPSoftwareDataType

输出包括:

  • 系统软件概览

  • OS 版本

  • Kernel 版本

  • 构建号

  • 计算机名

使用 sw_vers 命令

sw_vers # Complete version information
sw_vers -productName
sw_vers -product版本
sw_vers -build版本

使用 uname 命令

uname -a # Kernel and system information

macOS 版本 Mapping

版本 代号 发布年份
13 Ventura 2022
12 Monterey 2021
11 Big Sur 2020
10.15 Catalina 2019
10.14 Mojave 2018

 

进阶版本检测

检测架构(32 位与 64 位)

Windows

powershell
[Environment]::Is64BitOperatingSystem
 

Linux

bash
getconf LONG_BIT
 

macOS

bash
arch
 

CPU 与硬件信息

Windows

wmic cpu get name, manufacturer
 

Linux

bash
cat /proc/cpuinfo
lscpu
 

macOS

bash
sysctl -n hw.model
 

检查系统运行时间

Windows

powershell
(Get-Date) - (Get-CimInstance Win32_OperatingSystem).LastBootUpTime
 

Linux

bash
uptime

 

macOS

bash
uptime
 

通过脚本检测操作系统

跨平台检测脚本 (Bash)

#!/bin/bash
echo "Operating System Information"
echo "============================"
 
# Detect OS
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
 echo "Operating System: Linux"
 if [ -f /etc/os-release ]; then
  . /etc/os-release
  echo "Distribution: $PRETTY_NAME"
fi
 echo "Kernel: $(uname -r)"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo "Operating System: macOS"
 echo "版本: $(sw_vers -product版本)"
echo "Build: $(sw_vers -build版本)"
else
  echo "Unknown OS: $OSTYPE"
fi
 
echo "Architecture: $(uname -m)"
echo "Hostname: $(hostname)"
 

跨服务器 PowerShell 检测

Get-ComputerInfo -Computer "server1", "server2" | `
  Select-Object PSComputerName, OSName, OsBuildNumber, Os版本


Batch 文件操作系统检测 (Windows)

@echo off
for /f "tokens=3" %%i in ('reg query "HKLM\SOFTWARE\Microsoft\Windows NT\当前版本" ^| find "BuildLabEx"') do set BUILD=%%i echo Windows Build: %BUILD%
 
for /f "delims=" %%i in ('wmic os get caption ^| find "Windows"') do set
OSNAME=%%i
echo OS Name: %OSNAME%
 
 

在程序中比较版本

Bash 版本 Comparison

#!/bin/bash
 
VERSION1="20.04"
VERSION2="22.04"
 
# Convert versions to comparable numbers
ver1=$(echo "$VERSION1" | tr '.' ' ' | awk '{print $1*100+$2}')
ver2=$(echo "$VERSION2" | tr '.' ' ' | awk '{print $1*100+$2}')
 
if [ $ver1 -lt $ver2 ]; then
 echo "$VERSION1 is older than $VERSION2"
elif [ $ver1 -gt $ver2 ]; then
 echo "$VERSION1 is newer than $VERSION2"
else
 echo "版本s are identical"
fi

 

PowerShell 版本 Comparison

$version1 = [version]"10.0.19045"
$version2 = [version]"10.0.22000"

if ($version1 -lt $version2) {
  Write-Host "$version1 is older"
} else {
  Write-Host "$version1 is newer or equal"
}
 
 

系统管理员为什么要查询操作系统信息

定期查询操作系统版本对于以下方面至关重要:

安全补丁:识别需要安全更新的系统

兼容性验证:确保应用程序符合操作系统要求

资产清单管理:跨基础设施跟踪操作系统部署

许可证合规:验证已部署版本的相应许可

故障排除:识别特定于操作系统的问题

自动化:在部署脚本中查询版本信息

 

远程基础设施管理

Professional infrastructure management frequently requires querying OS versions across multiple systems. RDP.Monster enables efficient OS interrogation across distributed infrastructure:

远程操作系统检测功能

  • 用于远程 Linux/Unix 查询的 SSH 访问

  • 用于 Windows 服务器查询的 PowerShell Remoting

  • 同时跨多个系统的批量查询

  • 关于操作系统版本和合规性的自动化报告

基础设施监控集成

  • 实时操作系统版本跟踪

  • 关于过时系统的自动警报

  • 整个基础设施的合规性报告

  • 与自动化工作流程的集成

通过 RDP.Monster VPS 解决方案查询和管理整个基础设施中的操作系统信息

强大的 Linux VPS 主机

通过我们的 Linux VPS 享受完整控制和极致性能。非常适合托管应用、管理服务器和优化工作流程。

Dedicated Servers

高性能独立服务器

需要最高级别的控制和性能?我们的独立服务器为高负载任务带来无与伦比的性能。

常见问题

为什么操作系统版本会影响软件兼容性?

不同版本的操作系统功能集、安全模型和 API 兼容性都不一样。

针对某个操作系统版本编译的软件,未必能在差异较大的其他版本上正常运行。

我应该多久检查一次操作系统版本?

至少在每次安全更新周期检查一次;在自动化基础设施中应持续监控。

操作系统版本和构建号有什么区别?

操作系统版本表示主/次版本,构建号则标识特定的编译日期以及已应用的补丁。

不用管理员权限能查到操作系统版本吗?

可以。大多数查看操作系统版本的命令都可以用普通用户权限运行。

不过有些详细的硬件信息仍然需要提升权限才能读取。

怎么让操作系统版本保持最新?

开启自动更新;定期查看补丁版本;在投入生产之前先在非生产环境中测试更新。

有没有一条在所有操作系统上都通用的命令?

uname -a 在类 Unix 系统(Linux、macOS)上有效。Windows 需要专用命令(systeminfo 或 PowerShell)。

在远程主机上查看操作系统版本的最快方式是什么?

SSH 命令:Linux 用 ssh user@host "cat /etc/os-release";Windows 用 PowerShell 远程会话。

新版本一发布就要立刻升级吗?

不必。先在非生产环境中测试,把当前版本沿用到接近退役日期时再升级。

Register to our reseller program

Your informations

If you have any question, contact us by clicking here !
Name(Required)
Enter your email address, you must have an account on manager.rdp.monster !

Your company

Enter your website address if you have one
Quickly explain how you're going to sell services to your customers. For example, talk to people on forums.

我们使用 cookie !

我们使用 cookie 来改善您的浏览体验、提供个性化的广告或内容,并分析我们的流量。点击「接受」即表示您同意我们使用 cookie。