Asterisk, and other worldly endeavours.

A blog by Leif Madsen

Assign unique hostname to dhcp client with dnsmasq

Today I’ve been getting our lab environment setup with vagrant to auto-provision our lab servers with chef server in order to allow the development team to quickly and easily turn up and tear down web application servers.

Because when the server gets spun up with vagrant, it registers itself as a new node to the chef server using its hostname. Since using localhost for every node pretty much makes the chef server useless for more than 1 virtual machine at a time, I needed to figure out how to get dnsmasq to assign a unique hostname based on the IP address being provided by dnsmasq to the dhcp client.

I had seen a similar thing done with Amazon EC2 instances that when they turn up, they gets a hostname that looks similar to the private IP address it has been assigned. For example, if the private IP address assigned to the server was it would get a hostname like ip-192-168-12-14. I wanted to do a similar thing with our server.

After a little bit of Googling and reading the dnsmasq configuration file, it donned on me how simple this really was. You simply need to define the hostnames that the dnsmasq server could assign to a server, list those in the /etc/hosts file on the dnsmasq server, and then define the hostname you wanted to provide to the server. I didn’t want to use the MAC address of the servers (a la dhcp-host option) since the MAC address will be dynamic each time I spin up a virtual machine.

So in my dnsmasq.conf file I might have something defined like



So in my /etc/hosts file I’d just place the following to assign those unique hostnames:    ip-90-100-1-120    ip-90-100-1-121    ip-90-100-1-122    ip-90-100-1-123    ip-90-100-1-124

Written by Leif Madsen

2012/07/23 at 2:14 pm

2 Responses

Subscribe to comments with RSS.

  1. I also recently started using dnsmasq with /etc/hosts, which makes things easier, but another thing that might work for you is dnsmasq will try to use the hostname that the client asks for in the dhcp request, so the laptops, etc. that connect to my network often get hostnames (unless they conflict with a hostname that dnsmasq already knows about, and so then denies the request).

    Jon Daley

    2012/08/05 at 7:23 am

    • Ya that’s something I like about dnsmasq as well, but in the particular case I was using, I needed to assign the hostname based on the address provided to the server in a controllable manner rather than relying on the host itself to supply the ideal hostname.

      Leif Madsen

      2012/08/12 at 3:55 pm

Comments are closed.

%d bloggers like this: