0

I just learned what a chroot jail is so excuse me if this questions doesn't make much sense.

Basically I have two processes running locally in my machine and would like the results of "uname -n" to be different for each of these processes (and all their children). How can I use a chroot jail to do this for me?

  • In resent times there have been new features added to Unix name-spaces, cgroups, etc. These are much more powerful, than chroot. I think that the chroot system call is now implemented using them. So it may be worth a look. – ctrl-alt-delor May 14 '18 at 18:04
1

You can't do this with chroot. But you can do it by changing UTS namespace, for example using unshare.

mysystem# unshare --uts sh -c 'hostname test; uname -n'; uname -n
test
mysystem

The same, interactively:

First terminal:

mysystem# unshare --uts bash
mysystem# hostname test
mysystem# exec bash # to refresh prompt
test# uname -n
test
test# strace uname -n 2>&1|tail
brk(NULL)                               = 0x5592a93f6000
brk(0x5592a9417000)                     = 0x5592a9417000
uname({sysname="Linux", nodename="test", ...}) = 0
fstat(1, {st_mode=S_IFIFO|0600, st_size=0, ...}) = 0
write(1, "test\n", 5test
)                   = 5
close(1)                                = 0
close(2)                                = 0
exit_group(0)                           = ?
+++ exited with 0 +++
test# 

Second terminal, unaffected:

mysystem# uname -n
mysystem
mysystem# 

Simply exiting the shell on the first terminal, returns to the calling one which never changed namespace:

test# exit
mysystem# 

Note that only the hostname changes. There are similar namespaces for various features (network, mount points...). That's the founding brick of containers: all of them are then in use (the other main brick for containers is cgroups).

  • will this apply to all the processes that are run from that terminal? – Iliketoproveit May 14 '18 at 17:09
  • So if I try running a C program that calls uname from sys/utsname this should return a different name right? – Iliketoproveit May 14 '18 at 17:24
  • This resolved my problem! Is there a way to allow a non-root to change the namespace? What are the security concerns in allowing someone access to the unshare command? – Iliketoproveit May 17 '18 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.