19

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?

tripleee
  • 175,061
  • 34
  • 275
  • 318
Dmitriy
  • 3,305
  • 7
  • 44
  • 55

9 Answers9

40

Try:

cat /etc/lsb-release

You can also try

lsb_release -a

Or:

cat /proc/version
thekidder
  • 3,486
  • 1
  • 33
  • 36
  • 6
    +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. – KFro Aug 24 '09 at 04:23
  • 5
    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. – Jörg W Mittag Aug 24 '09 at 10:35
  • 1
    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. – Paweł Hajdan Dec 28 '11 at 17:19
  • Will `lsb-release` works on all the follow Distrubtions?: Debian / Ubuntu | Red Hat Enterprise / Fedora Linux / Suse Linux / Cent OS ? – Chris May 15 '17 at 23:51
  • 1
    `lsb-release` does *not* exist on CentOS 7.4. – phoenix Jan 04 '18 at 13:58
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
mda
  • 1,158
  • 14
  • 18
  • 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! – leetNightshade Aug 02 '12 at 22:47
7

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

$ cat /etc/*-release
ollie314
  • 440
  • 1
  • 10
  • 19
  • 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` – jaques-sam Apr 05 '23 at 12:16
4

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

uname -a
Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
Shaun
  • 3,928
  • 22
  • 21
  • 3
    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/Linux – Dmitriy Aug 24 '09 at 03:59
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/"
phoenix
  • 7,988
  • 6
  • 39
  • 45
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

Paweł Hajdan
  • 18,074
  • 9
  • 49
  • 65
3

Usually:

cat /etc/issue
tchen
  • 2,212
  • 1
  • 16
  • 18
  • 3
    /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. – Chris Huang-Leaver Aug 24 '09 at 03:59
  • This gives `Kernel \r on an \m (\l)` on RedHat's distros indeed. /etc/*release is better! – jaques-sam Apr 05 '23 at 12:16
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
    
Girdhar Singh Rathore
  • 5,030
  • 7
  • 49
  • 67
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
jaques-sam
  • 2,578
  • 1
  • 26
  • 24