How to set up DHCP service

1. Install DHCP server program

Firstly, you need to check if DHCP server is installed or not:

$ rpm -qa | grep 'dhcp'

If you see in the output a line like this:

dhcp-3.0.6-12.fc8

then you have already installed the service. Otherwise, you need to run the following command to install the service (first you need to switch your user account to the root user):

$ su -

Password:

# yum install dhcp

2. Edit DHCP service configuration file

Now it is time to edit the configuration file for DHCP service. The configuration file is /etc/dhcpd.conf. It is empty in the beginning, but you can copy a sample file at /usr/share/doc/dhcp-3.0.6/dhcp.conf.sample to /etc directory as the starting point (you may have a different version number):

# cp /usr/share/doc/dhcp-3.0.6/dhcpd.conf.sample /etc/dhcpd.conf

The sample configuration file with some explanations are listed here:

  1. ddns-update-style interim;
  2. ignore client-updates;
  3. subnet 192.168.0.0 netmask 255.255.255.0 {
  4. # --- default gateway
  5. option routers 192.168.0.1;
  6. option subnet-mask 255.255.255.0;
  7. option nis-domain "domain.org";
  8. option domain-name "domain.org";
  9. option domain-name-servers 192.168.0.1;
  10. option time-offset -18000; # Eastern Standard Time
  11. # option ntp-servers 192.168.0.1;
  12. # option netbios-name-servers 192.168.0.1;
  13. # --- Selects point-to-point node (default is hybrid). Don't change this unless
  14. # -- you understand Netbios very well
  15. # option netbios-node-type 2;
  16. range dynamic-bootp 192.168.0.128 192.168.0.254;
  17. default-lease-time 21600;
  18. max-lease-time 43200;
  19. # we want the nameserver to appear at a fixed address
  20. host ns {
  21. next-server marvin.redhat.com;
  22. hardware ethernet 12:34:56:78:AB:CD;
  23. fixed-address 207.175.42.254;
  24. }
  25. }

Most options are self-explaining. For example:

  1. line 4 specifies the network address and netmask of the private network;
  2. Line 7 specifies the IP address of the default router;
  3. Line 12 specifies the IP address of the DNS server. If you install a cache-only DNS server on the DHCP server as well, you can put the IP address of the DHCP server; otherwise, you can find the IP address of the DNS server in file /etc/resolv.conf. You can put more than one DNS server's IP address separated by comma;
  4. Line 21 specifies the range of IP addresses to be dynamically allocated. Usually, the router is assigned 192.169.0.1, and other servers may be assigned other static addresses. In this example, the address range 192.168.0.128 to 192.168.0.254 are dynamically allocated (192.168.0.255 is the local broadcast address);
  5. Line 22 specifies the default lease time of the IP address in units of seconds. The default value is 6 hours;
  6. Line 26 to 30 is a ns block for statically assigned IP address. You can use one ns block for each server that needs a fixed IP address (DNS server, NIS server, etc.). You need to specify its MAC address and fixed IP address.

You can also type the following command to get detailed explanations about these options:

$ man dhcpd.conf

3. Run DHCP service

Before we run DHCP service, we need to make sure the lease database file is created:

# touch /var/lib/dhcpd/dhcpd.leases

We can use chkconfig to run DHCP service on boot:

# chkconfig dhcpd on

Then, we can use service command to start DHCP server:

# service dhcpd start

Starting dhcpd: [ OK ]

4. Security

If you have more than one NIC, usually you want to limit DHCP service for only one NIC (the one that connects the private network). Suppose that NIC is eth1, then you can add the following line to file /etc/sysconfig/dhcpd:

DHCPDARGS=eth1