Find system name linux

How can I reliably get the operating system’s name?

Say I am logged into a remote system, how can I know what it’s running? On most modern Linuxes (Linuces?), you have the lsb_release command:

$ lsb_release -ic Distributor ID: LinuxMint Codename: debian 

Which as far as I can tell just gives the same info as /etc/lsb-release . What if that file is not present? I seem to recall that the lsb_release command is relatively new so what if I have to get the OS of an older system? In any case, lsb stands for Linux Standard Base so I am assuming it won’t work on non-Linux Unices. As far as I know, there is no way of getting this information from uname so how can I get this on systems that do not use lsb_release ?

unix.stackexchange.com/questions/6345/… for Linux. uname -s should be enough outside Linux (expect possibly for the BSDs).

Have you checked out facter ? facter operatingsystem should do what you want on all the systems facter was made to work with.

I pasted the facter code that gets the operating system name on pastebin. Find it here. It checks many different files to get the name reliably.

@JosephR. wow, that’s a lot of files. I’ll port that to bash when I get the chance, that should be portable enough. Thanks!

11 Answers 11

lsb_release -a is likely going to be your best option for finding this information out, and being able to do so in a consistent way.

History of LSB

The lsb in that command stands for the project Linux Standards Base which is an umbrella project sponsored by the Linux Foundation to provide generic methods for doing basic kinds of things on various Linux distros.

The project is voluntary and vendors can participate within the project as just a user and also as facilitators of the various specifications around different modules that help to drive standardization within the different Linux distributions.

The LSB workgroup has, as its core goal, to address these two concerns. We publish a standard that describes the minimum set of APIs a distribution must support, in consultation with the major distribution vendors. We also provide tests and tools which measure support for the standard, and enable application developers to target the common set. Finally, through our testing work, we seek to prevent unnecessary divergence between the distributions.

Criticisms

There are a number of problems with LSB that make it problematic for distros such as Debian. The forced usage of RPM being one. See the Wikipedia article for more on the matter.

Novell

If you search you’ll possibly come across a fairly dated looking page titled: Detecting Underlying Linux Distro from Novell. This is one of the few places I»ve seen an actual list that shows several of the major distros and how you can detect what underlying one you’re using.

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 

This same page also includes a handy script which attempts to codify for the above using just vanilla uname commands, and the presence of one of the above files.

Читайте также:  Linux command with grep

NOTE: This list is dated but you could easily drop the dated distros such as Mandrake from the list and replace them with alternatives. This type of a script might be one approach if you’re attempting to support a large swath of Solaris & Linux variants.

Linux Mafia

More searching will turn up the following page maintained on Linuxmafia.com, titled: /etc/release equivalents for sundry Linux (and other Unix) distributions. This is probably the most exhaustive list to date that I’ve seen. You could codify this list with a case/switch statement and include it as part of your software distribution.

In fact there is a script at the bottom of that page that does exactly that. So you could simply download and use the script as 3rd party to your software distribution.

#!/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 [ "$" = "SunOS" ] ; then OS=Solaris ARCH=`uname -p` OSSTR="$ $($ `uname -v`)" elif [ "$" = "AIX" ] ; then OSSTR="$ `oslevel` (`oslevel -r`)" elif [ "$" = "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="$[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi OSSTR="$ $ $($ $ $)" fi echo $

NOTE: This script should look familiar, it’s an up to date version of the Novell one!

Legroom script

Another method I’ve seen employed is to roll your own script, similar to the above Novell method but making use of LSB instead. This article titled: Generic Method to Determine Linux (or UNIX) Distribution Name, shows one such method.

# 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 

This chunk of code could be included into a system’s /etc/bashrc or some such file which would then set the environment variable $DISTRO .

Читайте также:  Linux погода в терминале

gcc

Believe it or not another method is to make use of gcc . If you query the command gcc —version you’ll get the distro that gcc was built for, which is invaribly the same as the system it’s running on.

$ gcc --version gcc (GCC) 4.5.1 20100924 (Red Hat 4.5.1-4) Copyright (C) 2010 Free Software Foundation, Inc. 
$ gcc --version gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-54) Copyright (C) 2006 Free Software Foundation, Inc. 
$ gcc --version gcc (GCC) 4.4.7 20120313 (Red Hat 4.4.7-3) Copyright (C) 2010 Free Software Foundation, Inc. 
$ gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 Copyright (C) 2011 Free Software Foundation, Inc. 

TL;DR;

So which one should I use? I would tend to go with lsb_release -a for any Linux distributions that I would frequent (RedHat, Debian, Ubuntu, etc.). For situations where you’re supporting systems that don’t provide lsb_release I’d roll my own as part of the distribution of software that I’m providing, similar to one of the above scripts.

UPDATE #1: Follow-up with SuSE

In speaking with @Nils in the comments below it was determined that for whatever reason, SLES11 appeared to drop LSB from being installed by default. It was only an optional installation, which seemed counter for a package that provides this type of key feature.

So I took the opportunity to contact someone from the OpenSuSE project to get a sense of why.

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

Источник

Читайте также:  Континент ап linux где хранятся сертификаты

How to get the computer name (not its hostname)?

All the results of my searches end up having something to do with hostname or uname -n . I looked up the manual for both, looking for sneaky options, but no luck. I am trying to find an equivalent of OSX’s scutil —get ComputerName on Linux systems. On Mac OS X, the computer name is used as a human-readable identifier for the computer; it’s shown in various management screens (e.g. on inventory management, Bonjour-based remote access, . ) and serves as the default hostname (after filtering to handle spaces etc.).

I might be wrong, but defining the name of the computer as its network name means that it doesn’t have a fixed name, right? When you install a Linux or OSX on a machine, you usually choose a name for that computer (which is the default network name I assume). For example my laptop might be named «FooBar» but when I connect to a network at work I get a hostname such as «machine42.work.localnetwork».

@Sh3ljohn what purpose does computername is osx serve? I don’t think there’s any equivalent in linux system.

Did you give some name other than hostname when installing that computer? Try searching for that name in /etc: grep -ri ‘name’ /etc

@Bibek_G I would like to use this to identify the machine on which I am running from a software of mine. UUID is insufficient because there might be several OS’s installed on the same disk which in turn run on the same machine.

5 Answers 5

The closest equivalent to a human-readable (and human-chosen) name for any computer running Linux is the default hostname stored in /etc/hostname . On some (not all) Linux distributions, this name is entered during installation as the computee’s name (but with network hostname constraints, unlike macOS’s computer name). This can be namespaced, i.e. each UTS namespace can have a different hostname.

Systems running systemd distinguish three different hostnames, including a “pretty” human-readable name which is supposed to be descriptive in a similar fashion to macOS’s computer name; this can be set and retrieved using hostnamectl ’s —pretty option. The other two hostnames are the static hostname, which is the default hostname described above, and the transient hostname which reflects the current network configuration.

Systemd also supports a chassis type (e.g. “tablet”) and an icon for the host; see systemd-hostnamed.service .

But this file is often used to set hostname and uname -n , and @Sh3ljohn explicitly wanted to avoid the output of those two commands.

Источник

Оцените статью
Adblock
detector