Linux lsb_release Command

Linux 命令大全Linux Command Encyclopedia


I. Command Overview

lsb_releaseIt is a command-line tool in Linux systems used to display Linux Standard Base (LSB) and specific distribution information. It can provide detailed information about the current Linux distribution, including distribution ID, description, version number, etc.

1.1 Introduction to LSB

LSB (Linux Standard Base) is a project led by the Linux Foundation, aimed at standardizing the structure of Linux systems so that different distributions can maintain a certain degree of compatibility.lsb_releaseThe lsb_release command is one of the implementation tools of this standard.

1.2 Typical Application Scenarios

  • Quickly view the distribution information of the current system
  • Determine the system version in scripts to execute different operations
  • System administrators collect system information
  • Check system compatibility before software installation

II. Command Installation

Most mainstream Linux distributions come with the lsb_release command pre-installed.lsb_releaseIf your system does not have it installed, you can use the following commands to install it:

2.1 Installation Methods for Different Distributions

# Debian/Ubuntu 系统
sudo apt-get install lsb-release

# RedHat/CentOS 系统
sudo yum install redhat-lsb-core

# Arch Linux 系统
sudo pacman -S lsb-release

2.2 Verify Installation

After installation is complete, you can verify whether the installation was successful with the following command:

which lsb_release

If it returns a path similar to/usr/bin/lsb_releasethe installation was successful.


III. Command Syntax and Options

3.1 Basic Syntax

lsb_release [选项]

3.2 Detailed Explanation of Common Options

Option Full Name Description
-a --all Display all information (default behavior)
-d --description Display distribution description
-i --id Display distribution ID
-r --release Display distribution version number
-c --codename Display distribution codename
-s --short Display information in short format
-h --help Display help information
-v --version Display command version information

IV. Usage Examples

4.1 Display All System Information

Example

lsb_release -a

Sample output:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 20.04.3 LTS
Release:        20.04
Codename:       focal

4.2 Display Only Distribution ID

Example

lsb_release -i

Sample output:

Distributor ID: Ubuntu

4.3 Display Only Version Number

Example

lsb_release -r

Sample output:

Release:        20.04

4.4 Short Format Output

Example

lsb_release -s -i

Sample output:

Ubuntu

V. Practical Application Cases

5.1 Determining System Version in Scripts

Example

#!/bin/bash

DISTRO=$(lsb_release -s -i)
VERSION=$(lsb_release -s -r)

if [ "$DISTRO" = "Ubuntu" ] && [ "$VERSION" = "20.04" ]; then
    echo "The system is Ubuntu 20.04"
else
    echo "The system is not Ubuntu 20.04"
fi

5.2 Check Whether the System Supports Specific Features

Example

#!/bin/bash

# Check whether it is CentOS 7 or higher
if [ "$(lsb_release -s -i)" = "CentOS" ]; then
    VERSION=$(lsb_release -s -r | cut -d '.' -f 1)
    if [ "$VERSION" -ge 7 ]; then
        echo "The system meets the requirements"
    else
        echo "CentOS 7 or higher is required"
    fi
else
    echo "Not a CentOS system"
fi

VI. Frequently Asked Questions

6.1 Command Returns "No LSB modules are available"

This is a common prompt message indicating that the system does not have the complete LSB modules installed, but it will not affectlsb_releasethe basic functionality. If you need to eliminate this prompt, you can install the complete LSB package:

Example

# Ubuntu/Debian
sudo apt-get install lsb-core

# CentOS/RHEL
sudo yum install redhat-lsb

6.2 How to Get Similar Information on Non-LSB Compatible Systems

For systems that do not supportlsb_releaseyou can use the following alternative methods:

Example

# View /etc/*-release files
cat /etc/*-release

# Or use the hostnamectl command (systemd systems)
hostnamectl

6.3 Command Output is Empty or Error

Iflsb_releasethe command output is empty or an error, it may be because the/etc/lsb-releasefile is missing or the format is incorrect. You can try to manually create or repair the file:

Example

sudo nano /etc/lsb-release

File content example:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"

VII. Summary

lsb_releaseIt is a simple but very useful command, especially suitable for obtaining system information in scripts or performing system compatibility checks. Through this article, you should be able to:

  1. Understandlsb_releasethe role of the command and the LSB standard
  2. Master the installation method and basic usage of the command
  3. Flexibly use the command in scripts to make system judgments
  4. Solve common problems encountered during use

Remember, althoughlsb_releaseit is very convenient, when writing cross-platform scripts, it is best to combine other system information checking methods to ensure script compatibility.

Linux 命令大全Linux Command Encyclopedia

Other Extensions