Bootstrap FreeKB - DHCP - Install and configure a DHCP server on Linux
DHCP - Install and configure a DHCP server on Linux

Updated:   |  DHCP articles

Use apt-get or yum to install DHCP server.

~]# yum install dhcp

 

The DHCP server IPv4 configuration file is located at /etc/dhcp/dhcpd.conf. By default, this file is empty. An example configuration file is at /usr/share/doc/dhcp-<verison>/dhcpd.conf.sample. The configuration file should at least have the following.

  • option routers should be the IP address of the router in your network
  • option domain-name-servers should be the hostname or IP address of your nameservers
  • Range is the range of IP addresses that can be leased by clients. In this example, IP addresses 192.168.0.1 through 192.168.0.99 cannot be leased, so that these IP addresses can be used as static IP addresses for servers and infrastructure devices.
subnet 192.168.0.0 netmask 255.255.255.0 {
  option routers                  192.168.0.1;
  option subnet-mask              255.255.255.0;
  option domain-name-servers      dns1.example.com, dns2.example.com;
  default-lease-time              43200;
  max-lease-time                  86400;
  range                           192.168.0.100 192.168.0.254;
}

 

The IPv6 configuration file is located at /etc/dhcp/dhcpd6.conf. By default, this file is empty. An example configuration file is at /usr/share/doc/dhcp-<verison>/dhcpd6.conf.example. The configuration file should at least have the following.

subnet6 fd00::/8 {
        range6 fd00:: fd00:ffff:ffff:ffff:ffff:ffff:ffff:ffff;
        option dhcp6.name-servers fd00:0:0:1::1;
        option dhcp6.domain-search "domain.example";
}

 

Allow DHCP in iptables or firewalld.

The ps command can be used to determine if your system is using init or systemd. If PID 1 is init, then you will use the service command. If PID 1 is systemd, then you will use the systemctl command.

If your system is using systemd, use the systemctl command to start and enable sshd.

systemctl enable dhcpd
systemctl start dhcpd
systemctl status dhcpd

 

If your system is using init, use the chkconfig and service commands to start and enable sshd.

chkconfig dhcpd on
service dhcpd start
service dhcpd status

 

Client lease Windows

Ensure the Windows client does not have a static IP address and also is pointing to DNS servers that are able to resolve the IP address of the DHCP server. In this example, 10.7.67.117 is the IP address of a local DNS server that contains the lookup information for the DHCP server.

 

Use the ipconfig /release and then ipconfig /renew command, and then verify IP address of the Linux DHCP server is listed. Also, the Windows client should now have leased an IP address, subnet mask, and default gateway from the Linux DHCPD server.

> ipconfig /all
. . .
DHCP server . . . . . . . . . : 10.7.67.4

 

The dhcpd.leases file on the DHCPD server should contain a record with the IP address that was leased to the Windows client.

~]# cat /var/lib/dhcpd/dhcpd.leases

lease 10.7.67.232 {
  starts 6 2016/10/01 11:48:24;
  ends 6 2016/10/02 11:48:24;
  tstp 6 2016/10/01 23:48:24;
  cltt 6 2016/10/01 23:48:24;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:11:22:33:44:55;
  uid "\001\300\275\321\270\247\236";
  client-hostname "WINDOWS-PC";
}

 

Client lease Linux

On a Linux PC that is configured to use DHCP, ensure the avahi-daemon is not active and disabled.

~]# sudo systemctl stop avahi-daemon
~]# sudo systemctl disable avahi-daemon

 

Use the dhclient command to lease an IP address from the DHCP server (replace wlan0 with the actual name of the interface). 

~]# dhclient wlan0

 

Use the nmcli dev list iface command to verify the client received an IP address from the DHCP server.

~]# nmcli dev list iface eth0
IP4.ADDRESS[1]:                         ip = 192.168.0.166/24, gw = 192.168.0.1
IP4.DNS[1]:                             192.168.0.6
IP4.DOMAIN[1]:                          software.eng.apl
DHCP4.OPTION[1]:                        domain_name = software.eng.apl
DHCP4.OPTION[2]:                        expiry = 1492304213
DHCP4.OPTION[3]:                        broadcast_address = 192.168.0.255
DHCP4.OPTION[4]:                        dhcp_message_type = 5
DHCP4.OPTION[5]:                        dhcp_lease_time = 43200
DHCP4.OPTION[6]:                        ip_address = 192.168.0.166
DHCP4.OPTION[7]:                        routers = 192.168.0.1
DHCP4.OPTION[8]:                        subnet_mask = 255.255.255.0
DHCP4.OPTION[9]:                        domain_name_servers = 192.168.0.6
DHCP4.OPTION[10]:                       network_number = 192.168.0.0
DHCP4.OPTION[11]:                       dhcp_server_identifier = 192.168.0.4

 

Back on the Linux DHCP server, the /var/lib/dhcpd/dhcpd.leases file will contain a block for each client in the network that has leased an IP address from the DHCP server.

~]# cat /var/lib/dhcpd/dhcpd.leases

lease 192.168.0.101 {
  starts 6 2016/10/01 11:48:24;
  ends 6 2016/10/02 11:48:24;
  tstp 6 2016/10/01 23:48:24;
  cltt 6 2016/10/01 23:48:24;
  binding state active;
  next binding state free;
  rewind binding state free;
  hardware ethernet 00:11:22:33:44:55;
  uid "\001\300\275\321\270\247\236";
  client-hostname "android-584f5d1d8v1d8d8f4f1d5df";
}

 

LOG FILE

If there is some problem, check the log file. The following command can be used to watch the log file in real time. A successful lease should produce 4 events, DHCPDISCOVER, DHCPREQUEST, DHCPOFFER, DHCPACK.

~]# tail -f /var/log/messages | grep dhcpd
Jun 25 17:29:45 dhcp1 dhcpd: DHCPDISCOVER from b0:72:bc:e1:91:5d (Samsung) via eno16777984
Jun 25 17:29:45 dhcp1 dhcpd: DHCPOFFER on 192.168.0.101 to b0:72:bc:e1:91:5d (Samsung) via eno16777984
Jun 25 17:29:45 dhcp1 dhcpd: DHCPREQUEST for 192.168.0.101 (192.168.0.4) from b0:72:bc:e1:91:5d (Samsung) via eno16777984
Jun 25 17:29:45 dhcp1 dhcpd: DHCPACK on 192.168.0.101 to b0:72:bc:e1:91:5d (Samsung) via eno16777984

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 23d14e in the box below so that we can be sure you are a human.