Find build of linux

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:

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.

Читайте также:  Nvidia settings linux разгон

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 can I determine the build/version of Linux kernel ‘uImage’?

As per this answer superuser.com/a/1234270/439040 if you are in fact dealing with a compressed kernel image bzImage file (people in both the question and answers do seem to be dealing with both formats already) then file -bL [bzImage] , e.g. file -bL /boot/vmlinuz , will work.

Читайте также:  Как добавить службу linux

7 Answers 7

According to kernel’s format specification, here’s C code:

kver.c

#include int main(int argc, char** argv) < if (argc >1)< FILE* f = fopen(argv[1], "r"); short offset = 0; char str[128]; if(f)< fseek(f, 0x20E, SEEK_SET); fread(&offset, 2, 1, f); fseek(f, offset + 0x200, SEEK_SET); fread(str, 128, 1, f); str[127] = '\0'; printf("%s\n", str); fclose(f); return 0; >else < return 2; >> else < printf("use: kver [kernel image file]\n"); return 1; >> 
gcc -o kver kver.c ./kver /boot/vmlinux-something 

Thanks for this! Unfortunately though, it doesn’t work for me for a 4.19 kernel. Has the spec changed recently? kver against that vmlinux just outputs \n , but no contents.

This is a very useful code sample, however worth noting that (AFAICT) it applies to compressed vmlinuz rather than extracted vmlinux. (And I just get \n when running it against vmlinux too.)

To find out what version of Linux is compiled, use the strings utility on the uncompressed vmlinux image.

strings linux-src/build/build-generic/vmlinux|grep «Linux version»

Linux version 3.2.0-56-generic (root@puerto-cayo) (gcc version 4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5) ) #86 SMP Fri Nov 1 10:24:18 EDT 2013 (Ubuntu 3.2.0-56.86-generic 3.2.51)

I just realized, the kernels I have immediate access to do have the version string stored uncompressed amongst the headers. strings uImage | grep 2.6 ought to be good enough for any 2.6 kernel which covers pretty much everything in the last 5+ years).

It’s theoretically possible, but not entirely trivial.

Modern Linux kernel versions use a format called bzImage (for x86/x86_64, YMMV on other platforms). It actually consists of an ELF header and some other minutia (like a bit of decompression code) followed by, yes, a compressed image of the actual kernel.

Traditionally, the compression algorithm was zlib (contrary to popular misconception, ‘bzImage’ did not stand for «bzipped image», but for «big zImage» — the original zImage format not being able to handle large kernels), though versions after 2.6.30 also support bzip2 and LZMA.

What you’ll probably have to do is determine exactly where the compressed data starts (sorry, can’t help you there, but trial and error might work), and write a bit of code to run it through the library for whichever compression algorithm is in use.

Источник

Where do I find the version of a Linux kernel source tree?

I have downloaded from a hardware vendor just a tarball of their Linux source tree (no Git repository metadata), is there a way to find out the version number of the kernel? Is the version number usually stored in a file somewhere in the source tree? I’d like to be able to do this without compiling and running the kernel.

6 Answers 6

You can find the version by running

I should have clarified that I want to be able to do this just by examing the source tree rather than attempting to compile an run the kernel if possible. I’ve updated my question accordingly.

Читайте также:  Can you install android apps on linux

Adrian, I suspect it was when you had the uname solution which would require installing the kernel. In any case, since you now have one that doesn’t require a build and install, here’s an upvote for you to counteract the drive-by downvote — I hate them with a vengeance 🙂

@paxdiablo Thanks and probably true — since the OP also edited his question the uname thing became the wrong answer. I just wish people would say — oh well 🙂

Looking at the question edit history, uname was always the wrong answer (the question implied that the hardware to run the kernel might not even be available). But from the original question I would have thought that something that inspected the build outputs would have been acceptable.

Check the top-level Makefile , an example of which is here. At the top of that, you should see something like:

VERSION = 3 PATCHLEVEL = 1 SUBLEVEL = 0 EXTRAVERSION = -pax NAME = Custom Pax Version 

The (admittedly minor) advantage of this method is that you only need the ability to view the files themselves rather than having to run a build process.

both methods are very good (I mean make kernelversion as well as vi Makefile) as in result before making any module or something depending on the kernel running now it’s important to verify if command uname -r will match the output of make kernelversion and if not to modify with vi Makefile

The major advantage of this method is that you don’t need to clone a large repository just to check the version, when said version is not guessable from the git tag. Thanks.

Yet another solution: in the older times include/linux/version.h , currently include/generated/uapi/linux/version.h , but only after at least a partially successful compilation.

include/linux/version.h is deprecated, it now gets generated during a build in include/generated/uapi/linux/version.h

In the Linux source tree’s root file, check the Makefile content. In its beginning part:

# SPDX-License-Identifier: GPL-2.0 VERSION = 4 PATCHLEVEL = 14 SUBLEVEL = 67 

Then you linux source tree’s version is: 4.14.67

In the kernel source tree, check the root directory Makefile to get the kernel version as below. Example as below: $ head Makefile # SPDX-License-Identifier: GPL-2.0 VERSION = 5 PATCHLEVEL = 18 SUBLEVEL = 0 EXTRAVERSION = -rc3 NAME = Superb Owl # *DOCUMENTATION* # To see a list of typical targets execute "make help" # More info can be located in ./README From the above we get the source code version is 5.18.0-rc3 

In case someone needs to script it: With awk , the version can be printed from Makefile like this:

awk '/^VERSION =/;/^PATCHLEVEL =/;/^SUBLEVEL =/;/^EXTRAVERSION =/;END' Makefile 

If the EXTRAVERSION appendix shall be skipped:

awk '/^VERSION =/;/^PATCHLEVEL =/;/^SUBLEVEL =/;END' Makefile 

If someone knows how to make awk continue with the next pattern if the current pattern has been matched once, that would make it failsafe in case multiple lines start with VERSION = respectively. But I haven’t seen this in any Linux source code version.

Источник

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