Getting linux distribution name

5 Linux Commands to Check Distro Name and Version

When working with an unknown server, the first task of a sys admin is to gather some information about the system, like what OS is it running, what version, what services are running and so on.

And there is no single command that can detect distribution specific information consistently across all linux distributions.

The command is different across Debian, CentOS and ArchLinux.

So in this post we are listing out some common commands that are used to detect distro specific information on linux. This includes the distro name and version.

1. lsb_release

The lsb_release command prints out distribution specific information about a linux distro.

On Ubuntu/debian based systems the command is available by default.

$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.04 Release: 11.04 Codename: natty

The lsb_release command is also available on CentOS/Fedora based systems, if the lsb core packages are installed.

# lsb_release -a LSB Version: :base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch Distributor ID: CentOS Description: CentOS release 6.4 (Final) Release: 6.4 Codename: Final

2. /etc/*-release files

The /etc directory contains a couple of files that contains information about the distribution. The following files are present on Ubuntu/Debian based systems.

/etc/issue /etc/issue.net /etc/lsb-release /etc/os-release
$ cat /etc/issue Ubuntu 13.10 \n \l
$ cat /etc/issue.net Ubuntu 13.10
$ cat /etc/lsb-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=13.10 DISTRIB_CODENAME=saucy DISTRIB_DESCRIPTION="Ubuntu 13.10"
$ cat /etc/os-release NAME="Ubuntu" VERSION="13.10, Saucy Salamander" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 13.10" VERSION_ID="13.10" HOME_URL="http://www.ubuntu.com/" SUPPORT_URL="http://help.ubuntu.com/" BUG_REPORT_URL="http://bugs.launchpad.net/ubuntu/"

The file os-release contains a whole lot of information about the system.

CentOS/Fedora based systems contain similar files but with different names.

/etc/centos-release /etc/lsb-release /etc/redhat-release /etc/system-release
# cat /etc/centos-release CentOS release 6.4 (Final)
# cat /etc/lsb-release LSB_VERSION=base-4.0-amd64:base-4.0-noarch:core-4.0-amd64:core-4.0-noarch:graphics-4.0-amd64:graphics-4.0-noarch:printing-4.0-amd64:printing-4.0-noarch
# cat /etc/redhat-release CentOS release 6.4 (Final)
# cat /etc/system-release CentOS release 6.4 (Final)

Note that the lsb specific command and files are not present on CentOS by default. The redhat-lsb-core packages have to be installed to make lsb available. The /etc/lsb-release file does not print the distro information in a simple format.

Fedora contains the /etc/os-release file, similar to ubuntu

$ cat /etc/os-release NAME=Fedora VERSION="18 (Spherical Cow)" ID=fedora VERSION_ID=18 PRETTY_NAME="Fedora 18 (Spherical Cow)" ANSI_COLOR="0;34" CPE_NAME="cpe:/o:fedoraproject:fedora:18"

3. cat /proc/version

The /proc/version file contains information about the kernel and some indication about the distro.

On a typical Ubuntu system the contents look like this

$ cat /proc/version Linux version 2.6.38-13-generic ([email protected]) (gcc version 4.5.2 (Ubuntu/Linaro 4.5.2-8ubuntu4) ) #52-Ubuntu SMP Tue Nov 8 16:53:51 UTC 2011

On a typical CentOS system the output looks as follows

# cat /proc/version Linux version 2.6.32-358.11.1.el6.x86_64 ([email protected]) (gcc version 4.4.7 20120313 (Red Hat 4.4.7-3) (GCC) ) #1 SMP Wed Jun 12 03:34:52 UTC 2013

As can be seen above, the version information about the distro is not very clear, although it might be possible to deduce the distro in use.

Читайте также:  Uninstall linux install windows

Output on a RHEL 5 system

# cat /proc/version Linux version 2.6.18-028stab070.14 ([email protected]) (gcc version 4.1.2 20080704 (Red Hat 4.1.2-46)) #1 SMP Thu Nov 18 16:04:02 MSK 2010

4. uname -a

The uname command can also indicate which linux distro is in use, but gives very little information about it.

On Ubuntu, uname can clearly indicate the distribution name.

$ uname -a Linux enlightened-desktop 2.6.38-13-generic #52-Ubuntu SMP Tue Nov 8 16:53:51 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux

However on CentOS, the exact distro name is not revealed.

# uname -a Linux dhcppc3 2.6.32-358.11.1.el6.x86_64 #1 SMP Wed Jun 12 03:34:52 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux

Instead it reports the base distro name EL (Redhat).

Portable command

The following is an attempt to get a portable command for checking distro info across different linux systems.

$ cat /etc/[A-Za-z]*[_-][rv]e[lr]* squeeze/sid DISTRIB_ID=Ubuntu DISTRIB_RELEASE=11.04 DISTRIB_CODENAME=natty DISTRIB_DESCRIPTION="Ubuntu 11.04"

A simpler approach to make a portable command would be like this

$ lsb_release -a || cat /etc/redhat-release || cat /etc/*-release || cat /etc/issue

If one option fails, the command moves to the next one, until one of them works. The above command is not a well tested one and is expected to work fine only on ubuntu/debian and centos/fedora based systems.

That would print all unique lines from all /etc/*-release files. Works well on most distros.

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected] .

One Comment

  1. Tony August 10, 2021 at 10:10 pm Hi, Nice article.
    I personaly use this command in one of my scripts: (lsb_release -a 2>/dev/null || cat /etc/redhat-release || cat /etc/*-release || cat /etc/issue) | grep -i “centos\|ubuntu\|red *hat\|fedora\|debian\|slackware\|suse\|arch\|deepin\|kubuntu\|alpine” -o | sort -f | uniq -i -u It’s not getting all the Distributions out there, but enough for my use. Cheers,
    Tony

Источник

How to Find Linux OS Name and Kernel Version You Are Running

There are several ways of knowing the version of Linux you are running on your machine as well as your distribution name and kernel version plus some extra information that you may probably want to have in mind or at your fingertips.

Therefore, in this simple yet important guide for new Linux users, I will show you how to find out your Linux system OS version from the command line. Doing this may seem to be a relatively easy task.

However, having a good knowledge of your system is always a recommended practice for a good number of reasons including installing and running the appropriate packages for your Linux version, for easy reporting of bugs coupled with many more.

Читайте также:  Linux ubuntu сменить пароль root

With that said, let us proceed to how you can figure out information about your Linux distribution.

Find Linux Kernel Version Using uname Command

We will use the uname command, which is used to print your Linux system information such as kernel version and release name, network hostname, machine hardware name, processor architecture, hardware platform, and the operating system.

To find out which version of Linux kernel you are running, type:

In the preceding command, the option -o prints the operating system name, and -r prints the kernel release version.

You can also use -a option with uname command to print all system information as shown:

Check Linux Kernel Version

Find Linux OS Info Using /proc/version File

Next, we will use /proc file system, which stores information about processes and other system information, it’s mapped to /proc and mounted at boot time.

Simply type the command below to display some of your system information including the Linux kernel version:

Show Linux System Information

From the image above, you have the following information:

  • A version of the Linux (kernel) you are running: Linux version 5.15.0-53-generic
  • Name of the user who compiled your kernel: [email protected]
  • A version of the GCC compiler used for building the kernel: gcc version 20.04.1
  • Type of the kernel: #1 SMP (Symmetric MultiProcessing kernel) supports systems with multiple CPUs or multiple CPU cores.
  • Date and time when the kernel was built: Thu Oct 20 15:10:22 UTC 2022

Find the Linux Distribution Name and Release Version

The best way to determine a Linux distribution name and release version information is by using the cat /etc/os-release command, which works on almost all Linux systems.

/etc/os-release file

$ cat /etc/os-release [On Debian, Ubuntu and Mint] $ cat /etc/os-release [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ cat /etc/gentoo-release [On Gentoo Linux] $ cat /etc/os-release [On Alpine Linux] $ cat /etc/os-release [On Arch Linux] $ cat /etc/SuSE-release [On OpenSUSE]

Find Linux Distribution Name and Release

lsb_release Command

Alternatively, you can also use the lsb_release tool, which will print LSB (Linux Standard Base) information about the Linux distribution on your terminal. The lsb_release command is not installed by default, you need to install it using your default package manager as shown.

$ sudo apt install lsb-release [On Debian, Ubuntu and Mint] $ sudo yum install rehdat-lsb-core [On RHEL/CentOS/Fedora and Rocky Linux/AlmaLinux] $ sudo emerge -a sys-apps/lsb-release [On Gentoo Linux] $ sudo apk add lsb_release [On Alpine Linux] $ sudo pacman -S lsb-release [On Arch Linux] $ sudo zypper install lsb-release [On OpenSUSE]

Once installed, run the lsb_release utility to print the standard Linux system information as shown.

Find Linux OS Information

hostnamectl Command

The hostnamectl command is a systemd utility that is used to get the Linux operating system information and also used to change or set the system hostname.

Check Linux System Info

I’ve used the tmux terminal multiplexer for accessing multiple Linux terminal sessions simultaneously in a single terminal window.

In this article, we walked through a brief and simple guide intended to help new Linux users find out the Linux version they are running and also get to know their Linux distribution name and version from the shell prompt.

Читайте также:  Kali linux ранние версии

Perhaps it can also be useful to advanced users on one or two occasions. Lastly, to reach us for any assistance or suggestions you wish to offer, make use of the feedback form below.

Источник

How to get Linux distribution name and version?

You might want to edit the title to make it clear the question is about doing it from C source, not as a script writer or user at the command line.

@Kolob Those were most emphatically not «more sensible» for this question; I have rolled back your change.

9 Answers 9

+1 this finally was «standardized» among the distro’s. Go back far enough though and this file doesn’t exist, instead each distro put their own file like /etc/redhat-release.

This will only work on LSB compliant Linux distributions, but is not guaranteed to work on non-compliant distributions. OTOH, it will also work on other LSB compliant non-Linux Unices. E.g. I’m pretty sure it won’t work on Adroid.

Note that on e.g. Gentoo Linux lsb_release is not always present by default. I just checked, and it’s provided by an optional package sys-apps/lsb-release, currently not installed on my system.

Will lsb-release works on all the follow Distrubtions?: Debian / Ubuntu | Red Hat Enterprise / Fedora Linux / Suse Linux / Cent OS ?

on my system yields the following from the bash (terminal) prompt:

Ubuntu 10.04.4 LTS 2.6.32-41-generic x86_64 

I believe uname -mr returns the version of the Linux Kernel, so ‘lsb_release -ds’ should be all you need for the release name and version, assuming the description format is consistent across releases. Thanks, I was wondering how you were supposed to use the short parameter, I was trying it ‘lsb_release -s’ and was wondering why it was failing. Cheers!

trying this way is an interesting one and less restrictive than lsb-release.

This is the best answer, to only retrieve the name of the distro one can do: cat /etc/*-release | grep ID | head -n1 | cut -d ‘=’ -f2

What’s the purpose of getting that information?

If you’re trying to detect some features or properties of the system (e.g. does it support some syscall or does it have some library), instead of relying on output of lsb_release you should either:

  • try to use given features and fail gracefully (e.g. dlopen for libraries, syscall(2) for syscalls and so on)
  • make it a part of your ./configure check if applicable (standard FOSS way of automatically recognizing system features/properties)

Note that the first way above applies even if your software is binary-only.

 dl = dlopen(module_path, RTLD_LAZY); if (!dl) < fprintf(stderr, "Failed to open module: %s\n", module_path); return; >funcptr = dlsym(dl, module_function); if (!funcptr) < fprintf(stderr, "Failed to find symbol: %s\n", module_function); return; >funcptr(); dlclose(dl); 

Источник

Оцените статью
Adblock
detector