如何获得Linux发行版的名称和版本?

时间:2009-08-24 03:48:08

标签: c++ c linux api

在Windows中,我读取了注册表项SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProductName以获取操作系统的全名和版本。

但在Linux中,代码

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

返回字符串linux,而不是Ubuntu 9.04

如何获取Linux发行版名称和版本?

8 个答案:

答案 0 :(得分:32)

尝试:

cat /etc/lsb-release

您也可以尝试

lsb_release -a

或者:

cat /proc/version

答案 1 :(得分:5)

lsb_release -ds ; uname -mr
我的系统上的

从bash(终端)提示符中产生以下内容:

Ubuntu 10.04.4 LTS
2.6.32-41-generic x86_64

答案 2 :(得分:4)

不确定我是否完全遵循了您的追求,但我认为您只想在uname上使用“all”标志:

uname -a

答案 3 :(得分:4)

获取该信息的目的是什么?

如果您正在尝试检测系统的某些功能或属性(例如,它是否支持某些系统调用或是否有某些库),而不是依赖于lsb_release的输出,您应该:

  • 尝试使用给定的功能并优雅地失败(例如dlopen for libraries,syscall(2)for syscalls等)
  • 将其作为./configure检查的一部分(如果适用)(自动识别系统功能/属性的标准FOSS方式)

请注意,即使您的软件仅为二进制文件,上述第一种方法也适用。

一些代码示例:

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

您甚至可以优雅地测试CPU操作码支持,例如http://neugierig.org/software/chromium/notes/2009/12/flash-lahf.htmlhttp://code.google.com/p/chromium/issues/detail?id=29789

答案 4 :(得分:3)

尝试这种方式是一个有趣的方法,并且比lsb-release更少限制。

$ cat /etc/*-release

答案 5 :(得分:2)

一般:

cat /etc/issue

答案 6 :(得分:1)

/etc/os-release至少在CentOS 7和Ubuntu 16.04上都可用,这使得它比lsb_release(不在CentOS上)或/etc/system-release(不在Ubuntu上)更具跨平台性。

$ cat /etc/os-release

示例:

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/"

答案 7 :(得分:1)

  1. cat发行文件以显示Linux发行版

    $ cat /etc/*-release
    
  2. lsb_release 将返回Linux发行版名称和版本

    $ lsb_release -a 
    
  3. hostnamectl 将返回Linux发行版名称和版本

    $ hostnamectl
    
  4. 打印某些系统信息

    $ 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. 要查找静态主机名,机箱,机器ID,虚拟化,操作系统,内核,体系结构

    $ cat /proc/version
    
相关问题