How to Check Linux Version from Command Line (CLI): 3 Simple Methods That Actually Work
Every systems administrator encounters this scenario: You SSH into a production server during an incident, and you need to know the exact Linux version immediately.
Version information determines:
- Which security patches apply to your system
- What commands and features are available
- How to troubleshoot kernel-specific issues
- Which documentation to reference
This guide shows you three reliable methods to check the Linux version from the command line. These methods work across different distributions and scenarios you’ll encounter in production environments.
Table of Contents
When and Why You Need to Check Linux Version Information
Critical Scenarios for Version Checking
- Security patching: Verify affected systems before applying CVE fixes
- Software deployment: Confirm compatibility requirements
- Troubleshooting: Identify kernel-specific bugs
- Compliance audits: Document system versions for regulatory requirements
- Automation: Detect versions in scripts for conditional execution
Linux Version Components Explained
- Distribution name: Ubuntu, RHEL, CentOS, Debian
- Release version: 20.04, 8.5, 11
- Kernel version: 5.4.0-42-generic
- Architecture: x86_64, aarch64, i386
- Codename: Focal Fossa, Stream, Bullseye
Method 1: Using the /etc/os-release File (Most Universal Method)
The cat /etc/os-release Command
The /etc/os-release file contains operating system identification data. This method works on all systemd-based distributions.
Basic Command:
cat /etc/os-release
Sample Output (Ubuntu 20.04):
NAME="Ubuntu"
VERSION="20.04.3 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.3 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal
Alternative Commands for Specific Information
Get only the pretty name:
grep PRETTY_NAME /etc/os-release | cut -d '"' -f2
Output: Ubuntu 20.04.3 LTS
Extract version using source:
source /etc/os-release && echo $PRETTY_NAME
Output: Ubuntu 20.04.3 LTS
Get distribution ID:
grep "^ID=" /etc/os-release | cut -d '=' -f2
Output: ubuntu
Pros and Cons
Advantages:
- Works on all modern Linux distributions (2012+)
- Standardized format across distributions
- No additional packages required
- Machine-readable format
Disadvantages:
- Not available on systems older than 2012
- Missing on some embedded systems
- May not exist in minimal containers
Method 2: Using the lsb_release Command (Most Detailed Information)
Understanding LSB (Linux Standard Base)
The lsb_release command provides standardized Linux distribution information. It reads data from multiple sources to give comprehensive version details.
Installation Check
# Check if lsb_release is installed
which lsb_release
# Install if missing (Ubuntu/Debian)
sudo apt-get install lsb-release
# Install if missing (RHEL/CentOS)
sudo yum install redhat-lsb-core
Essential lsb_release Commands
Display all information:
lsb_release -a
Sample Output (Ubuntu Server):
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 20.04.3 LTS
Release: 20.04
Codename: focal
Sample Output (CentOS 7):
LSB Version: :core-4.1-amd64:core-4.1-noarch
Distributor ID: CentOS
Description: CentOS Linux release 7.9.2009 (Core)
Release: 7.9.2009
Codename: Core
Specific Information Commands
lsb_release -d– Description onlylsb_release -r– Release number onlylsb_release -c– Codename onlylsb_release -i– Distributor ID onlylsb_release -s– Short output (no labels)
Examples with output:
# Get just the description
lsb_release -d
Description: Ubuntu 20.04.3 LTS
# Get release number without label
lsb_release -rs
20.04
# Get codename without label
lsb_release -cs
focal
When This Method Fails
The lsb_release command may not work when:
- The lsb-release package is not installed
- Running on minimal or embedded systems
- Using non-LSB compliant distributions (Arch Linux, Gentoo)
- Operating within Docker containers without the package
Method 3: Using the hostnamectl Command (Best for SystemD Systems)
The Power of hostnamectl
The hostnamectl command is part of systemd. It provides system and kernel information in a clean, readable format.
Basic Command:
hostnamectl
Sample Output (RHEL 8):
Static hostname: server01.example.com
Icon name: computer-server
Chassis: server
Machine ID: f9a3c6d7e8b9a0b1c2d3e4f5a6b7c8d9
Boot ID: a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6
Operating System: Red Hat Enterprise Linux 8.5 (Ootpa)
CPE OS Name: cpe:/o:redhat:enterprise_linux:8.5:GA:server
Kernel: Linux 4.18.0-348.el8.x86_64
Architecture: x86-64
Sample Output (Ubuntu 22.04):
Static hostname: ubuntu-server
Icon name: computer-vm
Chassis: vm
Machine ID: d5f6e7a8b9c0d1e2f3a4b5c6d7e8f9a0
Boot ID: e2f3a4b5c6d7e8f9a0b1c2d3e4f5a6b7
Operating System: Ubuntu 22.04 LTS
Kernel: Linux 5.15.0-25-generic
Architecture: x86-64
Specific Information Extraction
Get just the operating system:
hostnamectl | grep "Operating System" | cut -d ':' -f2
Output: Ubuntu 22.04 LTS
Get kernel version:
hostnamectl | grep "Kernel" | cut -d ':' -f2
Output: Linux 5.15.0-25-generic
JSON Output for Scripting
hostnamectl status --json=pretty
Requirements and Limitations
Works on:
- RHEL/CentOS 7+
- Ubuntu 16.04+
- Debian 8+
- Fedora (all recent versions)
- openSUSE Leap 15+
Does not work on:
- Systems without systemd
- Ubuntu 14.04 and older
- CentOS 6 and older
- Alpine Linux
- Most embedded systems
Bonus Methods: Distribution-Specific Commands
The uname Command for Kernel Information
The uname command displays system information, primarily kernel details.
Common uname options:
# All information
uname -a
Linux server01 5.4.0-42-generic #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020 x86_64 x86_64 x86_64 GNU/Linux
# Kernel release only
uname -r
5.4.0-42-generic
# Kernel version only
uname -v
#46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020
# Machine architecture only
uname -m
x86_64
# Operating system
uname -o
GNU/Linux
Distribution-Specific Files
Each distribution family maintains version files in different locations:
Red Hat based systems:
cat /etc/redhat-release
# Output: CentOS Linux release 7.9.2009 (Core)
Debian based systems:
cat /etc/debian_version
# Output: 11.2
SUSE based systems:
cat /etc/SuSE-release
# Output: openSUSE Leap 15.3
Arch Linux:
cat /etc/arch-release
# Note: This file is usually empty, just indicates Arch Linux
Using /proc/version
The /proc/version file provides kernel compilation information:
cat /proc/version
Linux version 5.4.0-42-generic (buildd@lgw01-amd64-038) (gcc version 9.3.0 (Ubuntu 9.3.0-10ubuntu2)) #46-Ubuntu SMP Fri Jul 10 00:24:02 UTC 2020
Distribution Detection Priority Order
- Check
/etc/os-release(most reliable) - Try
lsb_release -a(most detailed) - Use
hostnamectl(systemd systems) - Check distribution-specific files
- Parse
/proc/version(last resort)
Quick Reference: Command Cheat Sheet for Linux Version Checking
Universal Commands Table
| Command | Works On | Information Provided | Reliability |
|---|---|---|---|
cat /etc/os-release |
Most modern Linux (2012+) | Distribution, version, codename | 95% |
lsb_release -a |
LSB-compliant distributions | Detailed distribution info | 85% |
hostnamectl |
Systemd-based systems | OS, kernel, architecture | 80% |
uname -a |
All Linux systems | Kernel information only | 100% |
cat /proc/version |
All Linux systems | Kernel build information | 100% |
One-Liner Scripts for Multiple Checks
Universal version checker:
cat /etc/os-release 2>/dev/null || lsb_release -d 2>/dev/null || cat /etc/redhat-release 2>/dev/null || cat /etc/debian_version 2>/dev/null
Quick distribution identifier:
( source /etc/os-release 2>/dev/null && echo $ID ) || ( lsb_release -is 2>/dev/null )
Kernel and OS in one line:
echo "OS: $(cat /etc/os-release | grep PRETTY_NAME | cut -d'"' -f2) | Kernel: $(uname -r)"
Copy-Paste Commands by Distribution
Ubuntu/Debian:
cat /etc/os-release | grep PRETTY_NAME
lsb_release -ds
cat /etc/debian_version
RHEL/CentOS/Fedora:
cat /etc/redhat-release
rpm -q centos-release
hostnamectl | grep "Operating System"
SUSE/openSUSE:
cat /etc/os-release | grep PRETTY_NAME
cat /etc/SuSE-release
zypper --version
Troubleshooting: When Standard Commands Don’t Work
Common Issues and Solutions
Issue 1: Permission Denied
Problem: Cannot read version files
# Solution: Use sudo or check with readable files
sudo cat /etc/os-release
# Or use world-readable alternatives
cat /proc/version
Issue 2: File Not Found
Problem: /etc/os-release doesn’t exist
# Solution: Try alternative locations
ls /etc/*-release
ls /etc/*_version
# Check for any release files
find /etc -name "*release*" -o -name "*version*" 2>/dev/null
Issue 3: Command Not Found
Problem: lsb_release or hostnamectl missing
# Solution: Install required packages or use alternatives
# For lsb_release
which lsb_release || echo "lsb_release not installed"
# Use alternative
cat /etc/os-release 2>/dev/null || uname -a
Special Environments
Docker Containers:
# From host, check container OS
docker exec container_name cat /etc/os-release
# Inside container
cat /etc/os-release || cat /proc/version
Minimal Systems:
# Use package manager as indicator
# Debian/Ubuntu
dpkg --version && echo "Debian-based system"
# RHEL/CentOS
rpm --version && echo "RPM-based system"
# Alpine
apk --version && echo "Alpine Linux"
Embedded Systems:
# Check busybox version
busybox --help | head -1
# Check init system
ls -la /sbin/init
# Kernel version always works
uname -r
Automation Tips: Scripting Linux Version Detection
Bash Function for Cross-Distribution Detection
#!/bin/bash
get_linux_version() {
if [ -f /etc/os-release ]; then
. /etc/os-release
echo "Distribution: $NAME"
echo "Version: $VERSION"
echo "ID: $ID"
elif [ -f /etc/redhat-release ]; then
echo "Distribution: $(cat /etc/redhat-release)"
elif [ -f /etc/debian_version ]; then
echo "Distribution: Debian $(cat /etc/debian_version)"
else
echo "Unknown distribution"
echo "Kernel: $(uname -r)"
fi
}
# Call the function
get_linux_version
Python Script for Version Detection
#!/usr/bin/env python3
import platform
import subprocess
def get_linux_info():
info = {}
# Get basic platform info
info['system'] = platform.system()
info['node'] = platform.node()
info['release'] = platform.release()
info['version'] = platform.version()
info['machine'] = platform.machine()
# Try to get distribution info
try:
with open('/etc/os-release') as f:
for line in f:
if line.startswith('PRETTY_NAME='):
info['distribution'] = line.split('=')[1].strip().strip('"')
except:
info['distribution'] = 'Unknown'
return info
# Print the information
for key, value in get_linux_info().items():
print(f"{key}: {value}")
Integration with Configuration Management Tools
Ansible fact gathering:
ansible hostname -m setup -a "filter=ansible_distribution*"
# In playbook
- name: Display OS info
debug:
msg: "OS: {{ ansible_distribution }} {{ ansible_distribution_version }}"
Puppet facter:
facter os
facter os.family
facter os.release.full
Salt grains:
salt 'minion*' grains.item os os_family osrelease oscodename
Security Considerations When Checking Linux Versions
Information Disclosure Risks
- Version information reveals potential vulnerabilities
- Avoid exposing version details in public-facing services
- Limit version checks to authenticated sessions
Secure Methods for Remote Checking
SSH with key authentication:
ssh -i ~/.ssh/id_rsa user@server "cat /etc/os-release"
Using sudo for restricted access:
# In sudoers file
user ALL=(ALL) NOPASSWD: /bin/cat /etc/os-release
Audit Logging
# Log version checks
echo "$(date): Version check by $(whoami)" >> /var/log/version_checks.log
cat /etc/os-release
Best Practices
- Restrict version information to authorized personnel
- Use configuration management tools for centralized checking
- Avoid version disclosure in error messages
- Regularly update systems to latest stable versions
Conclusion: Best Practices for Linux Version Management
Key Takeaways
- Start with /etc/os-release – Works on 95% of modern Linux systems
- Use lsb_release -a for details – Provides comprehensive information when available
- Keep hostnamectl in your toolkit – Essential for systemd-based systems
- Always have uname -r ready – Universal kernel version checker
- Document your environment – Know which methods work on your systems
Quick Decision Tree
- Need distribution info? → Try
cat /etc/os-release - Need detailed output? → Use
lsb_release -a - On a systemd system? → Run
hostnamectl - Need kernel only? → Execute
uname -r - Nothing works? → Check
/proc/version
Action Items for System Administrators
- Create a standard version-checking script for your environment
- Document version information for all production systems
- Include version checks in your troubleshooting runbooks
- Automate version inventory using configuration management
- Set up monitoring for end-of-life versions
Remember This
Version checking is not just about knowing numbers. It enables informed decisions about security updates, compatibility, and system maintenance. Master these commands, and you’ll navigate any Linux environment with confidence.
FAQ: Common Questions About Linux Version Commands
Q: Which method works on all Linux distributions?
A: The uname -r command works on all Linux systems for kernel version. For distribution information, cat /etc/os-release works on most modern systems (2012+).
Q: How do I check Linux version without root access?
A: All methods mentioned work without root access:
cat /etc/os-release # No root needed
lsb_release -a # No root needed
hostnamectl # No root needed
uname -a # No root needed
Q: What’s the difference between kernel version and distribution version?
A: Kernel version (shown by uname -r) is the Linux kernel core version. Distribution version (shown by lsb_release or /etc/os-release) is the complete OS package version including userland tools.
Q: How do I check version in Docker containers?
A: From host system:
docker exec container_name cat /etc/os-release
From inside container:
cat /etc/os-release || cat /proc/version
Q: Can I check a remote Linux server’s version via SSH?
A: Yes, use SSH with any version command:
ssh user@server "cat /etc/os-release"
ssh user@server "lsb_release -a"
ssh user@server "uname -a"
Q: Why does lsb_release show “No LSB modules available”?
A: This message is normal. It means optional LSB modules aren’t installed, but the command still works and shows version information below this message.
Q: How do I check if my Linux version is still supported?
A: Check the distribution’s lifecycle page:
- Ubuntu:
ubuntu-support-status - RHEL: Check Red Hat’s lifecycle page
- Debian: Check Debian’s LTS page
Q: What if none of these commands work?
A: Try these fallback methods:
# Check package manager
which apt && echo "Likely Debian-based"
which yum && echo "Likely RHEL-based"
# Check for any version files
ls -la /etc/*release* /etc/*version*
# Last resort - check kernel
cat /proc/version
If you have MikroTik network equipment, feel free to check our list of MikroTik guides.