> For the complete documentation index, see [llms.txt](https://riteshs4hu.gitbook.io/infosec-notes/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://riteshs4hu.gitbook.io/infosec-notes/linux-server-administrator/servers-configurations-centos7/dynamic-host-configuration-protocol-dhcp.md).

# Dynamic Host Configuration Protocol (DHCP)

DHCP Server is a protocol used for IP address distribution. It uses the DORA process to assign IP addresses dynamically.

### DORA Process

DORA stands for **Discovery**, **Offer**, **Request**, and **Acknowledge**. This process is essential for a DHCP server to assign an IP address to a device connecting to a network.

#### Steps:

1. **Discovery**: The device broadcasts a message to find available DHCP servers.
2. **Offer**: A DHCP server responds with an IP address offer.
3. **Request**: The device requests the offered IP address.
4. **Acknowledge**: The server assigns the IP address, and the device can use it.

***

### DHCP Configuration

* Install DHCP

```bash
yum install dhcp*
```

* Start DHCP Service

```bash
systemctl enable dhcpd.service
systemctl start dhcpd.service
```

* Allow DHCP Port on Firewall

```bash
iptables -A INPUT -p udp --dport 67 -j ACCEPT
iptables-save
```

#### Configure DHCP

* Edit the DHCP configuration file:

```bash
vim /etc/dhcp/dhcp.config
```

* **Distribute IP Addresses**

```conf
authoritative;

subnet 192.168.43.0 netmask 255.255.255.0 {
    range 192.168.43.100 192.168.43.150;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
    option routers 192.168.43.1;
    option broadcast-address 192.168.43.255;
    default-lease-time 600;
    max-lease-time 7200;
}
```

* **Excluded IP Addresses**

```conf
authoritative;

subnet 192.168.43.0 netmask 255.255.255.0 {
    range 192.168.43.10 192.168.43.50;
    range 192.168.43.100 192.168.43.150;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
    option routers 192.168.43.1;
    option broadcast-address 192.168.43.255;
    default-lease-time 600;
    max-lease-time 7200;
}
```

* **Reserved IP Addresses**

```conf
authoritative;

subnet 192.168.43.0 netmask 255.255.255.0 {
    range 192.168.43.10 192.168.43.50;
    range 192.168.43.100 192.168.43.150;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
    option routers 192.168.43.1;
    option broadcast-address 192.168.43.255;
    default-lease-time 600;
    max-lease-time 7200;
}

host hostname {
    hardware ethernet Reserved_IP_address_Macaddress;
    fixed-address 192.168.43.10;
}
```

* **Blacklist IP Addresses**

```conf
authoritative;

subnet 192.168.43.0 netmask 255.255.255.0 {
    range 192.168.43.10 192.168.43.50;
    range 192.168.43.100 192.168.43.150;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
    option routers 192.168.43.1;
    option broadcast-address 192.168.43.255;
    default-lease-time 600;
    max-lease-time 7200;
}

host anyname {
    hardware ethernet Black_IP_Address_Macaddress;
    deny booting;
}
```

* **Whitelist IP Addresses**

```conf
authoritative;

subnet 192.168.43.0 netmask 255.255.255.0 {
    range 192.168.43.10 192.168.43.50;
    range 192.168.43.100 192.168.43.150;
    option domain-name-servers 8.8.8.8, 8.8.4.4;
    option routers 192.168.43.1;
    option broadcast-address 192.168.43.255;
    default-lease-time 600;
    max-lease-time 7200;
    deny unknown-clients;
}

host anyname {
    hardware ethernet macaddress;
}
```
