Linux下如何永久修改hostname的方法步骤

对于我们的终端设备,连接wifi,我们想要区分设备名称,那么修改hostname是比较好的选择,对于hostname,我们可以通过

man hostname 查看相关的内容;

如果我们想查看当前系统的hostname,可以使用如下指令:

(1)  uname -n

(2)  hostname

我们可以通过指令修改hostname:

hostname mydefinename 

我们通过指令修改的其实也是    /proc/sys/kernel/hostname   中的内容;

扫描二维码关注公众号,回复: 12203292 查看本文章

同样,我们还可以使用系统的API调用:

代码如下:hostname_main.cpp

#include <unistd.h>
#include <stdio.h>
#include<string.h>

int main()
{
    char buf[50] = "localhost.localdomain";

    if (sethostname(buf, strlen("localhost.localdomain")) < 0)
    {
        perror("sethostname");
    }
    else
    {
        printf("sethostname success!\n");
    }
     char buff[50];

    if (gethostname(buff, sizeof(buff)) == 0)
    {
        printf("%s\n", buff);
    }
    else
    {
        perror("gethostname");
    }
 return 0;
}

编译:

 g++ -o hostname_main hostname_main.cpp

执行结果如下:

[root@localhost Test307]# ./hostname_main 
sethostname success!
localhost.localdomain
[root@localhost Test307]# 

猜你喜欢

转载自blog.csdn.net/Swallow_he/article/details/107490439