Is there a way to determine what version (distribution & kernel version, I suppose) of Linux is running (from the command-line), that works on any Linux system?
9 Answers
The kernel is universally detected with uname:
$ uname -or
2.6.18-128.el5 GNU/LinuxThere really isn't a cross-distribution way to determine what distribution and version you're on. There have been attempts to make this consistent, but ultimately it varies, unfortunately. LSB tools provide this information, but ironically aren't installed by default everywhere. Example on an Ubuntu 9.04 system with the lsb-release package installed:
$ lsb_release -irc
Distributor ID: Ubuntu
Release: 9.04
Codename: jauntyOtherwise, the closest widely-available method is checking /etc/something-release files. These exist on most of the common platforms, and on their derivatives (i.e., Red Hat and CentOS).
Here are some examples.
Ubuntu has /etc/lsb-release:
$ cat /etc/lsb-release
DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=9.04
DISTRIB_CODENAME=jaunty
DISTRIB_DESCRIPTION="Ubuntu 9.04"But Debian has /etc/debian_version:
$ cat /etc/debian_version
5.0.2Fedora, Red Hat and CentOS have:
Fedora: $ cat /etc/fedora-release
Fedora release 10 (Cambridge)
Red Hat/older CentOS: $ cat /etc/redhat-release
CentOS release 5.3 (Final)
newer CentOS: $ cat /etc/centos-release
CentOS Linux release 7.1.1503 (Core)Gentoo:
$ cat /etc/gentoo-release
Gentoo Base System release 1.12.11.1I don't have a SUSE system available at the moment, but I believe it is /etc/SuSE-release.
Slackware has /etc/slackware-release and/or /etc/slackware-version.
Mandriva has /etc/mandriva-release.
For most of the popular distributions then,
$ cat /etc/*{release,version}will most often work. Stripped down and barebones "server" installations might not have the 'release' package for the distribution installed.
Additionally, two 3rd party programs you can use to automatically get this information are Ohai and Facter.
Note that many distributions have this kind of information in /etc/issue or /etc/motd, but some security policies and best practices indicate that these files should contain access notification banners.
Related:How to find out version of software package installed on the node?,puppet.
9You could also try:
$ cat /etc/issueIt usually (not always, though) will tell you what distribution you are using. /etc/issue is the file used for the login screen.
Kernel: uname -a
cat /etc/os-releaseat a minimum for Ubuntu, Fedora and OpenSUSE.
Does not work for OS X at least until 10.9 (Mavericks). Use sw_vers instead.
OpenSUSE had cat /etc/SuSE-release up until 13.1 but is deprecated in favour of os-release.
Redhat 6.1 has cat /etc/redhat-release
1lsb_release -a, when available, is useful.
cat /proc/version found me Red Hat on a shared VPS.
Kernel: uname -r
Distro: lsb_release -a
These will run on most Linux systems
One-liner
lsb_release -a && uname -r 2 This issue can also be solved using Python with the platform module:
Using platform() function:
python -c 'import platform; print platform.platform()'
# Linux-4.9.0-8-amd64-x86_64-with-debian-9.6The above command returns a single string identifying the underlying platform with as much useful information as possible.
Or using uname() function:
python -c 'import platform; print platform.uname()'
# ('Linux', 'debian', '4.9.0-8-amd64', '#1 SMP Debian 4.9.130-2 (2018-10-27)', 'x86_64', '')The above command returns a namedtuple() containing six attributes: system, node, release, version, machine, and processor.
Or using dist() function:
python -c 'import platform; print platform.dist()'
# ('debian', '9.6', '')The last command tries to determine the name of the Linux OS distribution name, but it is deprecated since Python 3.5 and will be removed in Python 3.8.