Section 2.1: 检测您正在使用的基于debian的发行版

只需执行 lsb_release -a 这个命令。

在Debian下执行:

demouser@vbox-debian:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Debian
Description:    Debian GNU/Linux 12 (bookworm)
Release:    12
Codename:   bookworm

在Ubuntu下执行:

demouser@demohosts:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 24.04.1 LTS
Release:        24.04
Codename:       noble

如果你的系统中没有安装 lsb_release 命令的话,你可以换种思路,查看 /etc/os-release 或者 /etc/issue 文件,它们通常也包含发行版名称等信息。

ubuntu下的例子:

demouser@demohosts:~$ cat /etc/issue
Ubuntu 24.04.1 LTS \n \l

不要使用/etc/debian_version文件,因为它的内容与发行版名称不匹配!

注意,这个命令也适用于非debian系列的发行版,如Fedora、RHEL或openSUSE,但可能没有安装lsb_release。

Section 2.2: 检测您正在使用的基于systemd的发行版

这种方法适用于Arch、CentOS、CoreOS、Debian、Fedora、Mageia、openSUSE、Red Hat Enterprise Linux、SUSE Linux Enterprise Server、Ubuntu等现代版本。这种广泛的适用性使它成为一种理想的查看发行版本名称和版本的首选方法,如果您还需要识别旧系统,则可以使用其他方法。

查看/etc/os-release。具体来说,就是查看变量 NAME、VERSION、ID、VERSION_ID 和 PRETTY_NAME。

在AlmaLinux上,这个文件可能看起来像这样:

[root@almelinux-base ~]# cat /etc/os-release 
NAME="AlmaLinux"
VERSION="9.4 (Seafoam Ocelot)"
ID="almalinux"
ID_LIKE="rhel centos fedora"
VERSION_ID="9.4"
PLATFORM_ID="platform:el9"
PRETTY_NAME="AlmaLinux 9.4 (Seafoam Ocelot)"
ANSI_COLOR="0;34"
LOGO="fedora-logo-icon"
CPE_NAME="cpe:/o:almalinux:almalinux:9::baseos"
HOME_URL="https://almalinux.org/"
DOCUMENTATION_URL="https://wiki.almalinux.org/"
BUG_REPORT_URL="https://bugs.almalinux.org/"

ALMALINUX_MANTISBT_PROJECT="AlmaLinux-9"
ALMALINUX_MANTISBT_PROJECT_VERSION="9.4"
REDHAT_SUPPORT_PRODUCT="AlmaLinux"
REDHAT_SUPPORT_PRODUCT_VERSION="9.4"
SUPPORT_END=2032-06-01

在CentOS上,这个文件可能看起来像这样:

[root@desktop-centos ~]# cat /etc/os-release 
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"

CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7"

在Alpine Linux上,这个文件可能看起来像这样:

localhost:~# cat /etc/os-release 
NAME="Alpine Linux"
ID=alpine
VERSION_ID=3.20.3
PRETTY_NAME="Alpine Linux v3.20"
HOME_URL="https://alpinelinux.org/"
BUG_REPORT_URL="https://gitlab.alpinelinux.org/alpine/aports/-/issues"

这个文件的文档在freedesktop网站上;原则上,它不是特定于systemd的,但它存在于所有基于systemd的发行版中。

在bash shell中,可以获取/etc/os-release文件的源代码,然后直接使用各种变量,如下命令的执行结果将输出发行版本的名称、版本号以及代号:

[root@almelinux-base ~]# ( source /etc/os-release && echo "$PRETTY_NAME" )
AlmaLinux 9.4 (Seafoam Ocelot)

Section 2.3: 检测您正在使用的RHEL / CentOS / Fedora发行版

从 /etc/redhat-release文件中查看发行版本信息

cat /etc/redhat-release

下面是Fedora 41机器的输出:

demouser@vbox:~$ cat /etc/redhat-release
Fedora release 41 (Forty One)

正如在基于debian的响应中提到的,您还可以使用lsb_release -a命令,红帽一系默认没有安装`lsb_release命令,需要执行dnf install -y lsb_release进行安装。

[demouser@vbox ~]$ sudo dnf install -y lsb_release
[sudo] demouser 的密码:
Updating and loading repositories:
Repositories loaded.
Package                          Arch      Version                          Repository             Size
Installing:
 lsb_release                     noarch    3.3-4.fc41                       fedora             30.4 KiB

Transaction Summary:
 Installing:         1 package

Total size of inbound packages is 20 KiB. Need to download 20 KiB.
After this operation, 30 KiB extra will be used (install 30 KiB, remove 0 B).
[1/1] lsb_release-0:3.3-4.fc41.noarch                          100% |  54.5 KiB/s |  20.0 KiB |  00m00s
-------------------------------------------------------------------------------------------------------
[1/1] Total                                                    100% |  11.8 KiB/s |  20.0 KiB |  00m02s
Running transaction
[1/3] Verify package files                                     100% |  90.0   B/s |   1.0   B |  00m00s
[2/3] Prepare transaction                                      100% |   3.0   B/s |   1.0   B |  00m00s
[3/3] Installing lsb_release-0:3.3-4.fc41.noarch               100% |  31.2 KiB/s |  31.6 KiB |  00m01s
Complete!

安装完成后执行该命令,从Fedora 41计算机输出以下内容:

[demouser@vbox ~]$ lsb_release -a
LSB Version:    n/a
Distributor ID: Fedora
Description:    Fedora Linux 41 (Workstation Edition)
Release:        41
Codename:       n/a

Section 2.4: Uname 打印当前系统的信息

Uname是unix name的缩写。只需在命令行控制台中输入uname即可获得有关操作系统的信息。

uname命令的使用格式:

uname [OPTION]

如果没有指定选项,uname使用-s选项。

-a 或 --all 打印所有信息,如果未知信息,则省略 -p和 -i。

例如:

[demouser@vbox ~]$ uname -a
Linux vbox 6.11.4-301.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Oct 20 15:02:33 UTC 2024 x86_64 GNU/Linux

下面是通过uname --help得到的关于uname命令的帮助信息。

用法:uname [选项]...
输出特定的系统信息。如果不带 <选项>,则视为使用了 -s 选项。

  -a, --all                按如下次序输出所有信息,其中若 -p 和 -i 的
                             探测结果为未知,则省略:
  -s, --kernel-name        输出内核名称
  -n, --nodename           输出网络节点的主机名
  -r, --kernel-release     输出内核发行号
  -v, --kernel-version     输出内核版本号
  -m, --machine            输出主机的硬件架构名称
  -p, --processor          输出处理器类型(不可移植)
  -i, --hardware-platform  输出硬件平台(不可移植)
  -o, --operating-system   输出操作系统名称
      --help        显示此帮助信息并退出
      --version     显示版本信息并退出

GNU coreutils 在线帮助:<https://www.gnu.org/software/coreutils/>
请向 <http://translationproject.org/team/zh_CN.html> 报告任何翻译错误
完整文档 <https://www.gnu.org/software/coreutils/uname>
或者在本地使用:info '(coreutils) uname invocation'

在Fedora 41中,输入uname -a获得

Section 2.5: 检测你的发行版的基本信息

只需要运行命令uname -a

在 AlmaLinux:

[root@almelinux-base ~]# uname -a
Linux almelinux-base 5.14.0-427.13.1.el9_4.x86_64 #1 SMP PREEMPT_DYNAMIC Tue Apr 30 18:22:29 EDT 2024 x86_64 x86_64 x86_64 GNU/Linux

Section 2.6: 使用GNU coreutils

因此,GNU coreutils应该可以在所有基于linux的系统上使用。

如果您不知道您正在使用的系统是那种类型的发行版本,那可能无法使用上面的示例其中的命令来进行查看,因此,你可以使用这个命令。

$ uname -a

在我的系统上,这个命令执行后的结果如下:

[demouser@vbox ~]$ uname -a
Linux vbox 6.11.4-301.fc41.x86_64 #1 SMP PREEMPT_DYNAMIC Sun Oct 20 15:02:33 UTC 2024 x86_64 GNU/Linux

Here you can see the following :

这些输出的信息中:

  • demohosts: 是主机名
  • 6.11.4-301.fc41.x86_64: 是内核版本和架构

欢迎大家说说RHEL和SuSe系统的结果。

Section 2.7: 查找您的linux操作系统(包括debian和rpm)的名称和发行版本号

大多数linux发行版将其版本信息存储在/etc/lsb-release (基于debian一系)或 /etc/redhat-release(基于红帽一系)文件中。

使用下面的通用命令应该可以让您通过大多数Debian和RPM衍生物,如Linux Mint和Cent-Os。

下面是基于Ubuntu系统的一个例子:

demouser@demohosts:~$ cat /etc/*release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=24.04
DISTRIB_CODENAME=noble
DISTRIB_DESCRIPTION="Ubuntu 24.04.1 LTS"
PRETTY_NAME="Ubuntu 24.04.1 LTS"
NAME="Ubuntu"
VERSION_ID="24.04"
VERSION="24.04.1 LTS (Noble Numbat)"
VERSION_CODENAME=noble
ID=ubuntu
ID_LIKE=debian
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"
UBUNTU_CODENAME=noble
LOGO=ubuntu-logo