Linux Commands

How to Check OS Version from Command Line

Linux is a powerful operating system that’s free and open-source. Because it’s open-source, Linux is available in numerous variations. Generally, each variant is known as a Linux distribution (also known as Linux distro). You may have already heard the name of the big ones like Ubuntu, Debian, RHEL, Fedora, Arch Linux, SUSE, etc.

Knowing the name and version of a distro is vital for users and administrators. The information is important in numerous situations like installing new apps or features, troubleshooting, etc. This guide will demonstrate how to check the OS version from the command line on Linux.

Checking the operating system version

Here are some of the simplest ways of checking the operating system version. Almost all of these methods should apply to any Linux distro. These methods utilize the built-in tools and feature Linux comes with.

Checking OS version from /etc/os-release
In Linux, the os-release file contains various information about the system. There are two copies of the file at two different locations.

$ /etc/os-release
$ /usr/lib/os-release

Interestingly, the file located at /etc/os-release will take precedence over /usr/lib/os-release.

It contains information in the format of a newline-separated list of various variables. It contains info like OS name, version, version ID, builds ID, etc.

To check the content of the os-release file, run the following command.

$ cat /etc/os-release

Note that the output will vary from one Linux distro to another. This file will only be available if the OS uses the systemd as the init system.

Here are some examples that fine-tune the content of os-release to print only specific details. The following command will print only the version and name of the OS.

$ egrep '^(VERSION|NAME)=' /etc/os-release

To check the version of the OS only, run the following command instead.

$ grep '^VERSION' /etc/os-release

Checking OS version using lsb_release
The lsb_release tool provides certain LSB (Linux Standard Base) and distro-specific info. Generally, lsb_release is used in the following format.

$ lsb_release -a

Here, the flag “-a” tells lsb_release to print all the info about the OS it can provide. Alternatively, you can print a minimal report using the flag “-s” or “–short”. It will omit the headers in the output.

$ lsb_release --all --short

This tool also supports printing one specific piece of information at a time. For example, to print only the OS description, use the flag “-d” or “–description”.

$ lsb_release -d

To display the codename of the current OS, use the flag “-c” or “–codename”.

$ lsb_release -c

The man page contains an in-depth description of all the available options.

$ man lsb_release

Checking OS version using hostnamectl
The hostnamectl command is responsible for managing the Linux system hostname and related settings. It can also print a host of info on the system and operating system.

$ hostnamectl

Checking OS version using uname
Similar to lsb_release and hostnamectl, uname is another tool to report various info about the machine and operating system. However, the output is a bit more disorganized compared to hostnamectl output.

To print all the information about the system, run the following uname command. Here, the flag “-a” tells uname to print all the supported info.

$ uname -a

We can also tell uname to print only specific detail. For example, the flag “-v” or “–kernel-version” will print the kernel version.

$ uname -v

The following uname command will print the operating system label.

$ uname -o

For all the available options and their explanations, check the man page.

$ man uname

Checking OS version from /etc/issue
The file /etc/issue stores system identification information. It’s generally shown before you’re prompted to log in to your account. Check the content of the file.

$ cat /etc/issue

Checking OS version using /proc/version
It’s a text file that includes information about the Linux kernel.

$ cat /proc/version

Checking OS version using /etc/*release
If none of the aforementioned methods succeeded, then it’s highly likely that you’re running a very old Linux system. If that’s the case, then we have to take advantage of a very rudimentary method of checking OS information.

Run the following command.

$ cat /etc/*release

Here, cat will read and print the content of all the files that match the pattern. You can check what files we’re reading from.

$ ls -lh /etc/*release

Final thoughts

This guide demonstrates how to check the version of the Linux operating system running on the machine. Although there are many third-party tools, we can achieve it using the built-in tools and features only.

The version of a Linux operating system is an important piece of information. Generally, Linux packages are built to work with a particular Linux version in mind. Knowing the Linux version can help in troubleshooting unexpected behaviors or errors. It’s also important when a new version of the Linux distro is released. It’s simple yet crucial info to keep track of.

About the author

Sidratul Muntaha

Student of CSE. I love Linux and playing with tech and gadgets. I use both Ubuntu and Linux Mint.