Linux lsb_release Command
I. Command Overview
lsb_release is a command-line tool in Linux used to display Linux Standard Base (LSB) and distribution-specific information. It provides detailed information about the current Linux distribution, including distribution ID, description, version number, and more.
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 degree of compatibility.lsb_release The command is one of the tools implementing this standard.
1.2 Typical use cases
- Quickly view the distribution information of the current system
- Determine the system version in scripts to perform different actions
- System administrators gathering system information
- Check system compatibility before software installation
II. Command Installation
Most mainstream Linux distributions come with lsb_release preinstalled. If your system does not have it, you can install it with the following command:
2.1 Installation Methods for Different Distributions
# Debian/Ubuntu system
sudo apt-get install lsb-release
# RedHat/CentOS System
sudo yum install redhat-lsb-core
# Arch Linux system
sudo pacman -S lsb-release
2.2 Verify the Installation
After installation, you can verify it was successful with the following command:
which lsb_release
If a path like /usr/bin/lsb_release is returned, the installation was successful.
III. Command Syntax and Options
3.1 Basic Syntax
lsb_release [选项]
3.2 Common Options in Detail
| Option | Full name | Description |
|---|---|---|
-a |
--all |
Display all information (default behavior) |
-d |
--description |
Display the distribution description |
-i |
--id |
Display the distribution ID |
-r |
--release |
Display the distribution version number |
-c |
--codename |
Display the distribution codename |
-s |
--short |
Display information in a short format |
-h |
--help |
Display help information |
-v |
--version |
Display the command's version information |
IV. Usage Examples
4.1 Display All System Information
Example
lsb_release
-a
Example output:
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
4.2 Display Only the Distribution ID
Example
lsb_release
-i
Example output:
Distributor ID: Ubuntu
4.3 Display Only the Version Number
Example
lsb_release
-r
Example output:
Release: 20.04
4.4 Short-Format Output
Example
lsb_release
-s
-i
Example output:
Ubuntu
V. Practical Application Cases
5.1 Determine the System Version in a Script
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 a Specific Feature
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 The Command Returns "No LSB modules are available"
This is a common message indicating that the system does not have the full LSB modules installed, but it does not affect the lsb_release basic functionality. If you want to remove this message, you can install the full LSB package:
Example
Ubuntu/Debian
sudo
apt-get install
lsb-core
CentOS/RHEL
sudo
yum install
redhat-lsb
6.2 How to Obtain Similar Information on Non-LSB-Compliant Systems
For systems that do not support lsb_release , you can use the following alternative methods:
Example
View the /etc/*-release files
cat
/
etc
/*
-release
Or use the hostnamectl command (systemd systems)
hostnamectl
6.3 The Command Output Is Empty or Incorrect
If both the lsb_release If the command output is empty or incorrect, the /etc/lsb-release file may be missing or incorrectly formatted. You can try to create or repair the file manually:
Example
sudo
nano
/
etc
/
lsb-release
Example file contents:
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=20.04
DISTRIB_CODENAME=focal
DISTRIB_DESCRIPTION="Ubuntu 20.04.3 LTS"
VII. Summary
lsb_release is a simple but very useful command, especially for obtaining system information or performing compatibility checks in scripts. Through this article, you should be able to:
- Understand
lsb_releaseUnderstand the purpose of the command and the LSB standard - Master the installation and basic usage of the command
- Use the command flexibly in scripts for system detection
- Resolve common problems encountered during use
Remember, although lsb_release is convenient, when writing cross-platform scripts it is best to combine it with other system information checks to ensure compatibility.
Linux Command Reference