【问题标题】:Need to get Linux Dist name from different Dists需要从不同的 Dist 获取 Linux Dist 名称
【发布时间】:2017-10-14 21:22:01
【问题描述】:

想要编写一个适用于多个 linux 发行版的脚本。 还想添加检查命令,如果不存在,请安装一些软件包。因此,需要一个解决方案来检查 Dist 名称(如 Debian 或 RedHat),以便为该 Dist 执行正确的命令。 我已经尝试过“lsb_release”,但这是否适用于所有后续版本? :Debian / Ubuntu | Red Hat / Fedora / Suse / Cent OS" 如果是的话,知道我可以从上面的所有 Dist 中获得lsb_release 的输出吗?

在 Debian 中,我从 lsb_release -a 2>/dev/null | grep Distributor 得到这个:

Distributor ID: Debian

在 Rasberry,我明白了:

Distributor ID: Raspbian

但是其他发行版呢?

【问题讨论】:

  • 你试过cat /proc/version吗?
  • @dps 这给出了内核的版本,它与发行版名称无关。 (在某些情况下,它甚至可能明显不同步——例如,如果机器在某些虚拟化环境中运行。)
  • @dps /proc/version 没有显示分布。 @duskwuff 已经写好了,它只是内核版本。但我需要 Dist 名称.. 认为 lsb_release cmd 是标准的 linux cmd,适用于所有 Dist,但我不确定
  • 很难开发出适用于所有这些发行版的东西。我认为您最好的选择是开发 2-3 个涵盖所有发行版的测试,并在您的脚本上“测试”每个关键字测试,例如 if [ -z "$myTest" ]; then... 和/或 echo "$myTest" | grep -i debian ...
  • 听起来您真的需要一个自动化解决方案。

标签: linux ubuntu centos debian redhat


【解决方案1】:

似乎所有现代 linux 发行版都具有 ID=distribution 的 /etc/os-release 文件(不幸的是没有 Raspberry 可供测试)。

【讨论】:

  • RASBERRY:PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)" NAME="Raspbian GNU/Linux" VERSION_ID="8" VERSION="8 (jessie)" ID=raspbian ID_LIKE=debian HOME_URL="http://www.raspbian.org/" SUPPORT_URL="http://www.raspbian.org/RaspbianForums" BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"
  • ID_LIKE=debian 很有趣......不幸的是,我没有安装任何 redhat/centod 等 linux 来测试它......我认为在 Ubuntu 上应该获得与 Rasbian = debian 中相同的 ID_LIKE
  • NAME="CentOS Linux" VERSION="7 (Core)" ID="centos" ID_LIKE="rhel fedora" VERSION_ID="7" PRETTY_NAME="CentOS Linux 7 (Core)" ANSI_COLOR= "0;31" CPE_NAME="cpe:/o:centos:centos:7" HOME_URL="centos.org" BUG_REPORT_URL="bugs.centos.org" CENTOS_MANTISBT_PROJECT="CENTOS-7" CENTOS_MANTISBT_PROJECT_VERSION="7" REDHAT_SUPPORT_PRODUCT="centos" REDHAT_SUPPORT_PRODUCT_VERSION="7"
  • 感谢支持!我看到ID_LIKE="rhel fedora" 听起来像 ReadHat “dist”...意思是,fedora 是基于 RedHat 的?
  • “Fedora 项目成立于 2003 年 9 月 22 日,当时 Red Hat 决定将 Red Hat Linux 拆分为 Red Hat Enterprise Linux (RHEL) 和基于社区的操作系统 Fedora”link跨度>
【解决方案2】:

是的,lsb_release 应该在安装所需的软件包后打印分发信息。

e,g:

基于 debian 的发行版:

apt-get install lsb-release

在基于 RHEL 的发行版上:

yum install redhat-lsb-core

yum install redhat-lsb

替代方法:您可以通过以下方式获取分布信息:

基于 debian 的 disto

cat /proc/version
cat /etc/issue
cat /etc/issue.net
cat /etc/lsb-release
cat /etc/os-release

在基于 RHEL 的发行版上:

cat /proc/version
cat /etc/centos-release
cat /etc/lsb-release
cat /etc/redhat-release
cat /etc/system-release

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 2015-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-11
    • 1970-01-01
    相关资源
    最近更新 更多