How to get Linux distribution name and version?
Asked Answered
S

9

20

In Windows I read the registry key SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName to get the full name and version of the OS.

But in Linux, the code

struct utsname ver;
uname(&ver);
retVal = ver.sysname;

returns the string linux, not Ubuntu 9.04.

How can I get the Linux distribution name and version?

Stenopetalous answered 24/8, 2009 at 3:48 Comment(5)
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.Synergistic
@DarenW: the question has tags C/C++Stenopetalous
See also #264790Kirkwall
@Stenopetalous I've updated it to have more sensible tagsFellmonger
@Kolob Those were most emphatically not "more sensible" for this question; I have rolled back your change.Kirkwall
T
41

Try:

cat /etc/lsb-release

You can also try

lsb_release -a

Or:

cat /proc/version
Tortuous answered 24/8, 2009 at 3:54 Comment(5)
+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.Muth
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.Algophobia
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.Byrann
Will lsb-release works on all the follow Distrubtions?: Debian / Ubuntu | Red Hat Enterprise / Fedora Linux / Suse Linux / Cent OS ?Renaldorenard
lsb-release does not exist on CentOS 7.4.Coad
F
9
lsb_release -ds ; uname -mr

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

Ubuntu 10.04.4 LTS
2.6.32-41-generic x86_64
Fluting answered 25/6, 2012 at 23:2 Comment(1)
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!Batish
F
9

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

$ cat /etc/*-release
Feature answered 27/11, 2013 at 9:59 Comment(1)
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 '=' -f2Wembley
M
4

Not sure I followed exactly what you're after but I think you just want the "all" flag on uname:

uname -a
Mysticism answered 24/8, 2009 at 3:51 Comment(1)
Unfortunately, on Fedora 11 output of "uname -a" if Linux localhost.localdomain 2.6.29.6-217.2.8.fc11.i586 #1 SMP Sat Aug 15 00:44:39 EDT 2009 i686 i686 i386 GNU/LinuxStenopetalous
M
4

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.

Some code examples:

  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);

You can even gracefully test for CPU opcodes support, read e.g. http://neugierig.org/software/chromium/notes/2009/12/flash-lahf.html , http://code.google.com/p/chromium/issues/detail?id=29789

Mezuzah answered 28/12, 2011 at 17:28 Comment(0)
C
4

/etc/os-release is available on at least both CentOS 7 and Ubuntu 16.04, which makes it more cross-platform than lsb_release (not on CentOS) or /etc/system-release (not on Ubuntu).

$ cat /etc/os-release

Example:

NAME=Fedora
VERSION="17 (Beefy Miracle)"
ID=fedora
VERSION_ID=17
PRETTY_NAME="Fedora 17 (Beefy Miracle)"
ANSI_COLOR="0;34"
CPE_NAME="cpe:/o:fedoraproject:fedora:17"
HOME_URL="https://fedoraproject.org/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
Coad answered 4/1, 2018 at 18:18 Comment(0)
R
3

Usually:

cat /etc/issue
Respectable answered 24/8, 2009 at 3:50 Comment(2)
/etc/issue is what is displayed on a terminal before you log in. Reading the file itself would normally give you something like "This is \n.\O (\s \m \r) \t ". Not very useful! It's a piece of freeform text which could contain anything in other words.Shorten
This gives Kernel \r on an \m (\l) on RedHat's distros indeed. /etc/*release is better!Wembley
S
2
  1. cat release file to display Linux distro version

    $ cat /etc/*-release
    
  2. lsb_release will return Linux distribution name and version

    $ lsb_release -a 
    
  3. hostnamectl will return Linux distribution name and version

    $ hostnamectl
    
  4. To print certain system information

    $ uname -a
    or 
      -s, --kernel-name        print the kernel name
      -n, --nodename           print the network node hostname
      -r, --kernel-release     print the kernel release
      -v, --kernel-version     print the kernel version
      -m, --machine            print the machine hardware name
      -p, --processor          print the processor type (non-portable)
      -i, --hardware-platform  print the hardware platform (non-portable)
      -o, --operating-system   print the operating system
    
  5. To find out Static hostname, Chassis, Mchine ID, Virtualization, OS, Kernel, Architecture

    $ cat /proc/version
    
Staghound answered 4/12, 2020 at 7:41 Comment(0)
W
1

This command filters just one line where the name of the distro is kept:

cat /etc/*-release | grep ID | head -n1 | cut -d '=' -f2
Wembley answered 5/4, 2023 at 12:17 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.