# DHCP

*<mark style="color:purple;">**SERVER SIDE:**</mark>*

In my lab i will be using ubuntu server as my dhcp server and centos a sthe dhcp client

`sudo apt install isc-dhcp-server`

* *for recovery purposes*

`sudo mv dhcpd.conf dhcpd.conf.bak`

`sudo vim dhcpd.conf`

```bash
# Default lease time
default-lease-time 28800;
max-lease-time 86400;

# Home lab network
subnet 172.16.1.0 netmask 255.255.255.0 {
        range 172.16.1.100 172.16.1.200;
        option subnet-mask 255.255.255.0;
        option routers 127.16.1.100;
        option domain-name "homelab.lan";
        option domain-name-servers ns1.homelab.lan;
}
```

{% hint style="info" %}
**Now i have to add a network interface inside my Vmware setting for ubuntu server machine**
{% endhint %}

A network interface with ens37 name has been added to this machine ( same thing to do for centos machine)

Netplan is responsible for network configuration in my machine:

`sudo vim /etc/netplan/00-installer-config.yaml`

```bash
# This is the network config written by 'subiquity'
network:
  ethernets:
    ens33:
      addresses:
      - 192.168.1.5/24
      nameservers:
        addresses:
        - 8.8.8.8
        search: []
      routes:
      - to: default
        via: 192.168.1.1
    ens37:
      addresses:
      - 172.16.1.100/24
      nameservers:
        addresses:
        - 192.168.1.9
        search: []
  version: 2
```

As you can see, i have added a network interface configuration for ens37

> <mark style="color:red;">Note: I didn’t add a route to the gateway because this will issue a conflict between this interfaces Instead I am planning to use my ubuntu server as a router too and let the forwarding from ens37 to ens33 so any client that takes address from this interface will reach the internet via my home router (not ubuntu server = 192.168.1.1)</mark>

And you can see that i have put the default route for these clients to be the same interface that gives dhcp which is ens37=172.16.1.100

*<mark style="color:purple;">**Client Side:**</mark>*

```bash
[k8s@k8s-manager ~]$ nmcli device  status
DEVICE  TYPE      STATE                   CONNECTION
ens33   ethernet  connected               ens33
lo      loopback  connected (externally)  lo
ens36   ethernet  disconnected            --
```

i am planning to use ens36 so i have to add it

`nmcli connection add con-name ens36 ifname ens36 type ethernet`

and VOILAA, ens36 will take ip address automatically since dhcp use broadcast

```bash
[k8s@k8s-manager ~]$ ifconfig ens36
ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 172.16.1.102  netmask 255.255.255.0  broadcast 172.16.1.255
        inet6 fe80::6f64:e247:657b:7b9f  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:66:54:2c  txqueuelen 1000  (Ethernet)
        RX packets 336  bytes 45221 (44.1 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 30  bytes 4538 (4.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
```

*<mark style="color:purple;">**Configure Routing**</mark>*

Before we start configuring this, let me prove that ens36 can’t reach the internet:

```bash
[k8s@k8s-manager ~]$ ping -I ens36 google.com
PING google.com (216.58.212.110) from 172.16.1.102 ens36: 56(84) bytes of data.
From k8s-manager (172.16.1.102) icmp_seq=1 Destination Host Unreachable
From k8s-manager (172.16.1.102) icmp_seq=2 Destination Host Unreachable
From k8s-manager (172.16.1.102) icmp_seq=3 Destination Host Unreachable
From k8s-manager (172.16.1.102) icmp_seq=4 Destination Host Unreachable
From k8s-manager (172.16.1.102) icmp_seq=5 Destination Host Unreachable
From k8s-manager (172.16.1.102) icmp_seq=6 Destination Host Unreachable
^C
--- google.com ping statistics ---
7 packets transmitted, 0 received, +6 errors, 100% packet loss, time 6161ms
pipe 3
[k8s@k8s-manager ~]$ ping -I ens33 google.com
PING google.com (216.58.212.110) from 192.168.1.16 ens33: 56(84) bytes of data.
64 bytes from lhr35s06-in-f110.1e100.net (216.58.212.110): icmp_seq=1 ttl=115 time=29.2 ms
64 bytes from mrs09s12-in-f14.1e100.net (216.58.212.110): icmp_seq=2 ttl=115 time=28.3 ms
64 bytes from lhr35s06-in-f14.1e100.net (216.58.212.110): icmp_seq=3 ttl=115 time=29.4 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 28.272/28.953/29.421/0.492 ms
```

Now let’s start setting up our machine to act like a router, matter of fact turn it into a router

* Enable IP Forwarding

  Edit the **`/etc/sysctl.conf`** file and uncomment the following line
* Apply it :

  ```bash
  touk@ubuntu-server:/etc/dhcp$ sudo sysctl -p
  net.ipv4.ip_forward = 1
  ```
