
Free operationg system. The word "Linux" is lined with gold letters on wooden planks. 3D illustration graphics
最简单的方法,是检查文件 /etc/os-release,该文件中记录有系统的相关信息:
- cat /etc/os-release
其输出类似于如下内容:
- NAME="Ubuntu"
- VERSION="20.04.1 LTS (Focal Fossa)"
- ID=ubuntu
- ID_LIKE=debian
- PRETTY_NAME="Ubuntu 20.04.1 LTS"
- VERSION_ID="20.04"
- HOME_URL="https://www.ubuntu.com/"
- SUPPORT_URL="https://help.ubuntu.com/"
- BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
- PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
- VERSION_CODENAME=focal
- UBUNTU_CODENAME=focal
从上面输出中可以看到,系统发行版为 Ubuntu,版本号为 20.04.1。
上述是查看 Linux 系统版本的一种方法,除此以外,还有另外一些方式,下面我们再来详细看一下。
如果你熟悉 Linux 目录结构,那么应该会了解在目录 /etc 下会包含有系统的核心配置文件。
其中的 os-release 文件,记录 Linux 发行版相关的信息,包括发行版名称、版本号或者ID等。
查看此文件,可以在终端中使用 cat 命令,如下所示:
- cat /etc/os-release
下面是运行在 Linode 基础设备上的 Alpine Linux 服务器的显示内容:
- $ cat /etc/os-release
- NAME="Alpine Linux"
- ID=alpine
- VERSION_ID=3.12.0
- PRETTY_NAME="Alpine Linux v3.12"
- HOME_URL="https://alpinelinux.org/"
- BUG_REPORT_URL="https://bugs.alpinelinux.org/"
上面输出中,Linux 发行版的名称是 Alpine Linux,版本号是 3.12。
基于不同的 Linux 发行版,文件 /etc/os-release 中的内容通常是不同的,它通常用来记录系统的一些额外信息,包括获取支持的方式,以及 bugs 等。
比如,在 CentOS 中,/etc/os-release 的内容如下所示:
- NAME="CentOS Linux"
- VERSION="8 (Core)"
- ID="centos"
- ID_LIKE="rhel fedora"
- VERSION_ID="8"
- PLATFORM_ID="platform:el8"
- PRETTY_NAME="CentOS Linux 8 (Core)"
- ANSI_COLOR="0;31"
- CPE_NAME="cpe:/o:centos:centos:8"
- HOME_URL="https://www.centos.org/"
- BUG_REPORT_URL="https://bugs.centos.org/"
- CENTOS_MANTISBT_PROJECT="CentOS-8"
- CENTOS_MANTISBT_PROJECT_VERSION="8"
- REDHAT_SUPPORT_PRODUCT="centos"
- REDHAT_SUPPORT_PRODUCT_VERSION="8"
不过,不管是哪个发行版,在文件 /etc/os-release 中都会记录系统的版本名称和版本号。基于此,查看该文件来获取版本信息是最可靠的方法。
现在大多数 Linux 发行版都使用 systemd,基于此,也可以使用 hostnamectl 命令来查看系统的版本信息。
- hostnamectl
比如在 CentOS 系统中,其输出信息如下:
- [root@li2498-99 ~]# hostnamectl
- Static hostname: localhost.localdomain
- Transient hostname: li2498-99.members.linode.com
- Icon name: computer-vm
- Chassis: vm
- Machine ID: e3fe2be3e17be3e1763bf43e8337e68b
- Boot ID: 33d3052bbffd44b1869bbffd4b00d26c
- Virtualization: kvm
- Operating System: CentOS Linux 8 (Core)
- CPE OS Name: cpe:/o:centos:centos:8
- Kernel: Linux 4.18.0-147.8.1.el8_1.x86_64
- Architecture: x86-64
在 “Operating System” 开头的那一行中,即显示了系统的信息。
hostnamectl 命令主要用来处理主机名相关的操作,但是因为它也会提供系统的其他信息,所以用它来查看系统版本信息也是一种方法。
lsb_release 命令并不是在所有 Linux 发行版中都能使用,它主要用在基于 Debian/Ubuntu 的系统中。
使用 lsb_release 命令连同 -a 选项可以查看到系统发行版的信息:
- lsb_release -a
在其输出中,可忽略信息“No LSB modules are available”,这并不是什么错误。如下所示:
- linux@handbook:~$ lsb_release -a
- No LSB modules are available.
- Distributor ID:Ubuntu
- Description:Ubuntu 20.04.1 LTS
- Release:20.04
- Codename:focal
上面我们介绍的是查看系统发行版的方法,接下来我们再简单介绍一下查看 Linux 内核版本的方式。
查看 Linux 内核版本信息,可以使用 uname 命令,如下:
- uname -r
其输出即为 Linux 内核版本信息:
- $ uname -r
- 5.4.43-1-virt
如上述输出所示,其内核版本为 5.4.43。
以上即为本次分享全部内容,欢迎讨论。
