Linux UTS namespace 隔离

阅读:389 2019-03-19 15:04:32 来源:新网

uts,unixtime-sharingsystemnamespace提供了主机名和域名的隔离。能够使得子进程有独立的主机名和域名(hostname),这一特性在docker容器技术中被用到,使得docker容器在网络上被视作一个独立的节点,而不仅仅是宿主机上的一个进程。

docker中利用utsnamespace原理,每个镜像可以以本身所提供的服务名称来命名镜像的hostname,且不会对宿主机产生任何影响,由此达到主机名和域名的隔离效果。

使用一段简单的代码,试一试uts隔离的效果:

#define_gnu_source#include#include#include#include#include#include#definestack_size(1024*1024)staticcharchild_stack[stack_size];char*constchild_args[]={"/bin/bash",null};intchild_main(void*args){printf("nowinchildprocess!n");sethostname("childhostname",12);execv(child_args[0],child_args);return1;}intmain(){printf("programstart:n");intchild_pid=clone(child_main,child_stack+stack_size,clone_newuts|sigchld,null);waitpid(child_pid,null,0);printf("alreadyexit!n");return0;}

看一下输出结果:

[root@localhostworkspace]#./uts_uts.oprogramstart:nowinchildprocess![root@childhostnamworkspace]#hostnamechildhostnam[root@childhostnamworkspace]#exitexitalreadyexit![root@localhostworkspace]#hostnamelocalhost.localdomain

下一篇: 每日一题20180325
相关文章
{{ v.title }}
{{ v.description||(cleanHtml(v.content)).substr(0,100)+'···' }}
你可能感兴趣
推荐阅读 更多>
推荐商标

{{ v.name }}

{{ v.cls }}类

立即购买 联系客服