Andrew Tomazos
Andrew Tomazos

Reputation: 68638

Programmatically determining Ubuntu distribution and architecture?

What's the best way to programmatically determine if the current machine is running Ubuntu, what architecture it has, and what version of Ubuntu it is running?

One way would be to scan the output of uname -a to check for the right kernel version and architecture.

Another way would be to scan /etc/apt/sources.list for the Ubuntu distribution keyword (eq precise, quantal, etc)

Is there a better way?

Upvotes: 0

Views: 1488

Answers (4)

Shiplu Mokaddim
Shiplu Mokaddim

Reputation: 57650

Apart from uname -a, There are several way to get information about the current distribution.

  1. The best way is to parse the release files. They usually ended with -release or _release and located in /etc. Following command will find them all.

    ls /etc/*{-,_}release
    
    • Ubuntu uses lsb_release
    • Redhat/Fedora uses redhat-release
    • Slackware uses slackware-release
    • Gentoo uses gentoo-release

Debian's corresponding file is /etc/debian_version. This file will also (somewhat misleadingly, but for a good reason) be present on Ubuntu systems, though.

  1. Another file is /etc/issue which is used for machine identification and pre-login prompt can be used to determine current distribution information.

  2. System information can be found in /proc/version too.

    cat /proc/version
    

Upvotes: 4

Mihai8
Mihai8

Reputation: 3147

You can use a library as a neutral to the operating system. A solution is lsband your question became close to using lsb question.

Upvotes: 2

jgr
jgr

Reputation: 3974

Afaik most Linux distributions also use /etc/issue. The text in it can ofcourse have been changed by the admin to show a different login message.

Sample from fedora:

Fedora release 17 (Beefy Miracle)
Kernel \r on an \m (\l)

Sample from ubuntu:

Ubuntu 11.04 \n \l

Upvotes: 0

user529758
user529758

Reputation:

One way would be to scan the output of uname -a to check for the right kernel version and architecture.

But one does not generally want to parse the output of such tools, because it's not elegant (it's considered a hack, so to say).

However, you can use the uname() function/syscall:

#include <sys/utsname.h>

struct utsname sysinfo;
if (uname(&sysinfo) < 0) {
    printf("Cannot determine OS\n");
    exit(-1);
}

printf("Operating system name: %s\n", sysinfo.sysname);

Upvotes: 3

Related Questions