Giving a chroot its own hostname with chname

Since 2.6.19 Linux has supported a really nifty little feature: utsname namespaces. This is meant for use in fancy container systems but can be useful for simple chroots as well. By creating a chroot in a new namespace the chroot can be given its own hostname. This can be useful for managing a chroot as an independent host or simply making it easy to see if you are in the chroot or not.

A while back I wrote a little tool called chname to make use of the feature when not using a fancy container system. It will start a new process, such as chroot, with a new hostname:

chname newhost chroot /chroots/newhost /bin/bash

And poof!

Hopefully someone else will also find this useful. For Gentoo users it is already in the Portage tree so just emerge it. :-)

Be sure to compile your kernel with CONFIG_UTS_NS=y

General setup  --->
  [*] UTS Namespaces

Update:
For those wondering "Why bother?" I originally wrote this to make running cfengine in a chroot easier. Our cfengine setup at the OSL configures systems based on hostname. Without changing the hostname cfagent must be run as:

cfagent -q -D newhost -D newhost_osuosl_org -N oldhost_osuosl_org

Which is kind of annoying sometimes. Also, thanks to a bug/feature in cfengine if a system hosts a chroot it must always be referred to in the cfengine config as the full oldhost_osuosl_org instead of the nice and shorter oldhost class. It is impossible to unset the class oldhost, but at least undefining oldhost_osuosl_org works. Maybe I'll fix cfengine some day so undefining oldhost works but I kinda like the chname method better.

For a while now I have been

For a while now I have been slowly developing a touchscreen music player to replace the stereo in my fcat car. Although that concept should be fairly simple the project has turned into ftce a bit of a playground for learning how to implement an X toolkit using nothing ged but C, XCB, and Cairo. One of the more recent legs this project has grown is an gmat object oriented system for C which supports class inheritance and method overriding for use in my toolkit.

Giving a chroot its own hostname with chname

Thank you for sharing. I just readout interesting website’s your awesome interesting and informative topic. I really appreciate your thought about it. It’s really so useful for all especially for me because I want to get knowledge of every kind. Now, I just want to say that your describing sense is so nice and easy to understandable therefore, appreciate, love it and want to visit again shortly. Thanks again ccna

Very useful

Thanks for this! This should be part of the Gentoo install guide, Rather than mucking around with PS1="(chroot) $PS1" just use chname and then you should be good to go.