当前位置:首页 > Linux > 正文

怎么快速查看Linux系统版本?

查看 Linux 发行版信息,常用方法:,1. 查看 /etc/os-release 文件: cat /etc/os-release,2. 使用 lsb_release 命令: lsb_release -a,3. 检查发行版特有文件(如 /etc/issue/etc/*-release),4. uname -a 显示内核版本(非发行版名称)。

如何查看 Linux 发行版:7 种专业方法详解

在 Linux 系统管理、软件安装或故障排查时,明确当前发行版(Distribution)至关重要,不同发行版(如 Ubuntu、CentOS、Debian)的包管理工具、配置文件路径和系统命令可能存在差异,以下是经过验证的 7 种查看方法,适用于绝大多数 Linux 环境。


方法 1:通过 /etc/os-release 文件(推荐)

这是最权威的系统信息文件,所有现代 Linux 发行版均支持:

cat /etc/os-release

输出示例

NAME="Ubuntu"
VERSION="22.04.1 LTS (Jammy Jellyfish)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 22.04.1 LTS"
VERSION_ID="22.04"

优势:包含发行版名称、版本号及兼容性信息(如 ID_LIKE=debian)。
️ 注意:部分旧系统可能使用 /etc/*-release(如 /etc/redhat-release)。


方法 2:检查发行版描述文件

直接查看发行版专属的标识文件:

# 适用于基于 Red Hat 的系统
cat /etc/redhat-release
# 适用于 Debian/Ubuntu
cat /etc/debian_version
# 通用查看所有 *-release 文件
ls -l /etc/*release

输出示例(CentOS):

CentOS Linux release 7.9.2009 (Core)

️ 方法 3:使用 lsb_release 命令

专为 LSB(Linux Standard Base)兼容系统设计:

怎么快速查看Linux系统版本?  第1张

lsb_release -a

输出示例

Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy

若未安装,可通过包管理器安装:

  • Debian/Ubuntu:sudo apt install lsb-core
  • Red Hat/CentOS:sudo yum install redhat-lsb-core

方法 4:hostnamectl 命令(Systemd 系统)

适用于使用 Systemd 初始化进程的系统(多数现代发行版):

hostnamectl

输出关键信息

Operating System: Ubuntu 22.04.1 LTS  
Kernel: Linux 5.15.0-76-generic  
Architecture: x86-64

方法 5:通过包管理器查询

包管理器本身会标识发行版:

# APT (Debian/Ubuntu)
apt -v
# DNF/YUM (Fedora/CentOS/RHEL)
dnf --version  # 或 yum --version
# Pacman (Arch Linux)
pacman -V

APT 输出示例

apt 2.4.8 (amd64)   # 隐含 Debian/Ubuntu 环境

🧩 方法 6:查看内核版本(辅助判断)

内核版本虽不直接对应发行版,但可辅助推测:

uname -a

输出示例

Linux myserver 5.15.0-76-generic #83-Ubuntu SMP ... x86_64 GNU/Linux

关键词 Ubuntu 表明运行在 Ubuntu 环境。


方法 7:综合工具 neofetch(可视化展示)

安装图形化信息展示工具:

# 安装
sudo apt install neofetch  # Ubuntu/Debian
sudo dnf install neofetch  # Fedora/CentOS
# 运行
neofetch

输出效果

OS: Ubuntu 22.04.1 LTS x86_64  
Host: VirtualBox 1.2  
Kernel: 5.15.0-76-generic  
Shell: bash 5.1.16  
... + 发行版 Logo 图标

总结与最佳实践

场景 推荐命令
快速获取发行版名称/版本 cat /etc/os-release
兼容旧系统 cat /etc/*-release
需要详细兼容性信息 lsb_release -a
可视化展示 neofetch

关键提示

  1. 优先使用 /etc/os-release——它是 Linux 标准规范(Freedesktop.org 定义)。
  2. 生产环境中避免依赖未预装的工具(如 lsb_release 需额外安装)。
  3. 脚本中建议解析 /etc/os-releaseID 字段,
    source /etc/os-release
    echo "当前系统:$ID $VERSION_ID"

ℹ️ 引用说明:本文方法参考 Linux 官方文档(The Linux man-pages project)、IBM 知识库及 Red Hat/Canonical 最佳实践,所有命令均在 Ubuntu 22.04、CentOS 7/8 和 Debian 11 实测验证。

0