lsb_release — 顯示 Linux 發行版資訊
顯示 Linux 發行版的名稱、版本號與代號(如 Ubuntu 22.04 Jammy)。
語法
lsb_release [選項]
常用選項
| 選項 | 說明 | 範例 |
|---|---|---|
-a, --all |
顯示所有資訊 | lsb_release -a |
-d, --description |
顯示發行版說明(最常用) | lsb_release -d |
-r, --release |
只顯示版本號 | lsb_release -r |
-c, --codename |
只顯示代號(如 jammy、bullseye) | lsb_release -c |
-i, --id |
只顯示發行版 ID(如 Ubuntu、Debian) | lsb_release -i |
-s, --short |
只輸出數值部分(不含標籤) | lsb_release -rs |
使用範例
範例 1:顯示發行版詳細資訊
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
範例 2:只顯示版本號(腳本常用)
在腳本中用 -rs(short release)取得純版本字串,方便比較。
$ lsb_release -rs
22.04
範例 3:在腳本中依發行版選擇操作
根據發行版 ID 決定要執行的套件管理指令。
$ DISTRO=$(lsb_release -si)
if [ "" = "Ubuntu" ] || [ "" = "Debian" ]; then
sudo apt install -y nginx
elif [ "" = "CentOS" ] || [ "" = "RedHatEnterpriseServer" ]; then
sudo yum install -y nginx
fi
範例 4:替代方式:直接讀取 os-release
若 lsb_release 不可用,/etc/os-release 是另一個標準方式。
$ cat /etc/os-release
NAME="Ubuntu"
VERSION="22.04.3 LTS (Jammy Jellyfish)"
ID=ubuntu
VERSION_ID="22.04"
常見錯誤與排錯
lsb_release: command not found
- 原因
- lsb-release 套件未安裝。
- 解法
- sudo apt install lsb-release,或改讀 cat /etc/os-release。