Understanding uname command in Linux Verify Distribution
The uname command is one of the first commands that a system administrator learns to use and ends up being one of the most frequently used commands throughout a system administrators’ career. Most of us, when we use the uname command, we use it with the -a option to print all the available information. For system administrators beginning their careers, we would like to mention that the uname command could sometimes be the subject of an interview question or two. In this article, we’ll show how to selectively use the uname command with different options to print a subset of the output of the uname -a command and understand the meaning of the output of each of the options.
uname Command in Linux
Print the kernel name
To print the kernel name we will use the uname -s command.
[root@arkit-centos ~]# uname -s Linux
I’ve executed this command on a Centos system but regardless of the OS distribution, be it centos, Red hat or Debian, the output of uname -s will be the word Linux.
Given below is the output of the uname -s command executed on a Solaris 11 system.
sa@s11:~$ uname -s SunOS
Print system name
To print the hostname of the system, we use the uname command with the -n option for the node name.
[root@arkit-centos ~]# uname -n arkit-centos
While using the system name in scripts it is advisable to use the uname -n command instead of using the hostname command to avoid accidentally changing the hostname with the hostname command.
The output of the uname -n command will be same as that of executing the hostname command.
[root@arkit-centos ~]# hostname arkit-centos
Know Kernel release
We commonly refer to the kernel release as the kernel version. To view the kernel release, we use the -r option with the uname command.
[root@arkit-centos ~]# uname -r 3.10.0-514.el7.x86_64
From the above output, we can validate that our Centos 7 system is running version 3 of the Linux kernel.
If we execute the same command on a Centos 6 system, we will observe that is running version 2 of the Linux kernel.
[root@arkit-centos6 ~]# uname -r 2.6.32-642.el6.x86_64
The same would be true for Red Hat Enterprise Linux versions 6 and 7 (RHEL 7).
If we execute the same command on a Solaris system, we get the below result.
sa@s11:~$ uname -r 5.11
As with the uname -n command we could make use of the uname -r command in scripts or even on the command line while accessing some kernel related files etc.
For example, we would access the kernel configuration file under the /boot directory like this.
[root@arkit-centos ~]# ls -l /boot/config-$(uname -r) -rw-r--r--. 1 root root 137696 Nov 22 2016 /boot/config-3.10.0-514.el7.x86_64
As you may notice, using the uname -r command in the above example to access the file was much easier than typing the whole name.
Over time with system upgrades our system might end up with multiple config files for different kernels.
Using the uname -r command with the file name would ensure that we are accessing the config file of the running kernel.
Machine Architecture
To know the OS architecture i.e. whether we are working on a 64 bit or 32-bit machine, we use the uname -m command.
[root@arkit-centos ~]# uname -m x86_64
Above output confirms that we are working on a 64-bit system.
The source of uname -a output:
Operating system has a system call named uname and the command line utility uname uses it to print its information.
If you type in man 2 uname, the following information is printed:
[root@arkit-centos ~]# man 2 uname UNAME(2) Linux Programmer's Manual UNAME(2) NAME uname - get name and information about current kernel SYNOPSIS #include <sys/utsname.h> int uname(struct utsname *buf); DESCRIPTION uname() returns system information in the structure pointed to by buf. The utsname struct is defined in <sys/utsname.h>: struct utsname { char sysname[]; /* Operating system name (e.g., "Linux") */ char nodename[]; /* Name within "some implementation-defined network" */ char release[]; /* Operating system release (e.g., "2.6.28") */ char version[]; /* Operating system version */ char machine[]; /* Hardware identifier */ #ifdef _GNU_SOURCE char domainname[]; /* NIS or YP domain name */ #endif };
Would like to note however that most of the information provided by the uname -a command can be found in the files present in the /proc file system and we will mention these files now
Can obtain the name of the kernel from the file /proc/sys/kernel/ostype
[root@arkit-centos ~]# cat /proc/sys/kernel/ostype Linux
Kernel version from the file /proc/sys/kernel/osrelease
[root@arkit-centos ~]# cat /proc/sys/kernel/osrelease 3.10.0-514.el7.x86_64
System hostname from the file /proc/sys/kernel/hostname
[root@arkit-centos ~]# cat /proc/sys/kernel/hostname arkit-centos
Conclusion
This concludes our discussion on the uname command. We hope that after going through this article you would be able to find more uses for the options provided with uname rather than restricting yourself to the use of uname -a alone.
Related Articles
How to Increase XFS File System Linux
Thanks for your wonderful Support and Encouragement