Command To Find Os Version In Linux

Command To Find Os Version In Linux

Reading time1 min
#Linux#Commands#OS#LinuxVersion#Distro#Kernel

How to Identify Your Linux OS Version—Efficiently and Reliably

Scenario:
Package compatibility breaks. Security teams request patch status. You’re handed a VM with a prompt, no context, and need to identify OS and kernel specifics immediately.


Why Precise OS Versioning Matters

Distributions segment support boundaries on minor releases—installing a Python 3.12 wheel built for Ubuntu 22.04 may fail on 20.04. Container images rely on base OS consistency for reproducibility. Kernel version determines hardware enablement and CVE applicability. In short: “Ubuntu” isn’t enough.


Reliable Methods to Extract OS and Kernel Version

Direct-to-the-point: several approaches provide slightly different level of detail and usability. The viability depends on distro, system age, and minimalism of base image.

1. lsb_release Utility (When Present)

lsb_release aggregates release info from known files. Useful for automation and clean string parsing.

lsb_release -a

Sample output:

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.2 LTS
Release:        22.04
Codename:       jammy
  • For just the version:
    lsb_release -r | awk '{print $2}'
    # Output: 22.04
    

Note: Minimal containers and some stripped-down distros won’t have lsb_release installed. On Alpine or busybox-based systems, this is common.


2. Parsing /etc/os-release

/etc/os-release is the freedesktop standard for identifying OS information, nearly always present post-2016.

cat /etc/os-release

Typical excerpt:

NAME="Debian GNU/Linux"
VERSION="12 (bookworm)"
ID=debian
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
VERSION_ID="12"
  • Parse for automation:
    grep PRETTY_NAME /etc/os-release | cut -d= -f2 | tr -d '"'
    # Output: Debian GNU/Linux 12 (bookworm)
    

Known issue: nonstandard or customized distros sometimes rewrite this file during image build. Manual verification may be necessary on custom appliances.


3. Old-School: /etc/issue (Login Prompt Context Only)

cat /etc/issue
# Output: Ubuntu 22.04.2 LTS \n \l

Use-case: identifying distro version pre-login on physical terminals or serial consoles. Less reliable on containers or heavily-customized images.


4. Kernel Version: uname

The kernel version is orthogonal to base OS—it often tracks ahead on security- or performance-tuned systems.

uname -r
# Output: 6.5.0-28-generic
  • Full identification (with architecture):
    uname -mrs
    # Output: Linux 6.5.0-28-generic x86_64
    

Gotcha: distributions backport kernel patches, so a 5.15.x kernel on Ubuntu may differ in security state from the upstream mainline. Use uname -v and check for # with build info for further granularity.


Quick Reference—Command Table

CommandPrimary UseOutput Example
lsb_release -aDistro and version (if present)Ubuntu 22.04.2 LTS
cat /etc/os-releaseUniversal, free-form infoPRETTY_NAME="Debian GNU/Linux 12 ..."
cat /etc/issueLogin prompt overrideUbuntu 22.04.2 LTS
uname -rKernel version6.5.0-28-generic

Practical Automation Tips

  • Script for Docker builds: Many minimal containers (Alpine, scratch) will lack these files/utilities. For Alpine:
    cat /etc/alpine-release
    
  • Ambiguity in cloud images: Cloud distros—like Amazon Linux or custom GCE images—sometimes bake custom /etc/os-release fields.
  • Historical systems: RHEL/CentOS 6-era systems use /etc/redhat-release.

Non-obvious tip: if all else fails and the OS was booted with systemd, try:

hostnamectl

which also surfaces OS, kernel, and virtualization status. Not universal, but handy.


Example: Diagnosing a Real Incident

A support ticket references failed package installation:

E: Unable to locate package docker-ce

On SSH, immediately run:

cat /etc/os-release
uname -r

Result:

NAME="Ubuntu"
VERSION="18.04.6 LTS (Bionic Beaver)"
...
4.15.0-214-generic

Conclusion: Package repos for Ubuntu 18.04 are EOL. Kernel is end-of-life. Plan upgrade before attempting modern container deployment.


Efficient OS and kernel identification form the basis for reliable automation, compliance, and production support. Bookmark the reference table above—experience shows that “what OS is this actually?” is rarely as trivial as expected.