Skip to content

DHCP in Linux

image.png

image.png

image.png

  • Installation: Use the command sudo apt install isc-dhcp-server to install the daemon (dhcpd).
  • Logging: Server messages are sent to the system log at /var/log/syslog.
  • Configuration File: Primary settings are managed in /etc/dhcp/dhcpd.conf.
  • Restarting: Apply changes using sudo systemctl restart isc-dhcp-server.service.

The configuration file consists of statements divided into two categories:

  • Parameters: Define how or if to do something (e.g., lease duration, allowing unknown clients).
  • Declarations: Describe the network layout using keywords like subnet, host, group, and pool.
    • Key Global Parameters

      • default-lease-time: The duration a client gets if they do not request a specific time.
      • max-lease-time: The absolute limit for a lease; if a client requests more, they are capped at this value.
      • authoritative: Should be uncommented if this is the primary DHCP server for the local network.
    • Subnet & Host Declarations

      • subnet [addr] netmask [mask]: Describes the network segment for leasing.
      • range [first] [last]: Defines the pool of available addresses allocated sequentially.
      • option routers: Defines the default gateway for clients.
      • option domain-name-servers: Specifies DNS server addresses.
      • host [Name]: Assigns a predefined IP (fixed-address) to a specific device based on its hardware ethernet (MAC address).
    • Example: dhcpd.conf

      Terminal window
      default-lease-time 600;
      max-lease-time 7200;
      authoritative;
      subnet **192.168.1.0** netmask **255.255.255.0**
      {
      range **192.168.1.150 192.168.1.200**;
      option routers **192.168.1.254**;
      option domain-name-servers **192.168.1.1**, **192.168.1.2**;
      option domain-name **"mydomain.example"**;
      }
      host Client1
      {
      hardware ethernet 00:02:3f:3d:73:b3;
      fixed-address 192.168.1.100;
      }
  • Interfaces: Edit /etc/default/isc-dhcp-server to specify which network card (e.g., enp0s3) the server should listen on.
  • Pools: Used to treat groups of addresses differently within the same subnet.
  • Groups: Apply parameters to a collection of declarations not related by subnet.
  • Allow/Deny Unknown Clients: The unknown-clients flag determines if devices without a specific host declaration can receive an IP.
  • Ubuntu: Set dhcp4: true (or yes) within a Netplan YAML file in /etc/netplan/*.yaml.
  • CentOS: Edit the interface script (e.g., /etc/sysconfig/network-scripts/ifcfg-enp0s3) and set BOOTPROTO=dhcp.
  • Active Leases: Use dhcp-lease-list to see currently assigned IPs, hostnames, and expiration times.
  • Lease History: The file /var/lib/dhcp/dhcpd.leases stores the full history and current states (e.g., active, free) of all IP assignments.

image.png