因為對于諸如 RHEL、Debian、openSUSE、Arch Linux 這幾種主流發(fā)行版來(lái)說(shuō),它們各自擁有不同的包管理器來(lái)管理系統上的軟件包,如果不知道所使用的是哪一個(gè)發(fā)行版的系統,在軟件包安裝的時(shí)候就會(huì )無(wú)從下手,而且由于大多數發(fā)行版都是用 systemd 命令而不是 SysVinit 腳本,在重啟服務(wù)的時(shí)候也難以執行正確的命令。
下面來(lái)看看可以使用那些基本命令來(lái)查看 Linux 發(fā)行版名稱(chēng)和版本號。
方法總覽
方法 1: lsb_release 命令
LSB(Linux 標準庫Linux Standard Base)能夠打印發(fā)行版的具體信息,包括發(fā)行版名稱(chēng)、版本號、代號等。
- # lsb_release -a
- No LSB modules are available.
- Distributor ID: Ubuntu
- Description: Ubuntu 16.04.3 LTS
- Release: 16.04
- Codename: xenial
方法 2: /etc/*-release 文件
release 文件通常被視為操作系統的標識。在 /etc 目錄下放置了很多記錄著(zhù)發(fā)行版各種信息的文件,每個(gè)發(fā)行版都各自有一套這樣記錄著(zhù)相關(guān)信息的文件。下面是一組在 Ubuntu/Debian 系統上顯示出來(lái)的文件內容。
- # cat /etc/issue
- Ubuntu 16.04.3 LTS \n \l
- # cat /etc/issue.net
- Ubuntu 16.04.3 LTS
- # cat /etc/lsb-release
- DISTRIB_ID=Ubuntu
- DISTRIB_RELEASE=16.04
- DISTRIB_CODENAME=xenial
- DISTRIB_DESCRIPTION="Ubuntu 16.04.3 LTS"
- # cat /etc/os-release
- NAME="Ubuntu"
- VERSION="16.04.3 LTS (Xenial Xerus)"
- ID=ubuntu
- ID_LIKE=debian
- PRETTY_NAME="Ubuntu 16.04.3 LTS"
- VERSION_ID="16.04"
- HOME_URL="http://www.ubuntu.com/"
- SUPPORT_URL="http://help.ubuntu.com/"
- BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"
- VERSION_CODENAME=xenial
- UBUNTU_CODENAME=xenial
- # cat /etc/debian_version
- 9.3
下面這一組是在 RHEL/CentOS/Fedora 系統上顯示出來(lái)的文件內容。其中 /etc/redhat-release 和 /etc/system-release 文件是指向 /etc/[發(fā)行版名稱(chēng)]-release文件的一個(gè)連接。
- # cat /etc/centos-release
- CentOS release 6.9 (Final)
- # cat /etc/fedora-release
- Fedora release 27 (Twenty Seven)
- # cat /etc/os-release
- NAME=Fedora
- VERSION="27 (Twenty Seven)"
- ID=fedora
- VERSION_ID=27
- PRETTY_NAME="Fedora 27 (Twenty Seven)"
- ANSI_COLOR="0;34"
- CPE_NAME="cpe:/o:fedoraproject:fedora:27"
- HOME_URL="https://fedoraproject.org/"
- SUPPORT_URL="https://fedoraproject.org/wiki/Communicating_and_getting_help"
- BUG_REPORT_URL="https://bugzilla.redhat.com/"
- REDHAT_BUGZILLA_PRODUCT="Fedora"
- REDHAT_BUGZILLA_PRODUCT_VERSION=27
- REDHAT_SUPPORT_PRODUCT="Fedora"
- REDHAT_SUPPORT_PRODUCT_VERSION=27
- PRIVACY_POLICY_URL="https://fedoraproject.org/wiki/Legal:PrivacyPolicy"
- # cat /etc/redhat-release
- Fedora release 27 (Twenty Seven)
- # cat /etc/system-release
- Fedora release 27 (Twenty Seven)
方法 3: uname 命令
uname(unix name 的意思) 是一個(gè)打印系統信息的工具,包括內核名稱(chēng)、版本號、系統詳細信息以及所運行的操作系統等等。
- # uname -a
- Linux localhost.localdomain 4.12.14-300.fc26.x86_64 #1 SMP Wed Sep 20 16:28:07 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux
以上運行結果說(shuō)明使用的操作系統版本是 Fedora 26。
方法 4: /proc/version 文件
這個(gè)文件記錄了 Linux 內核的版本、用于編譯內核的 gcc 的版本、內核編譯的時(shí)間,以及內核編譯者的用戶(hù)名。
- # cat /proc/version
- Linux version 4.12.14-300.fc26.x86_64 ([email protected]) (gcc version 7.2.1 20170915 (Red Hat 7.2.1-2) (GCC) ) #1 SMP Wed Sep 20 16:28:07 UTC 2017
方法 5: dmesg 命令
- # dmesg | grep "Linux"
- [ 0.000000] Linux version 4.12.14-300.fc26.x86_64 ([email protected]) (gcc version 7.2.1 20170915 (Red Hat 7.2.1-2) (GCC) ) #1 SMP Wed Sep 20 16:28:07 UTC 2017
- [ 0.001000] SELinux: Initializing.
- [ 0.001000] SELinux: Starting in permissive mode
- [ 0.470288] SELinux: Registering netfilter hooks
- [ 0.616351] Linux agpgart interface v0.103
- [ 0.630063] usb usb1: Manufacturer: Linux 4.12.14-300.fc26.x86_64 ehci_hcd
- [ 0.688949] usb usb2: Manufacturer: Linux 4.12.14-300.fc26.x86_64 ohci_hcd
- [ 2.564554] SELinux: Disabled at runtime.
- [ 2.564584] SELinux: Unregistering netfilter hooks
方法 6: Yum/Dnf 命令
Yum(Yellowdog 更新器修改版Yellowdog Updater Modified)是 Linux 操作系統上的一個(gè)包管理工具,而 yum 命令被用于一些基于 RedHat 的 Linux 發(fā)行版上安裝、更新、查找、刪除軟件包。
- # yum info nano
- Loaded plugins: fastestmirror, ovl
- Loading mirror speeds from cached hostfile
- * base: centos.zswap.net
- * extras: mirror2.evolution-host.com
- * updates: centos.zswap.net
- Available Packages
- Name : nano
- Arch : x86_64
- Version : 2.3.1
- Release : 10.el7
- Size : 440 k
- Repo : base/7/x86_64
- Summary : A small text editor
- URL : http://www.nano-editor.org
- License : GPLv3+
- Description : GNU nano is a small and friendly text editor.
下面的 yum repolist 命令執行后顯示了 yum 的基礎源倉庫、額外源倉庫、更新源倉庫都來(lái)自 CentOS 7 倉庫。
- # yum repolist
- Loaded plugins: fastestmirror, ovl
- Loading mirror speeds from cached hostfile
- * base: centos.zswap.net
- * extras: mirror2.evolution-host.com
- * updates: centos.zswap.net
- repo id repo name status
- base/7/x86_64 CentOS-7 - Base 9591
- extras/7/x86_64 CentOS-7 - Extras 388
- updates/7/x86_64 CentOS-7 - Updates 1929
- repolist: 11908
使用 dnf 命令也同樣可以查看發(fā)行版名稱(chēng)和版本號。
- # dnf info nano
- Last metadata expiration check: 0:01:25 ago on Thu Feb 15 01:59:31 2018.
- Installed Packages
- Name : nano
- Version : 2.8.7
- Release : 1.fc27
- Arch : x86_64
- Size : 2.1 M
- Source : nano-2.8.7-1.fc27.src.rpm
- Repo : @System
- From repo : fedora
- Summary : A small text editor
- URL : https://www.nano-editor.org
- License : GPLv3+
- Description : GNU nano is a small and friendly text editor.
方法 7: RPM 命令
RPM(紅帽包管理器RedHat Package Manager)是在 CentOS、Oracle Linux、Fedora 這些基于 RedHat 的操作系統上的一個(gè)強大的命令行包管理工具,同樣也可以幫助我們查看系統的版本信息。
- # rpm -q nano
- nano-2.8.7-1.fc27.x86_64
方法 8: APT-GET 命令
Apt-Get(高級打包工具Advanced Packaging Tool)是一個(gè)強大的命令行工具,可以自動(dòng)下載安裝新軟件包、更新已有的軟件包、更新軟件包列表索引,甚至更新整個(gè) Debian 系統。
- # apt-cache policy nano
- nano:
- Installed: 2.5.3-2ubuntu2
- Candidate: 2.5.3-2ubuntu2
- Version table:
- * 2.5.3-2ubuntu2 500
- 500 http://nova.clouds.archive.ubuntu.com/ubuntu xenial-updates/main amd64 Packages
- 100 /var/lib/dpkg/status
- 2.5.3-2 500
- 500 http://nova.clouds.archive.ubuntu.com/ubuntu xenial/main amd64 Packages