How to get linux distro name

Best way to find the OS name and version on a Unix/Linux platform

But it does not seem to be the best solution, as LSB_RELEASE support is no longer for RHEL 7.

Is there a way that will work on any Unix or Linux platform?

uname is in most unix environments and guaranteed to be on every LSB compliant linux distro: refspecs.linuxfoundation.org/LSB_2.0.1/LSB-Core/LSB-Core/…

@Niraj — By reading the manpage linux.die.net/man/1/uname and grokking its output (assuming it is supported in RH6.5) . either way there is no (single) portable way to get this because it is mostly irrelevant info. Portable programs should probe for required features, not use some whitelist of prechecked distros.

10 Answers 10

This work fine for all Linux environments.

$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=10.04 DISTRIB_CODENAME=lucid DISTRIB_DESCRIPTION="Ubuntu 10.04.4 LTS" 
$ cat /etc/*-release DISTRIB_ID=Ubuntu DISTRIB_RELEASE=12.04 DISTRIB_CODENAME=precise DISTRIB_DESCRIPTION="Ubuntu 12.04.4 LTS" NAME="Ubuntu" VERSION="12.04.4 LTS, Precise Pangolin" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu precise (12.04.4 LTS)" VERSION_ID="12.04" 
$ cat /etc/*-release Red Hat Enterprise Linux Server release 6.5 (Santiago) Red Hat Enterprise Linux Server release 6.5 (Santiago) 
#!/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 $

The script is useful but for linux it is showing ==Linux RedHat version(Final 2.6.32-431.el6.x86_64 x86_64) .my redhat version is 6.5 but it is not showing in output ?

I tested on RHEL6.3 It is showing output as Linux RedHat 6.3(Santiago 2.6.32-279.22.1.el6.x86_64 x86_64)

Following command worked out for me nicely. It gives you the OS name and version.

The «lsb_release» command provides a certain Linux Standard Base (LSB) and distribution-specific information.

So using the below command we can get the Operating system name and operating system version.

«lsb_release -a«

This command gives you a description of your operating system:

Читайте также:  Vmware horizon client 64 bit linux

In every distribution, it has different files, so I list the most common ones:

---- CentOS Linux distribution `cat /proc/version` ---- Debian Linux distribution `cat /etc/debian_version` ---- Redhat Linux distribution `cat /etc/redhat-release` ---- Ubuntu Linux distribution `cat /etc/issue` or `cat /etc/lsb-release` 

In the last one, /etc/issue didn’t exist, so I tried the second one and it returned the right answer.

With quotes:

cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' 

Without quotes:

cat /etc/*-release | grep "PRETTY_NAME" | sed 's/PRETTY_NAME=//g' | sed 's/"//g' 

@PeterMortensen in /etc/*-release file original value is stored with double quotes. if you want to save it in database most probably you would want to remove those double quotes as well. 🙂

My own take at @kvivek’s script, with more easily machine parsable output:

#!/bin/sh # Outputs OS Name, Version & misc. info in a machine-readable way. # See also NeoFetch for a more professional and elaborate bash script: # https://github.com/dylanaraps/neofetch SEP="," PRINT_HEADER=false print_help() < echo "`basename $0` - Outputs OS Name, Version & misc. info" echo "in a machine-readable way." echo echo "Usage:" echo " `basename $0` [OPTIONS]" echo "Options:" echo " -h, --help print this help message" echo " -n, --names print a header line, naming the fields" echo " -s, --separator SEP overrides the default field-separator ('$SEP') with the supplied one" ># parse command-line args while [ $# -gt 0 ] do arg="$1" shift # past switch case "$" in -h|--help) print_help exit 0 ;; -n|--names) PRINT_HEADER=true ;; -s|--separator) SEP="$1" shift # past value ;; *) # non-/unknown option echo "Unknown switch '$arg'" >&2 print_help ;; esac done OS=`uname -s` DIST="N/A" REV=`uname -r` MACH=`uname -m` PSUEDONAME="N/A" GetVersionFromFile() < VERSION=`cat $1 | tr "\n" ' ' | sed s/.*VERSION.*=\ // ` >if [ "$" = "SunOS" ] ; then DIST=Solaris DIST_VER=`uname -v` # also: cat /etc/release elif [ "$" = "AIX" ] ; then DIST="$" DIST_VER=`oslevel -r` elif [ "$" = "Linux" ] ; then if [ -f /etc/redhat-release ] ; then DIST='RedHat' PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/redhat-release ` DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/redhat-release ` elif [ -f /etc/SuSE-release ] ; then DIST=`cat /etc/SuSE-release | tr "\n" ' '| sed s/VERSION.*//` DIST_VER=`cat /etc/SuSE-release | tr "\n" ' ' | sed s/.*=\ //` elif [ -f /etc/mandrake-release ] ; then DIST='Mandrake' PSUEDONAME=`sed -e 's/.*\(//' -e 's/\)//' /etc/mandrake-release` DIST_VER=`sed -e 's/.*release\ //' -e 's/\ .*//' /etc/mandrake-release` elif [ -f /etc/debian_version ] ; then DIST="Debian" DIST_VER=`cat /etc/debian_version` PSUEDONAME=`lsb_release -a 2> /dev/null | grep '^Codename:' | sed -e 's/.*[[:space:]]//'` #elif [ -f /etc/gentoo-release ] ; then #TODO #elif [ -f /etc/slackware-version ] ; then #TODO elif [ -f /etc/issue ] ; then # We use this indirection because /etc/issue may look like # "Debian GNU/Linux 10 \n \l" ISSUE=`cat /etc/issue` ISSUE=`echo -e "$" | head -n 1 | sed -e 's/[[:space:]]\+$//'` DIST=`echo -e "$" | sed -e 's/[[:space:]].*//'` DIST_VER=`echo -e "$" | sed -e 's/.*[[:space:]]//'` fi if [ -f /etc/UnitedLinux-release ] ; then DIST="$[`cat /etc/UnitedLinux-release | tr "\n" ' ' | sed s/VERSION.*//`]" fi # NOTE `sed -e 's/.*(//' -e 's/).*//' /proc/version` # is an option that worked ~ 2010 and earlier fi if $PRINT_HEADER then echo "OS$Distribution$Distribution-Version$Pseudo-Name$Kernel-Revision$Machine-Architecture" fi echo "$$$$$$$$$$$" 

NOTE: Only tested on Debian 11

Example Runs

No args

Linux,Debian,10.0,buster,4.19.0-5-amd64,x86_64 

Header with names and custom separator

OS | Distribution | Distribution-Version | Pseudo-Name | Kernel-Revision | Machine-Architecture Linux | Debian | 10.0 | buster | 4.19.0-5-amd64 | x86_64 

Filtered output

Источник

How to get linux distribution name and version?

In a Linux environment, it’s important to know the distribution name and version for various reasons such as compatibility issues with software, managing system updates and upgrades, and troubleshooting. There are various methods to retrieve the Linux distribution name and version, which are discussed below.

Читайте также:  Setup ssh server on linux

Method 1: Using /etc/os-release file

How to get Linux distribution name and version using /etc/os-release file

Here are the steps to get the Linux distribution name and version using the /etc/os-release file:

  1. Open the terminal and enter the following command to view the contents of the /etc/os-release file:
NAME="Ubuntu" VERSION="20.04.1 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.1 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal
  1. To get the Linux distribution name, use the grep command to filter the output of the /etc/os-release file:
grep '^NAME' /etc/os-release | awk -F= '< print $2 >' | tr -d '"'
grep '^VERSION_ID' /etc/os-release | awk -F= '< print $2 >' | tr -d '"'

That’s it! You now know how to get the Linux distribution name and version using the /etc/os-release file.

Method 2: Using lsb_release command

The lsb_release command is a utility for obtaining information about the Linux distribution you are using. To get the Linux distribution name and version using lsb_release command, follow the steps below:

  1. Open the terminal on your Linux machine.
  2. Type the following command:

This command will display all the information about the distribution name, version, codename, and other details.

Distributor ID: Ubuntu Description: Ubuntu 20.04.2 LTS Release: 20.04 Codename: focal

That’s it! You have successfully obtained the Linux distribution name and version using the lsb_release command.

Method 3: Using /proc/version file

To get the name and version of your Linux distribution using the /proc/version file, follow these steps:

  1. Open your terminal.
  2. Type the following command to view the contents of the /proc/version file:
Linux version 5.8.0-43-generic (buildd@lgw01-amd64-051) (gcc (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0, GNU ld (GNU Binutils for Ubuntu) 2.34) #49~20.04.1-Ubuntu SMP Fri Feb 5 09:57:56 UTC 2021
  1. From this output, you can extract the name and version of your Linux distribution using the cut command. For example, to get the distribution name, type:
cat /proc/version | cut -d " " -f1
cat /proc/version | cut -d " " -f3

That’s it! You now know how to get the name and version of your Linux distribution using the /proc/version file.

Method 4: Using hostnamectl command

To get the Linux distribution name and version using the hostnamectl command, follow these steps:

  1. Open the terminal on your Linux system.
  2. Type the following command and press Enter:

Operating System: Ubuntu 20.04.2 LTS Kernel: Linux 5.4.0-77-generic Architecture: x86-64

 4. In this example, the operating system is Ubuntu 20.04.2 LTS. You can also use the `hostnamectl` command with specific options to get more detailed information about the operating system. For example: - To get only the distribution name, use the `--pretty` option: 
 - To get only the distribution version, use the `--static` option: 
 - To get both the distribution name and version, use the `--pretty` and `--static` options together: 

hostnamectl —pretty —static

Each of these commands will output the corresponding information about your Linux distribution.

Источник

How to find Linux Distribution name using shell script?

I am writing a shell script in which I need the current operating system name to make it generic. Like:

if [ $Operating_System == "CentOS" ] then echo "CentOS"; # Do this elif [ $Operating_System == "Ubuntu" ] then echo "Ubuntu"; # Do that else echo "Unsupported Operating System"; fi 

How will it be possible? Applying regular expression on lsb_release -a command or something else? Thanks..

Yup — lsb-release is definitely the most robust, generic way to do it. EXAMPLE: lsb_release -d|awk ‘‘ .

7 Answers 7

$ lsb_release -i Distributor ID: Fedora $ lsb_release -i | cut -f 2- Fedora 

You can get the info from lsb_release :

i stands for distributor id.

For ex. It shows Ubuntu instead of Distributor Id: Ubuntu

You can get this information by running lsb_release —help or man lsb_release

EDIT 2: As pointed out by @Jean-Michaël Celerier, some distros prefer to add double quotes.

awk -F'=' '/^ID=/ < gsub("\"","",$2); print tolower($2) >' /etc/*-release 2> /dev/null 
(awk -F'=' '/^ID=/ < print tolower($2) >' /etc/*-release | tr -d '"') 2> /dev/null 

. can be used to remove them.

EDIT 1: as suggested by @S0AndS0, this is slightly better:

awk -F'=' '/^ID=/ ' /etc/*-release 2> /dev/null 

This is almost perfect, no need for the piping between awk though as awk -F’=’ ‘/^ID=/ ‘ /etc/*-release does just fine, also for those with the want to detect distros based on Debian awk -F’=’ ‘/^ID_LIKE=/ ‘ /etc/*-release 2>/dev/null is even closer to perfect for my use case.

Small improvement: remove the quotes that some distros like opensuse add: awk -F’=’ ‘/^ID=/ < gsub("\"","",$2); print tolower($2) >‘ /etc/*-release 2> /dev/null

DISTRO=$( cat /etc/*-release | tr [:upper:] [:lower:] | grep -Poi '(debian|ubuntu|red hat|centos|nameyourdistro)' | uniq ) if [ -z $DISTRO ]; then DISTRO='unknown' fi echo "Detected Linux distribution: $DISTRO" 

For almost all linux distros, cat /etc/issue will do the trick.

Edit: Obviously, no solution can apply to all distros, as distros are free to do as they please.

Further clarification: This is not guaranteed to work — nothing is — but in my experience, this is the method that most often works. Actually, it’s the only method that works consistently ( lsb_release , which was mentioned here, often produces command not found ).

/etc/issue is not meant for storing information about the operating system and may contain whatever text the admin likes. I would never trust it for detecting operating system or distribution.

Specifically, /etc/issue is a text file displayed by getty at the console login. It is not guaranteed to contain any meaningful information at all about the distribution.

this is not a reliable solution, since these are files for the administrator to put in whatever he wants.

Источник

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