From charlesreid1

This page contains information/instructions for setting up a DHCP server on Linux.

The DHCP Server

DHCP stands for domain host control protocol. It controls the details of your network - the IP addresses, the broadcast addresses, the lease times, the network name, and the network submask. The DHCP server hands out IP addresses.

We used ipcalc to decide on a network layout (Linux/IP Schema page), which includes number of addresses, number of bits, subnet mask, etc. Now we put it into action with a DHCP server.

example: 4 subnets

Let's give an example of how to configure a DHCP server to pick and hand out IPv4 addresses to any machine that asks for one.

Use the isc-dhcp-server to do this. Start by installing it:

$ apt-get install isc-dhcp-server

Now configure the dhcp server by editing its configuration file, located at /etc/dhcp/dhcp.conf

If you have a sample dhcp.conf file present, it will be pretty extensive. Or, it might be totally empty. Here is a sample:

default-lease-time 86400;
max-lease-time 86400;
option subnet-mask 255.255.252.0;
option broadcast-address 10.10.99.255;
option domain-name "local.lan";
authoritative;
subnet 10.10.96.0 netmask 255.255.252.0 {
    range 10.10.99.100 10.10.99.254;
    option routers 10.10.96.1;
    option domain-name-servers 10.10.96.1;
}

To go through that a bit slower:

The default lease time and max lease time set the lease times of ip addresses.

The subnet mask and broadcast address options specify what subnet mask the clients will be given, and what broadcast address they'll use.

The subnet mask identifies the network that each connecting node will be a part of. The clients check their IP information after being provided an address. The subnet mask identified here should be shown.

The broadcast address is the address on which all nodes within this subnet are able to receive packets.

The domain-name option applies the domain name to the hostnames of each node that connects to the DHCP server. The authoritative option specifies that this DHCP server should be the primary DHCP server for this subnet.

The following lines about the subnet range, routers, and domain name servers:

subnet 10.10.96.0 netmask 255.255.252.0 {
    range 10.10.99.1 10.10.99.254;
    option routers 10.10.96.1;
    option domain-name-servers 10.10.96.1;
}

these identify network address for the subnet, the subnet mask, the range of IP addresses that will be issued, the default gateway, and the DNS server.

This example shows that the first DHCP-issued address will be at 10.10.99.100 and it will end at 10.10.99.254.

This cuts out a really large number of possible IP addresses. The range we have specified, 10.10.96.0/22, covers the following subnets:

10.10.96.0
10.10.97.0
10.10.98.0
10.10.99.0

so in fact we could hand out IP addresses starting at 10.10.96.1, rather than 10.10.99.100.

Once you have edited this file, you will restart the daemon.

$ systemctl restart isc-dhcp-server

an easier example

This example is easier, as it only configures a network like 10.10.10.0/24 - a 24 bit network:

default-lease-time 86400;
max-lease-time 86400;
option subnet-mask 255.255.255.0;
option broadcast-address 10.10.10.255;
option domain-name "local.lan";
authoritative;
subnet 10.10.10.0 netmask 255.255.255.0 {
    range 10.10.10.10 10.10.10.254;
    option routers 10.10.96.1;
    option domain-name-servers 10.10.96.1;
}

This sets the dhcp range to start at 10.10.10.10 and end at 10.10.10.254. This gives 9 IP addresses that will never be assigned - which is useful if you need to assign static IP addresses to any machines.

Static IP Addresses

To configure the DHCP server to hand out a static IP address lease, place a reservation at the end of /etc/dhcp/dhcpd.conf:

host miami {
    hardware ethernet AA:BB:AB:BA:DE:AD:BE:EF
    fixed-address 10.10.97.4
}

This will give the host miami with the mac address specified the fixed IP address of 10.10.97.4.

Restarting DHCP Server

To restart the DHCP server once you have made changes:

$ systemctl restart isc-dhcp-server



Related

See the Template:LinuxNetworkingFlag page for more links.