Namespaces in Linux provide a mechanism for implementing containers. They’re useful for isolating processes.
In this tutorial, we’ll discuss UTS namespaces, which are one of the namespaces in Linux.
2. UTS Namespaces
A Linux namespace provides an abstraction wrapping a global resource for processes. For example, process IDs, networks, and mounted file systems are such global resources. Processes in the same namespace have the illusion of having their own isolated instances of global resources. Therefore, Linux namespaces are useful for building containers.
UTS namespaces are one of the namespaces available in Linux. They let us set hostnames and domain names without affecting the rest of the system. Therefore, processes in different UTS namespaces can have different hostnames and domain names.
UTS is the abbreviation of Unix Time-sharing System. The name stems from the C structure, struct utsname, passed to the uname() system call. The nodename and domainname are two members of this structure.
3. Using UTS Namespaces
We’ll see how to use UTS namespaces from the command line in this section.
3.1. Invoking a UTS Namespace
We can use the unshare command for creating namespaces. Firstly, let’s check the currently available UTS namespaces using the lsns command:
$ sudo lsns -t uts NS TYPE NPROCS PID USER COMMAND 4026531838 uts 187 1 root /sbin/init 4026532172 uts 1 265 root /lib/systemd/system-udevd 4026532262 uts 1 475 root /lib/systemd/system-logind
If we call the lsns command without any options, it lists information about all available namespaces. However, the -t option lists only the specified type of namespaces. We listed only UTS namespaces because of the -t uts part of the command. It seems that there are currently three UTS namespaces for the root user.
Now, let’s run a process in a new UTS namespace using unshare:
$ sudo unshare --uts /bin/bash
The –uts option of unshare is for running a process in a new UTS namespace. If we don’t specify a program to run in the new UTS namespace, unshare runs the shell defined in the $SHELL variable by default. We specify /bin/bash as the program to run in the new UTS namespace. Therefore, after running the command sudo unshare –uts /bin/bash, we’re in a new Bash shell running in a new UTS namespace.
Let’s check the available UTS namespaces once more:
$ sudo lsns –t uts NS TYPE NPROCS PID USER COMMAND 4026531838 uts 187 1 root /sbin/init 4026532172 uts 1 265 root /lib/systemd/system-udevd 4026532262 uts 1 475 root /lib/systemd/system-logind 4026532315 uts 3 2519 root /bin/bash
As is apparent from the output, the UTS namespace we invoked is listed in the last row. The command executed while running this namespace is /bin/bash as expected.
3.2. Changing the Hostname
Having a Bash shell running in a UTS namespace, let’s change the hostname in the Bash shell:
$ hostname debian11 $ hostname debian11_uts $ hostname debian11_uts
The machine’s name is debian11 as the first hostname command shows. This is inherited from the parent process. Then, we set the machine’s name as debian11_uts using the command hostname debian11_uts. Running the last hostname command verifies that we changed the machine’s name successfully.
Now, we’ll check whether changing the hostname in the UTS namespace affects other processes. Let’s open a new shell, and check the hostname in it:
$ hostname debian11
The new shell isn’t in the already running UTS namespace. The hostname is still debian11 in the new shell. Therefore, changing the name of the machine in a UTS namespace doesn’t affect the name of the machine for other processes outside the namespace. Hence, we’re successful in obtaining processes having different hostnames in the same machine.
3.3. Changing the Domain Name
Similar to having different hostnames, we can use UTS namespaces so that a process can have a separate domain name without affecting other processes. We can use the domainname command for setting and getting the domain name.
Firstly, let’s check the domain name in the Bash shell running in the UTS namespace:
$ domainname (none)
(none) is the value set by the kernel if the domain name isn’t set. Let’s change the domain name:
$ domainname –b domain_uts $ domainname domain_uts
We set the domain name using the domainname –b domain_uts command. The -b option of domainname sets the system’s NIS domain name. domain_uts is the new domain name. The output of the last domainname command shows that we’re successful in setting the domain name as domain_uts.
Now, we’ll check whether other processes are affected by this change. Let’s open a new shell, and check the domain name:
$ domainname (none)
Therefore, changing the domain name in the UTS namespace doesn’t affect the rest of the system as expected.
In this article, we discussed UTS namespaces. Firstly, we learned about UTS namespaces. We saw that they’re useful in isolating hostnames and domain names.
Then, we learned how to use UTS namespaces. We saw that we can use the –uts option of the unshare command to create a UTS namespace. Finally, we changed the hostname and domain name in a UTS namespace as an example and confirmed that other processes outside the UTS namespace aren’t affected.