假设我登录到远程系统,我怎么知道它正在运行什么?在大多数现代 Linux(Linux?)上,您有以下lsb_release命令:
$ lsb_release -ic
Distributor ID: LinuxMint
Codename: debian
Run Code Online (Sandbox Code Playgroud)
据我所知,它提供的信息与/etc/lsb-release. 如果该文件不存在怎么办?我似乎记得该lsb_release命令相对较新,所以如果我必须获得旧系统的操作系统怎么办?
在任何情况下,lsb代表Linux Standard Base所以我假设它不会在非 Linux Unices 上工作。据我所知,无法从中获取此信息,uname那么如何在不使用 的系统上获取此信息lsb_release?
slm*_*slm 83
lsb_release -a 可能是您找出这些信息并能够以一致的方式进行的最佳选择。
该lsb命令中的 代表项目Linux Standards Base,这是一个由Linux 基金会赞助的伞形项目,提供在各种 Linux 发行版上执行基本类型操作的通用方法。
该项目是自愿的,供应商可以仅作为用户参与该项目,也可以作为围绕不同模块的各种规范的推动者,这些规范有助于推动不同 Linux 发行版中的标准化。
宪章节选
LSB 工作组的核心目标是解决这两个问题。我们发布了一个标准,该标准描述了发行版必须支持的最小 API 集,并与主要发行版供应商协商。我们还提供测试和工具来衡量对标准的支持,并使应用程序开发人员能够针对通用集。最后,通过我们的测试工作,我们试图防止分布之间出现不必要的差异。
LSB 存在许多问题,使其对 Debian 等发行版产生问题。RPM 的强制使用是其中之一。有关此问题的更多信息,请参阅Wikipedia 文章。
如果您进行搜索,您可能会看到一个相当陈旧的页面,标题为:从 Novell检测底层 Linux 发行版。这是我见过的实际列表中的少数几个地方之一,其中显示了几个主要发行版以及如何检测您正在使用的底层发行版。
摘抄
Novell SUSE /etc/SUSE-release
Red Hat /etc/redhat-release, /etc/redhat_version
Fedora /etc/fedora-release
Slackware /etc/slackware-release, /etc/slackware-version
Debian /etc/debian_release, /etc/debian_version,
Mandrake /etc/mandrake-release
Yellow dog /etc/yellowdog-release
Sun JDS /etc/sun-release
Solaris/Sparc /etc/release
Gentoo /etc/gentoo-release
UnitedLinux /etc/UnitedLinux-release
ubuntu /etc/lsb-release
Run Code Online (Sandbox Code Playgroud)
同一页面还包含一个方便的脚本,该脚本尝试仅使用普通uname命令以及上述文件之一的存在来对上述内容进行编码。
注意:此列表已过时,但您可以轻松地从列表中删除过时的发行版,例如 Mandrake,并用替代品替换它们。如果您试图支持大量 Solaris 和 Linux 变体,这种类型的脚本可能是一种方法。
更多搜索将出现在 Linuxmafia.com 上维护的以下页面,标题为: /etc/release 等价于各种 Linux(和其他 Unix)发行版。这可能是迄今为止我见过的最详尽的清单。您可以使用 case/switch 语句将此列表编入代码,并将其作为软件分发的一部分。
事实上,该页面底部有一个脚本就是这样做的。因此,您可以简单地下载脚本并将其用作软件分发的第 3 方。
脚本
#!/bin/sh
# Detects which OS and if it is Linux then it will detect which Linux
# Distribution.
OS=`uname -s`
REV=`uname -r`
MACH=`uname -m`
GetVersionFromFile()
{
VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // `
}
if [ "${OS}" = "SunOS" ] ; then
OS=Solaris
ARCH=`uname -p`
OSSTR="${OS} ${REV}(${ARCH} `uname -v`)"
elif [ "${OS}" = "AIX" ] ; then
OSSTR="${OS} `oslevel` (`oslevel -r`)"
elif [ "${OS}" = "Linux" ] ; then
KERNEL=`uname -r`
if [ -f /etc/redhat-release ] ; then
DIST='RedHat'
PSUEDONAME=`cat /etc/redhat-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/redhat-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/SuSE-release ] ; then
DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//`
REV=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //`
elif [ -f /etc/mandrake-release ] ; then
DIST='Mandrake'
PSUEDONAME=`cat /etc/mandrake-release | sed s/.*\(// | sed s/\)//`
REV=`cat /etc/mandrake-release | sed s/.*release\ // | sed s/\ .*//`
elif [ -f /etc/debian_version ] ; then
DIST="Debian `cat /etc/debian_version`"
REV=""
fi
if [ -f /etc/UnitedLinux-release ] ; then
DIST="${DIST}[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]"
fi
OSSTR="${OS} ${DIST} ${REV}(${PSUEDONAME} ${KERNEL} ${MACH})"
fi
echo ${OSSTR}
Run Code Online (Sandbox Code Playgroud)
注意:这个脚本应该看起来很熟悉,它是 Novell 脚本的最新版本!
我见过的另一种方法是滚动你自己的脚本,类似于上面的 Novell 方法,但使用 LSB。这篇题为:确定 Linux(或 UNIX)分发名称的通用方法的文章展示了一种这样的方法。
# Determine OS platform
UNAME=$(uname | tr "[:upper:]" "[:lower:]")
# If Linux, try to determine specific distribution
if [ "$UNAME" == "linux" ]; then
# If available, use LSB to identify distribution
if [ -f /etc/lsb-release -o -d /etc/lsb-release.d ]; then
export DISTRO=$(lsb_release -i | cut -d: -f2 | sed s/'^\t'//)
# Otherwise, use release info file
else
export DISTRO=$(ls -d /etc/[A-Za-z]*[_-][rv]e[lr]* | grep -v "lsb" | cut -d'/' -f3 | cut -d'-' -f1 | cut -d'_' -f1)
fi
fi
# For everything else (or if above failed), just use generic identifier
[ "$DISTRO" == "" ] && export DISTRO=$UNAME
unset UNAME
Run Code Online (Sandbox Code Playgroud)
这段代码可以包含在系统/etc/bashrc或某些此类文件中,然后将设置环境变量$DISTRO。
信不信由你,另一种方法是利用gcc. 如果您查询该命令,gcc --version您将获得 gcc 为其构建的发行版,该发行版始终与其运行的系统相同。
软呢帽 14
$ gcc --version
gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4)
Copyright (C) 2010 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)
CentOS 5.x
$ gcc --version
gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54)
Copyright (C) 2006 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)
CentOS 6.x
$ gcc --version
gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3)
Copyright (C) 2010 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)
Ubuntu 12.04
$ gcc --version
gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3
Copyright (C) 2011 Free Software Foundation, Inc.
Run Code Online (Sandbox Code Playgroud)
那么我应该使用哪一种呢?我倾向于使用lsb_release -a我经常使用的任何 Linux 发行版(RedHat、Debian、Ubuntu 等)。对于您支持不提供的系统的情况,lsb_release我将自己推出作为我提供的软件分发的一部分,类似于上述脚本之一。
在下面的评论中与@Nils 交谈时,确定无论出于何种原因,SLES11 似乎都默认不安装 LSB。这只是一个可选安装,这似乎与提供此类关键功能的软件包背道而驰。
所以我借此机会联系了 OpenSuSE 项目的某个人,以了解原因。
电子邮件摘录
Hi Rob,
I hope you don't mind me contacting you directly but I found your info here:
https://en.opensuse.org/User:Rjschwei. I participate on one of the StackExchange
sites, Unix & Linux and a question recently came up regarding the best option
for determining the underlying OS.
http://unix.stackexchange.com/questions/92199/how-can-i-reliably-get-the-operating-systems-name/92218?noredirect=1#comment140840_92218
In my answer I suggested using lsb_release, but one of the other users mentioned
that this command wasn't installed as part of SLES11 which kind of surprised me.
Anyway we were looking for some way to confirm whether this was intentionally
dropped from SLES or it was accidental.
Would you know how we could go about confirming this one way or another?
Thanks for reading this, appreciate any help and/or guidance on this.
-Sam Mingolelli
http://unix.stackexchange.com/users/7453/slm
Run Code Online (Sandbox Code Playgroud)
这是罗布的回应
Hi,
On 10/01/2013 09:31 AM, Sam Mingo wrote:
- show quoted text -
lsb_release was not dropped in SLES 11. SLES 11 is LSB certified. However, it
is not installed by default, which is consistent with pretty much every other
distribution. The lsb_release command is part of the lsb-release package.
At present almost every distribution has an entry in /etc such as
/etc/SuSE-release for SLES and openSUSE. Since this is difficult for ISVs and
others there is a standardization effort going on driven by the convergence to
systemd. The standard location for distribution information in the future will
be /etc/os-release, although Ubuntu will probably do something different.
HTH,
Robert
-- Robert Schweikert MAY THE SOURCE BE WITH YOU
SUSE-IBM Software Integration Center LINUX
Tech Lead
Public Cloud Architect
Run Code Online (Sandbox Code Playgroud)
Jos*_* R. 17
由于您可能无法facter在远程服务器上安装,您可以模仿它的作用来查找操作系统名称。operatingsystem可以在 pastebin上找到该事实的 Ruby 代码。基本上,它查看不同的*-release文件和其他文件以确定操作系统名称。
它查看的一些文件:
/etc/debian_version
/etc/gentoo-release
/etc/fedora-release
/etc/mandriva-release
/etc/mandrake-release
/etc/meego-release
/etc/arch-release
/etc/oracle-release
/etc/enterprise-release
/etc/ovs-release
/etc/vmware-release
/etc/redhat-release
/etc/SuSE-release
/etc/bluewhite64-version
/etc/slamd64-version
/etc/slackware-version
/etc/alpine-release
/etc/system-release
/etc/centos-release
Run Code Online (Sandbox Code Playgroud)
如果您在此列表中发现重复项,我很抱歉,我使用grep. 将其移植到 POSIX shell 脚本应该相当容易(虽然有点乏味)。
hei*_*991 10
如果您已经python安装(无论是 Python 3 还是 Python 2),您都可以找到发行版名称而无需重新发明轮子:
python -c "import platform;print(platform.linux_distribution()[0])"
Run Code Online (Sandbox Code Playgroud)
小智 6
/etc/issue应包含发布信息。我相当确定我已经在 Solaris 系统上看到过它。这是来自现代 Debian 系统的文件:
$ cat /etc/issue
Debian GNU/Linux 7 \n \l
$
Run Code Online (Sandbox Code Playgroud)
FHS 中也提到了 /etc/issue (它不仅适用于 Linux 系统),尽管它是“可选的”。
| 归档时间: |
|
| 查看次数: |
83942 次 |
| 最近记录: |