Leveraging system hostname as part of a jail's hostname

James Gritton jamie at freebsd.org
Wed Jun 20 16:52:28 UTC 2018


On 2018-06-19 20:41, Joseph Ward wrote:
> Hi everyone,
> 
> I have several jails, configured via jail.conf, whose hostname I want 
> to
> make: $name.$system_hostname.
> 
> Is there a way to do this in jail.conf?   If I use:
> 
> host = inherit;
> 
> the hostnames of the jails all match the hostname of the system.  I
> tried using:
> 
> host.hostname = $name;
> host.domainname = inherit;
> 
> but the hostname ends up just being $name (expanded, of course).
> 
> Trying:
> 
> host = inherit;
> host.hostname = $name;
> 
> ended up with simply $name as well, with the "inherit" ignored.
> 
> So, am I missing something? 

You can't do it with a simple substitution in a parameter setting, since 
there's no way in the config file to read the current hostname.  You 
don't want "host = inherit", because that will cause all jails to use 
the same hostname - if you change one, it changes all of them (and 
changes the system hostname).

But you can do it in two steps, involving some fairly ugly hackery 
around exec.poststart.  Something like:

foo
{
   exec.poststart = "jail -m name=foo host.hostname=$name.`hostname`";
   exec.poststart += "jexec foo sleep 600";
}

Unfortunately the second exec.poststart line is required as a 
replacement for the typical "exec.start='sh /etc/rc'" because 
exec.poststart runs after exec.start and that's to late to set the 
hostname.  This is one of these cases that suggests the need for a 
parameter run by the main host between jail creation and the running of 
exec.start.

Another possible hack would be to do something with setting a file 
inside the jail that's peeked at by the jail's rc.conf or rc.d/hostname.

Nothing pretty, I'm afraid.

- Jamie


More information about the freebsd-jail mailing list