Perl programmer for hire: download my resume (PDF).
John Bokma MexIT
freelance Perl programmer

DHCP static IP addresses with dnsmasq

Wednesday, September 3, 2008 | 0 comments

Today I configured dnsmasq, running on Kamikaze 7.09, to serve out static IP addresses via DHCP to the computers connected to the router, a Linksys WRT54GL. The original firmware that came with the WRT54GL didn't support this feature, hence the major reason for me to use an alternative firmware. I had flashed Kamikaze 7.09 (OpenWrt) on the WRT54GLlast Monday, and I already liked it a lot. I am quite used to working on the command line, so I was not alienated by typing commands in PuTTY to configure the Linksys router.

I also extended the /etc/hosts file on the router for three (good) reasons: the first one is that somehow Ubuntu couldn't resolve hostnames from XP machines when I used DHCP. For example, ping cadwal didn't work from a computer running Ubuntu, with cadwal running XP. Yet the original firmware of the Linksys router did recognize the machine as cadwal. Second, I use virtual hosting a lot for development, and by extending /etc/hosts on the router, I can access those virtual hosts from other computers as well, thanks to dnsmasq. And the third reason: being able to block websites by hostname for all devices connected to the router.

Gathering the MAC addresses

The program that provides the DHCP service on Kamikaze is dnsmasq. Static IP addresses via DHCP are assigned to the connected computers based on the MAC address, a quasi-unique identifier assigned to the network adapter of each computer. The dnsmasq program can use information in /etc/ethers - MAC/IP pairs - to hand-out the IP address for a MAC address it received. So first I needed the MAC address of each device that could connect to the router in our house.

The easiest way I could think of to obtain the MAC addresses was to turn on each computer, and examine the contents of /tmp/dhcp.leases on the router using cat:

946728494 xx:xx:xx:xx:xx:xx 192.168.1.105 claudia 01:xx:xx:xx:xx:xx:xx
946728233 xx:xx:xx:xx:xx:xx 192.168.1.100 ecce *
946728124 xx:xx:xx:xx:xx:xx 192.168.1.101 cadwal 01:xx:xx:xx:xx:xx:xx
946728106 xx:xx:xx:xx:xx:xx 192.168.1.104 WM_Araminta 01:xx:xx:xx:xx:xx:xx
946728062 xx:xx:xx:xx:xx:xx 192.168.1.137 * 01:xx:xx:xx:xx:xx:xx
946728027 xx:xx:xx:xx:xx:xx 192.168.1.102 throy *

Note: I've replaced the actual MAC address of each device with xx:xx:xx:xx:xx:xx.

The /tmp/dhcp.leases has the following fields:

Time of lease expiry
This number is in epoch time (seconds since 1970) the lease expires. Note that the numbers shown above are in the past (year 2000) because I haven't configured Kamikaze to obtain the time from a NTP (time) server (will blog on that later).
MAC address
The quasi-unique identifier assigned to the network adapter of the device connected to the router.
IP address
The current IP address assigned to the device connected to the router.
Device name
If known, the unqualified name of the device, an asterisk otherwise. For example, 192.168.1.137 as shown above has an asterisk. This is Esme's Dell Inspiron 1525 running Windows Vista.
Client-ID
If known, the client ID of the device, an asterisk otherwise. In my case, the computers running Windows XP, Vista, or Windows Mobile 5 (WM_Araminta) show '01:' immediately followed by their MAC address. Devices running Ubuntu Linux show an asterisk.

Creating /etc/ethers

Next, I copied the output of cat /tmp/dhcp.leasesfrom PuTTY, using the middle mouse button, to TextPad, and moved the data around a bit, and added comments. The end result looked as follows:

# ecce - Dell Vostro 200 ST
xx:xx:xx:xx:xx:xx 192.168.1.100

# cadwal - Compaq Presario SR1505LA
xx:xx:xx:xx:xx:xx 192.168.1.101

# throy - Acer Aspire 4320
xx:xx:xx:xx:xx:xx 192.168.1.102

# wonderland - Dell Inspiron 1525
xx:xx:xx:xx:xx:xx 192.168.1.103

#  WM_Araminta - Dell Axim X51v
xx:xx:xx:xx:xx:xx 192.168.1.104

# claudia
xx:xx:xx:xx:xx:xx 192.168.1.105

Note: I've replaced the actual MAC address of each device with xx:xx:xx:xx:xx:xx.

Then I typed vi /etc/ethers in PuTTY to start vi on the router, and typed 'i' to set vi in insert mode. Next, I selected all lines in TextPad, and pasted them in PuTTY. I saved the lines and quit vi by pressing the ESC key, and typing 'w' followed by 'q'.

Finally, I rebooted the router by typing reboot. Note that not all computers got their new IP address according to /etc/ethers this way, they kept their earlier assigned IP address. Executing ipconfig /renew with Adminstrator rights on the computers running Windows XP fixed this.

Extending /etc/hosts

Next I extended the /etc/hosts file. An excerpt follows below:

127.0.0.1 localhost.
192.168.1.100 ecce
192.168.1.101 cadwal
192.168.1.101 lc.johnbokma.com
192.168.1.101 lc.castleamber.com
192.168.1.102 throy
192.168.1.103 wonderland
192.168.1.104 axim
192.168.1.105 claudia

# Disable ads in Live Messenger
0.0.0.0 rad.msn.com

The first line was already in the original /etc/hosts file. The two domains that start with lc are local versions of two of my websites, currently hosted on cadwal, see also: Installing and configuring Apache on Windows XP. Those local versions can now be accessed from any other machine connected to the router, which makes it easier to check those sites in different browsers, and with different screen sizes.

dnsmasq related

Also today

Please post a comment | read 0 comments | RSS feed