Configuring the /etc/dhcp/dhclient.conf File on Raspberry Pi

Ben
Ben
@benjislab

The /etc/dhcp/dhclient.conf file is a configuration file used by the DHCP client (dhclient) on your Raspberry Pi to control how it interacts with the DHCP server when requesting network configuration parameters. This file is essential for managing dynamic IP address assignments and customizing how your Raspberry Pi handles DHCP leases and options.

What is the /etc/dhcp/dhclient.conf File?

The dhclient.conf file provides a flexible way to customize the behavior of the DHCP client on your Raspberry Pi. This file allows you to define which options to request from the DHCP server, how to handle DNS servers, network routes, lease times, and much more. While the default configuration is sufficient for most users, advanced users can fine-tune network settings to suit specific needs.

Example /etc/dhcp/dhclient.conf File

Here is an example of a typical /etc/dhcp/dhclient.conf file:

# Configuration file for /sbin/dhclient.
#
# This is a sample configuration file for dhclient. See dhclient.conf's
#       man page for more information about the syntax of this file
#       and a more comprehensive list of the parameters understood by
#       dhclient.
#
# Normally, if the DHCP server provides reasonable information and does
#       not leave anything out (like the domain name, for example), then
#       few changes must be made to this file, if any.
#

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

send host-name = gethostname();
request subnet-mask, broadcast-address, time-offset, routers,
        domain-name, domain-name-servers, domain-search, host-name,
        dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
        netbios-name-servers, netbios-scope, interface-mtu,
        rfc3442-classless-static-routes, ntp-servers;

#send dhcp-client-identifier 1:0:a0:24:ab:fb:9c;
#send dhcp-lease-time 3600;
#supersede domain-name "fugue.com home.vix.com";
#prepend domain-name-servers 127.0.0.1;
#require subnet-mask, domain-name-servers;
#timeout 60;
#retry 60;
#reboot 10;
#select-timeout 5;
#initial-interval 2;
#script "/sbin/dhclient-script";
#media "-link0 -link1 -link2", "link0 link1";
#reject 192.33.137.209;

#alias {
#  interface "eth0";
#  fixed-address 192.5.5.213;
#  option subnet-mask 255.255.255.255;
#}

#lease {
#  interface "eth0";
#  fixed-address 192.33.137.200;
#  medium "link0 link1";
#  option host-name "andare.swiftmedia.com";
#  option subnet-mask 255.255.255.0;
#  option broadcast-address 192.33.137.255;
#  option routers 192.33.137.250;
#  option domain-name-servers 127.0.0.1;
#  renew 2 2000/1/12 00:00:01;
#  rebind 2 2000/1/12 00:00:01;
#  expire 2 2000/1/12 00:00:01;
#}

In this example, the configuration includes both active settings and commented-out examples that can be customized for specific needs.

Key Parameters in /etc/dhcp/dhclient.conf

The dhclient.conf file can be tailored to customize DHCP behavior, including which options are requested from the DHCP server, how certain DHCP options are handled, and how network interfaces are configured. Below are some of the key parameters and their purposes.

1. Requesting DHCP Options

The request directive specifies which options the DHCP client should ask for from the DHCP server.

request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, domain-search, host-name,
dhcp6.name-servers, dhcp6.domain-search, dhcp6.fqdn, dhcp6.sntp-servers,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes, ntp-servers;
  • subnet-mask: The network mask for the local network.
  • routers: The default gateway for outbound traffic.
  • domain-name-servers: The DNS servers to use.
  • ntp-servers: Network Time Protocol (NTP) servers to synchronize the system clock.

2. Sending DHCP Options

The send directive allows you to send specific information to the DHCP server.

send host-name = gethostname();
  • host-name: Sends the hostname of the Raspberry Pi to the DHCP server. The gethostname() function retrieves the current hostname of the system.

3. Classless Static Routes (RFC 3442)

The option directive can define custom options. In this example, it defines the RFC 3442 classless static routes option.

option rfc3442-classless-static-routes code 121 = array of unsigned integer 8;

This option allows the DHCP server to provide classless static routes to the client, which is useful for advanced network configurations.

4. Prepending DNS Servers

The prepend directive allows you to add DNS servers that should be preferred over those provided by the DHCP server.

#prepend domain-name-servers 127.0.0.1;

This is useful if you want to use a local DNS resolver or specific DNS servers in addition to those provided by the DHCP server.

5. Specifying Lease Times

You can control how long the DHCP client should request a lease for with the send dhcp-lease-time directive.

#send dhcp-lease-time 3600;

In this example, the lease time is set to 3600 seconds (1 hour).

6. Configuring Timeout and Retry Intervals

The timeout, retry, and reboot directives control how long the DHCP client should wait and how often it should retry obtaining a lease.

#timeout 60;
#retry 60;
#reboot 10;
  • timeout: The time in seconds to wait for a lease before giving up.
  • retry: The time in seconds between retries.
  • reboot: The time in seconds to wait before retrying if the system reboots.

7. Custom DHCP Scripts

The script directive allows you to specify a custom script to run when a DHCP lease is obtained or renewed.

#script "/sbin/dhclient-script";

You can use this to execute additional commands or scripts based on the network configuration provided by the DHCP server.

8. Rejecting Specific Servers

The reject directive can be used to reject offers from specific DHCP servers.

#reject 192.33.137.209;

This might be useful in environments with multiple DHCP servers where you want to prioritize or exclude specific ones.

Best Practices for Modifying /etc/dhcp/dhclient.conf

  • Backup Your Configuration: Always create a backup before making changes:
sudo cp /etc/dhcp/dhclient.conf /etc/dhcp/dhclient.conf.bak
  • Test Changes: After modifying the dhclient.conf file, restart the network interface to apply the changes:
sudo dhclient -r
sudo dhclient
  • Use Comments: Keep your configuration organized by using comments to explain why certain options are used.

  • Monitor Logs: Check the system logs (/var/log/syslog) for any DHCP-related messages after making changes to ensure everything works correctly.

Conclusion

The /etc/dhcp/dhclient.conf file is a powerful tool for customizing how your Raspberry Pi interacts with DHCP servers. Whether you're setting custom DNS servers, defining static routes, or optimizing lease times, understanding how to configure this file gives you greater control over your network configuration. As always, make changes carefully, test thoroughly, and keep backups to ensure your Raspberry Pi's network setup remains stable and reliable.