gpt4 book ai didi

linux-kernel - 如何从 linux 内核模块获取主机名?

转载 作者:行者123 更新时间:2023-12-04 02:47:41 24 4
gpt4 key购买 nike

我觉得这个标题很好理解。我可以读取 procfs 文件/proc/sys/kernel/hostname,但是从内核空间读取文件是个坏主意。还有其他办法吗?

最佳答案

uname man page说:

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.

如果您真的认为需要主机名,请使用如下代码:

#include <linux/utsname.h>

printk("hostname: %s\n", utsname()->nodename);

关于linux-kernel - 如何从 linux 内核模块获取主机名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18548468/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com