The UTS namespace
The UTS namespace manages two resources: the hostname and the NIS domain name. The hostname identifies a machine on a network. It can be queried or changed with commands such as hostname and uname. Thanks to namespaces, each process or group of processes can have its own hostname on the machine. For example:
use strict;
use Eixo::Zone;
my $pid = Eixo::Zone->init(
init => sub {
`hostname CHILD-MACHINE`;
print "The hostname [in the child namespace] is " . `hostname`;
exit(0);
},
self_uts => 1
);
waitpid($pid, 0);
print "The hostname [in the parent namespace] is " . `hostname`;

This produces the following output (depending on the machine’s actual hostname):
The hostname [in the child namespace] is CHILD-MACHINE
The hostname [in the parent namespace] is examples
What happened?
We created a process with clone, passing the CLONE_NEWUTS flag. This gives the child process its own UTS namespace. The sethostname call runs in the child’s UTS namespace, so it changes that namespace’s hostname. Calling gethostname from the parent’s namespace returns the original hostname. The kernel contextualises [set/get]hostname calls according to the namespace from which they are made.
Creating a “visitor” process
Suppose we want to know the hostname used inside a project’s private UTS namespace. Linux lets a process enter another process’s or process group’s namespace and make kernel calls from within it. Here is the code:
use strict;
use Eixo::Zone;
my $pid = Eixo::Zone->init(
init => sub {
`hostname CHILD-MACHINE`;
print "The hostname [in the child namespace] is " . `hostname`;
sleep(2);
},
self_uts => 1
);
# Join the child's UTS namespace.
sleep(1);
Eixo::Zone->join($pid, uts => 1);
# We are now in the process's hostname namespace.
print "The hostname [in the child namespace] is " . `hostname`;
waitpid($pid, 0);
This produces the following output:
The hostname [in the child namespace] is CHILD-MACHINE
The hostname [in the child namespace] is CHILD-MACHINE
The child process performed the same operation as in the previous section. The difference is in the parent: by calling setns, it joined the child’s UTS namespace. From that point on, its gethostname call is relative to that namespace. In other words, with setns calls, a process can “visit” other namespaces.
This article follows our introduction to PID namespaces.
