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.
- Login to post comments
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.