前言

使用uname命令的好处:

1 使用uname命令可以写出可移植的shell

2 使用uname命令可以写出可移植的Makefile

使用uname系统调用的好处:

使用uname系统调用可以写出可移植的应用程序。

一 uname命令帮助信息

uname --help
root@ATK-IMX6U:~# uname --help
Usage: uname [OPTION]...
Print certain system information.  With no OPTION, same as -s.-a, --all                print all information, in the following order,except omit -p and -i if unknown:-s, --kernel-name        print the kernel name-n, --nodename           print the network node hostname-r, --kernel-release     print the kernel release-v, --kernel-version     print the kernel version-m, --machine            print the machine hardware name-p, --processor          print the processor type (non-portable)-i, --hardware-platform  print the hardware platform (non-portable)-o, --operating-system   print the operating system--help     display this help and exit--version  output version information and exitGNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/uname>
or available locally via: info '(coreutils) uname invocation'
root@ATK-IMX6U:~#

二 uname命令参数使用

下面测试是在arm系统中完成。

参数-p:获取架构

# uname -p
armv7l

参数 -a:打印所有信息

# uname -a
Linux ATK-IMX6U 4.1.15 #3 SMP PREEMPT Fri Sep 30 11:39:58 CST 2022 armv7l armv7l armv7l GNU/Linux

参数-s:打印内核名字

# uname -s
Linux

参数-n:打印网络节点名字(主机名)

# uname -n
ATK-IMX6U

参数-r:打印内核发布版本

# uname -r
4.1.15

参数-m:打印机器硬件名字

# uname -m
armv7l

参数-v:打印内核的编译时间

# uname -v
#3 SMP PREEMPT Fri Sep 30 11:39:58 CST 2022

参数-i:打印硬件平台

# uname -i
armv7l

参数-o:打印操作系统

# uname -o
GNU/Linux

三 uname系统调用分析

man帮助手册

man 2 uname

$ man 2 uname
       #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
           };

The length of the arrays in a struct utsname is unspecified (see NOTES); the fields are terminated by a null byte ('\0').

RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and errno is set appropriately.

ERRORS
       EFAULT buf is not valid.

CONFORMING TO
       POSIX.1-2001, POSIX.1-2008, SVr4.  There is no uname() call in 4.3BSD.

The domainname member (the NIS or YP domain name) is a GNU extension.

NOTES
       This  is  a  system call, and the operating system presumably knows its name, release and version.  It also knows what hardware it runs on.  So, four of the fields of the struct are meaningful.  On the
       other hand, the field nodename is meaningless: it gives the name of the present machine in some undefined network, but typically machines are in more than one network and have several names.  Moreover,
       the kernel has no way of knowing about such things, so it has to be told what to answer here.  The same holds for the additional domainname field.

To  this end, Linux uses the system calls sethostname(2) and setdomainname(2).  Note that there is no standard that says that the hostname set by sethostname(2) is the same string as the nodename field
       of the struct returned by uname() (indeed, some systems allow a 256-byte hostname and an 8-byte nodename), but this is true on Linux.  The same holds for setdomainname(2) and the domainname field.

The length of the fields in the struct varies.  Some operating systems or libraries use a hardcoded 9 or 33 or 65 or 257.  Other  systems  use  SYS_NMLN  or  _SYS_NMLN  or  UTSLEN  or  _UTSNAME_LENGTH.
       Clearly, it is a bad idea to use any of these constants; just use sizeof(...).  Often 257 is chosen in order to have room for an internet hostname.

Part of the utsname information is also accessible via /proc/sys/kernel/{ostype, hostname, osrelease, version, domainname}.

C library/kernel differences
       Over  time, increases in the size of the utsname structure have led to three successive versions of uname(): sys_olduname() (slot __NR_oldolduname), sys_uname() (slot __NR_olduname), and sys_newuname()
       (slot __NR_uname).  The first one used length 9 for all fields; the second used 65; the third also uses 65 but adds the domainname field.  The glibc uname() wrapper function hides  these  details  from
       applications, invoking the most recent version of the system call provided by the kernel.

SEE ALSO
       uname(1), getdomainname(2), gethostname(2), namespaces(7)

从man帮助信息中提取有用信息

头文件和函数原型

#include <sys/utsname.h>int uname(struct utsname *buf);
struct utsname {char sysname[];    /* Operating system name (e.g., "Linux") */char nodename[];   /* Name within "some implementation-definednetwork" */char release[];    /* Operating system release (e.g., "2.6.28") */char version[];    /* Operating system version */char machine[];    /* Hardware identifier */#ifdef _GNU_SOURCEchar domainname[]; /* NIS or YP domain name */#endif};

测试代码

#include <stdio.h>
#include <sys/utsname.h>int main(int args,char *argv[])
{struct utsname ubuf;struct utsname *u;int ret;ret = uname(&ubuf);u = &ubuf;printf("ret = %d\n",ret);printf("sysname = %s\n",u->sysname);printf("nodename = %s\n",u->nodename);printf("release = %s\n",u->release);printf("version = %s\n",u->version);printf("machine = %s\n",u->machine);
#ifdef _GNU_SOURCEprintf("domainname = %s\n",u->domainname);
#elseprintf("nodef _GNU_SOURCE");
#endifreturn 0;
}

ubuntu中测试输出:

$ ./a.out
ret = 0
sysname = Linux
nodename = ubuntu
release = 4.19.260
version = #1 SMP Thu Sep 29 14:19:07 CST 2022
machine = x86_64
nodef _GNU_SOURCE

arm中测试输出

# ./arm_u
ret = 0
sysname = Linux
nodename = ATK-IMX6U
release = 4.1.15
version = #3 SMP PREEMPT Fri Sep 30 11:39:58 CST 2022
machine = armv7l
nodef _GNU_SOURCE

从测试结果得出,通过machine成员判断不同的架构,可以用于通用代码的编写。

总结

念念不忘必有回响

uname命令使用和uname系统调用相关推荐

  1. uname命令 linux,Linux uname命令详解

    Linux uname命令用于显示系统信息.uname可显示电脑以及操作系统的相关信息. 语法参数: uname [参数] 参数: -a或--all:显示全部的信息: -m或--machine:显示电 ...

  2. uname命令 linux,Linux uname 命令

    Linux uname 命令 Linux uname(英文全拼:unix name)命令用于显示系统信息. uname 可显示电脑以及操作系统的相关信息. 语法uname [-amnrsv][--he ...

  3. uname命令 linux,Linux系统调用--uname()函数及系统下的uname命令

    [uname系统调用] 功能描述: 获取当前内核名称和其它信息. 用法: #include /* Put information about the system in NAME.  */ exter ...

  4. linux uname命令参数及用法详解--linux查看系统信息命令

    uname 命令可用于大多数 UNIX 和类 UNIX 系统以及 Linux. 功能说明:uname用来获取电脑和操作系统的相关信息. 语 法:uname [-amnrsvpio][--help][- ...

  5. uname 命令如何实现?

    strace 跟踪 uname 命令执行 strace 跟踪得到下面这些关键信息: uname({sysname="Linux", nodename="debian-10 ...

  6. linux uname 命令

    2019独角兽企业重金招聘Python工程师标准>>> uname 命令可用于大多数 UNIX 和类 UNIX 系统以及 Linux. 功能说明:uname用来获取电脑和操作系统的相 ...

  7. linux uname 命令详解

    linux uname 命令详解 功能说明:显示系统信息. 语 法:uname [-amnrsv][--help][--version] 补充说明:uname可显示电脑以及操作系统的相关信息. 参 数 ...

  8. 【Linux】一步一步学Linux——uname命令(72)

    00. 目录 文章目录 00. 目录 01. 命令概述 02. 命令格式 03. 常用选项 04. 参考示例 05. 附录 01. 命令概述 uname命令的英文全称即"Unix name& ...

  9. Linux uname 命令 打印系统信息

    转自:https://www.jb51.net/LINUXjishu/417626.html 1.概述 打印系统信息 2.命令格式 uname [OPTION]... 3.常用命令参数 打印一些系统信 ...

最新文章

  1. 表格对决CSS--一场生死之战
  2. 地平线机器人提出Anchor free、NMS free的3D目标检测算法 | CVPR2020 Workshop
  3. 转: java学习路线图
  4. 如何在geth中创建genesis.json_如何在Photoshop中应用“通道混合器”创建一个复古韵味色调...
  5. matlab正弦序列频谱,【求助】正弦信号序列fft频谱分析!!!
  6. python 升级服务器_开发服务器之升级到Python2.7
  7. 技术篇-符号制作-线符号制作
  8. 基于yaf+yar微服务解决方案教程
  9. VS2015 ASSERT(false)直接退出不弹出Assert failed对话框的解决方法
  10. 1.4多媒体计算机系统教案,1.4 多媒体计算机系统教案3
  11. 信息安全-终端安全-Windows安全配置
  12. 【数据分析】销售案例——会员分析
  13. 求95859回文数c语言程序,csdn 回文数
  14. 42表盘直径是从哪测量_手表尺寸怎么选择 手表尺寸怎么测量
  15. Nvidia最新三维重建技术Instant-ngp初探
  16. 码元 码元传输速率 波特率 比特率 数据率
  17. FTTB FTTC FTTH FTTO FSA
  18. 计算机专业用什么轴的键盘,平常爱打游戏,机械键盘应该买什么轴?
  19. FLStudio21水果免费版本FL2023电音制作软件
  20. DEC多国字符集(MCS)-ASCII码对照表、键盘常用ASCII码

热门文章

  1. 记我一次成功的入侵学校网站服务器的黑客行动
  2. 【蓝桥杯选拔赛真题34】Scratch数苹果 少儿编程scratch蓝桥杯选拔赛真题讲解
  3. Java开发学习路线,大拿告诉你Java学习都应该学什么
  4. 淫欲的真相是什么呢?可能很多英雄豪杰都没有想到过
  5. 如何在Raspberry Pi和ESP8266中使用MQTT
  6. excel不显示提示对话框
  7. 小提琴统计图_【统计图】如何绘制小提琴图?
  8. Loss——Focal Loss
  9. 像追女神一样学好java~
  10. youtube后端是python吗_Web后端开发,用Python还是Go呢?