Third party cookies may be stored when visiting this site. Please see the cookie information.

PenguinTutor YouTube Channel

DHCP server setup in Ubuntu Linux - automatic IP address allocation

I have recently configured a DHCP server on my Ubuntu Linux server. Before I was using static IP addresses on each of the laptops that connected to my wireless network which wasn't a problem. With the appearance of more public wireless networks it means that I was having to switch between a static and dynamic address on a regular basis and so DHCP promised to make life easier.

DHCP stands for Dynamic Host Control Protocol. It is a protocol that allows a client computer (in this case my laptops), to request that the server provides them with an IP address valid for the network they connect to. This technique is used by most broadband providers and by any home broadband routers / wireless routers. Due to my Linux firewall I however use a Wireless Access Point, rather than a wireless router, so it does not have DHCP built into it, which is why I need to configure it in Linux.

The following is based on Ubuntu 7.10 - the Gutsy Gibbon, but should be the same in most versions of Ubuntu (and the configuration files should be similar across other Linux OS, such as Fedora / Redhat / SuSe etc).

Installation of the dhcp server

To install the server code then use
sudo apt-get install dhcp

There is also a package dhcp3-server, but I used just dhcp in this case.
Also note that you will most likely already have dhcpcd, dhcp-client or dhcp3-client already installed, which is used to acquire a DHCP address for the local machine and not provide the server aspects.

The computer may tray to automatically start the server, but it will most likely fail until configuration is complete.

Configuration of the dhcp server

The first file to edit is /etc/default/dhcp

This has an entry:
INTERFACES=""

which needs to be edited to list the interfaces that the server should listen for. In my case the local network is eth1 (eth0 is my Internet connection), so my entry is:

INTERFACES="eth1"

The other file is the main configuration file /etc/dhcpd.conf

The first part of the file contains some default values that will be applied to any addresses given out. An important entry is the Name Server entry:

option domain-name-servers <address1>, <address2>;

The addresses of your DNS servers is normally provided by your ISP. Two servers are normally listed as this provides built-in redundancy in the protocol. Your name servers may be listed in /etc/resolv.conf if you don't know what they are.

Then there are some sections that describe how addresses are to be given out. These are all commented out by default, an example is shown below:

subnet 192.168.1.0 netmask 255.255.255.0 {

range 192.168.1.10 192.168.1.250;
option broadcast-address 192.168.1.255;
option routers 192.168.1.1;
}

This example uses the private address range 192.168.1.0/24. The range of addresses allocated starts at .10, and goes up to .250, which leaves a few addresses at the top and bottom of the range that can be allocated to static devices (e.g. my server / Wireless Access Point etc).

The routers entry needs to point at the gateway to the Internet which is the address of my Linux server in this case.

The server can then be restarted using:
sudo /etc/init.d/dhcp

Look in the system log /var/log/syslog if the daemon fails to start.

If you are running a firewall you may need to add a rule to allow the DHCP traffic to reach the server, but other than that configuration is complete and any computers set to automatically request their IP address should be allocated an address in the specified range.

You can see any IP addresses handed out in the file: /var/lib/dhcp/dhcpd.leases

Advanced Configuration

This concludes the basic configuration for the DHCP server, but further options allow for specific IP addresses to be allocated to certain machines (based on their MAC address), or for different addresses / routing / DNS information based up the interface that they connect on etc.

More information is available from
man dhcpd

and
man dhcpd.conf