Linux 查看系统版本,这几种方法够用了
刚入行那会,总记不住系统版本怎么看,每次都得man一下。后来发现就这几个命令,背都背下来了。直接上干货。
1. 最通用:cat /etc/os-release
几乎现代所有发行版都支持,输出干净整洁:
cat /etc/os-release
输出示例:
NAME="Ubuntu"
VERSION="20.04.6 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
VERSION_ID="20.04"
注意:有些精简容器镜像可能没有这个文件,但大部分都有。
2. 红帽系专用:/etc/redhat-release
CentOS、RHEL、Fedora 等系统专用,直接读文件:
cat /etc/redhat-release
输出类似:
CentOS Linux release 7.9.2009 (Core)
Fedora 上也可能叫 /etc/fedora-release,但通常软链到 redhat-release。
3. Debian/Ubuntu 系:lsb_release -a
需要安装 lsb-release 包,但大部分桌面版自带:
lsb_release -a
输出:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
如果没装这个包,直接看 /etc/debian_version:
cat /etc/debian_version
4. 内核版本:uname -a
看内核版本和架构,这是最底层的:
uname -a
输出:
Linux hostname 5.15.0-91-generic #101-Ubuntu SMP Tue Nov 14 13:30:08 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
只想要内核版本:
uname -r
只想要架构:
uname -m
5. 更详细的信息:hostnamectl
Systemd 系统下,这个命令能一次性给出系统版本、内核、架构:
hostnamectl
输出:
Static hostname: hostname
Icon name: computer-vm
Chassis: vm
Machine ID: xxxx
Boot ID: xxxx
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.15.0-91-generic
Architecture: x86-64
6. 其他发行版特殊文件
- Alpine Linux:
cat /etc/alpine-release - openSUSE:
cat /etc/SuSE-release或/etc/os-release - Arch Linux:
cat /etc/arch-release(内容为空,存在即代表是 Arch)
7. 快速判断发行版(一行命令)
如果你只想知道是什么发行版,不用管版本号:
cat /etc/*release | head -1
或者更粗暴的:
grep -E '^ID=' /etc/os-release | cut -d= -f2
总结一下我的习惯
- 日常看版本:
cat /etc/os-release或者hostnamectl - 检查内核:
uname -r - 写脚本判断:读
/etc/os-release里的ID和VERSION_ID - 快速确认是啥系统:
cat /etc/*release | head -1
记住,/etc/os-release 是标准,其他都是特例。遇到不认识的系统,先 cat 这个文件准没错。
最后提醒:别用 lsb_release 写脚本,因为不是所有系统都装了。写脚本就用 cat /etc/os-release 或者 grep 提取,稳得很。
💻 安全运维 / Linux运维 / 渗透测试 技术支持
业务需求可联系博客作者
