Network Services

DHCP to the Rescue…using Linux

From home users to advance IT professionals – All of us have ran across obtaining an IP address dynamically for our devices; whether if it’s from our ISP for Internet home connectivity, when we connect to WiFi at our favourite coffee shop (Dunkin Cold Brew for me), or even when a new client is brought onto the network within a company. Dynamically getting an IP saves some time and removes configuration error by the end-user.
In this case less IS more!

For network professionals, using a Dynamic Host Configuration Protocol (DHCP) server on Enterprises networks is very valuable, not only for the distribution of IP addresses dynamically for a given VLAN, VRF, or any of the like – but also for assigning Default Gateway (DG), DNS, and NTP servers to end devices. It can be useful for home and lab networks for testing and training purposes. Nowadays DHCP comes in quite handy when you need to set ‘DHCP Options’ for specific tasks – especially when it comes to IT Automation.

In this Blog post, I’ll show you how to setup a DHCP server on CentOS 7 (This is my flavour of Linux, by the way). Before I begin – why Linux over a Windows DHCP server?  We’ll, in my opinion – I have greater control and, plus, it’s free! 🙂

Before we begin here’s a basic diagram of what we’re trying to accomplish:

My DHCP server have two connections – one for OOBM and the other for the internal network where it will dynamically assign IPs. And I have hosts (Linux) with a connection on the internal network segment.

We want our Clients to obtain the following:
– An IP address from the range 171.16.5.100 to 172.16.5.110 on the 172.16.5.0/24 subnet
– Default gateway of 172.16.5.1/24
– Domain of lab.aha.local
– DNS and NTP servers information of 172.16.5.10 & 172.16.5.11, respectively
– Assign centos-2 an ip address of 172.16.5.120/24 based on its MAC address

Before we begin, this is what my two Linux host interfaces look like Centos-1 and Centos-2, respectively:


Last login: Mon Jan 29 21:20:50 2018
[root@centos-1 ~]# ifconfig ens33
flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.92.129 netmask 255.255.255.0 broadcast 192.168.92.255inet6 fe80::a8a:a67:adf6:9485 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:2a:ec:f9 txqueuelen 1000 (Ethernet)RX packets 126 bytes 14287 (13.9 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 109 bytes 12290 (12.0 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500ether 00:0c:29:2a:ec:03 txqueuelen 1000 (Ethernet)RX packets 28 bytes 9576 (9.3 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 54 bytes 10020 (9.7 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 
[root@centos-1 ~]#

Last login: Mon Jan 29 21:21:10 2018
[root@centos-2 ~]# ifconfig ens33
flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.92.131 netmask 255.255.255.0 broadcast 192.168.92.255inet6 fe80::98ac:e57d:f0d9:60fe prefixlen 64 scopeid 0x20<link>ether 00:0c:29:89:34:7f txqueuelen 1000 (Ethernet)RX packets 136 bytes 14798 (14.4 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 133 bytes 14494 (14.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet6 fe80::f61e:ec88:477e:e030 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:89:34:89 txqueuelen 1000 (Ethernet)RX packets 23 bytes 7866 (7.6 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 69 bytes 13318 (13.0 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 0 bytes 0 (0.0 B)RX errors 0 dropped 0 overruns 0 frame 0TX packets 0 bytes 0 (0.0 B)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos-2 ~]#

Notice: Take note of the MAC address of Centos-2

Task 1: Download and install CentOS 7 from: https://www.centos.org/download/
I normally choose the ‘Minimal ISO’ because it’s about 800MB compared to 8Gigs of the ‘Everything ISO’

Once installed.  It’s best to perform an update and install net-tools

1
2
[root@dhcp~]# yum -y update
[root@dhcp~]# yum –y install net-tools

I’ll issue ifconfig to see our interfaces:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
[root@dhcp ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 192.168.92.132  netmask 255.255.255.0  broadcast 192.168.92.255inet6 fe80::de60:c0aa:2473:a280  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:26:ff:c7  txqueuelen 1000  (Ethernet)RX packets 436  bytes 45844 (44.7 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 227  bytes 41386 (40.4 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500inet 172.16.5.2  netmask 255.255.255.0  broadcast 172.16.5.255inet6 fe80::e28a:a73:4e9e:420e  prefixlen 64  scopeid 0x20<link>ether 00:0c:29:26:ff:d1  txqueuelen 1000  (Ethernet)RX packets 0  bytes 0 (0.0 B)RX errors 0  dropped 0  overruns 0  frame 0TX packets 178  bytes 10824 (10.5 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536inet 127.0.0.1  netmask 255.0.0.0inet6 ::1  prefixlen 128  scopeid 0x10<host>loop  txqueuelen 1  (Local Loopback)RX packets 97  bytes 9939 (9.7 KiB)RX errors 0  dropped 0  overruns 0  frame 0TX packets 97  bytes 9939 (9.7 KiB)TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0[root@dhcp ~]#

Task 2: Install DHCP Services
Use the following syntax to install dhcp services onto your DHCP host:

1[root@dhcp~]# yum -y install dhcp

Task 3: Edit dhcpd.conf file
Once Task 2 is complete, we’ll need to edit the dhcpd.conf file located here:

1
2
[root@dhcp~]# cd /etc/dhcp
[root@dhcp~]# vi dhcpd.conf

Edit to add the following configurations:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
subnet 172.16.5.0 netmask 255.255.255.0 {range 172.16.5.100 172.16.5.110;default-lease-time 600;max-lease-time 7200;option routers 172.16.5.1;option broadcast-address 172.16.5.255;option ntp-servers 172.16.5.10;option domain-name-servers 172.16.5.11, 8.8.8.8;} host centos-2 {hardware ethernet 00:0c:29:89:34:89;fixed-address 172.16.5.120;}:wq!

Let’s break this down:

subnet 172.16.5.0 netmask 255.255.255.0 {   This the Subnet declaration (notice the open curly brace)
range 172.16.5.100 172.16.5.110;     This is the subnet ‘range’ parameter.  Clients will get an IP in this range
option routers, ntp-servers, domain-name-servers  These are global ‘option’ parameters for every client in the subnet.
Default/max-lease-time – Are additional parameters for every client in the subnet.

Now for host centos-2 entry. Here’s where we’ll assign a fixed IP to that client based on its MAC address in lieu of an IP lease.

On a side note – DHCP also uses the file /var/lib/dhcpd/dhcpd.leases to store the client lease database

Task 4: Start DHCP services
In order to start our DHCP services, we’ll enter the following command:

1[root@dhcp ~]# systemctl start dhcpd

To check the status of it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@dhcp ~]# systemctl status dhcpd● dhcpd.service - DHCPv4 Server DaemonLoaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)Active: active (running) since Sun 2018-01-28 18:40:17 EST; 5s agoDocs: man:dhcpd(8)man:dhcpd.conf(5)Main PID: 1352 (dhcpd)Status: "Dispatching packets..."CGroup: /system.slice/dhcpd.service└─1352 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: Sending on LPF/ens34/00:0c:29:26:ff:d1/172.16.5.0/24Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]:Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: No subnet declaration for ens33 (192.168.92.132).Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: ** Ignoring requests on ens33. If this is not whatJan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: you want, please write a subnet declarationJan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: in your dhcpd.conf file for the network segmentJan 28 18:40:17 dhcp.lab.aha.local systemd[1]: Started DHCPv4 Server Daemon.Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: to which interface ens33 is attached. **Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]:Jan 28 18:40:17 dhcp.lab.aha.local dhcpd[1352]: Sending on Socket/fallback/fallback-net[root@dhcp ~]#

If you notice in my example, ens33 (OOBM) shows no subnet declaration – this is okay since ens34 is the interface connected into our internal network.  As you can see the Active state is active (running).

Task 5: Start up your Clients and run the DHCP status command
When you start up your clients and run the DHCP status command you’re output should be similar to this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
[root@dhcp ~]# systemctl status dhcpd● dhcpd.service - DHCPv4 Server DaemonLoaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)Active: active (running) since Sun 2018-01-28 18:40:17 EST; 4h 7min agoDocs: man:dhcpd(8)man:dhcpd.conf(5)Main PID: 1352 (dhcpd)Status: "Dispatching packets..."CGroup: /system.slice/dhcpd.service└─1352 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPDISCOVER from 00:0c:29:89:34:89 via ens34Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPOFFER on 172.16.5.120 to 00:0c:29:89:34:89 via ens34Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPREQUEST for 172.16.5.120 (172.16.5.2) from 00:0c:29:89:34:89 via ens34Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPACK on 172.16.5.120 to 00:0c:29:89:34:89 via ens34Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPOFFER on 172.16.5.100 to 00:0c:29:2a:ec:03 (centos-1) via ens34Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: Wrote 0 deleted host decls to leases file.Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: Wrote 0 new dynamic host decls to leases file.Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: Wrote 1 leases to leases file.Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPREQUEST for 172.16.5.100 (172.16.5.2) from 00:0c:29:2a:ec:03 (centos-1) via ens34Jan 28 22:47:03 dhcp.lab.aha.local dhcpd[1352]: DHCPACK on 172.16.5.100 to 00:0c:29:2a:ec:03 (centos-1) via ens34[root@dhcp ~]#

The first entry shows it discovers the client MAC of 00:0c:29:89:34:89 (Centos 2 ens37 interface)
The second entry show what the DHCP Server is offering (172.16.5.120) and the subsequent acceptance of it.
The fifth entry shows its offering 172.16.5.100 to MAC 00:0c:29:2a:ec:03 (Centos 1 ens37 interface) and the subsequent acceptance and writing to the lease file.

And now, my clients are all setup 🙂 :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@centos-1 ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.92.129 netmask 255.255.255.0 broadcast 192.168.92.255inet6 fe80::a8a:a67:adf6:9485 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:2a:ec:f9 txqueuelen 1000 (Ethernet)RX packets 62 bytes 7945 (7.7 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 74 bytes 8752 (8.5 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.16.5.100 netmask 255.255.255.0 broadcast 172.16.5.255inet6 fe80::e537:8fc0:6ae6:a53 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:2a:ec:03 txqueuelen 1000 (Ethernet)RX packets 28 bytes 2246 (2.1 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 24 bytes 2150 (2.0 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 72 bytes 6464 (6.3 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 72 bytes 6464 (6.3 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos-1 ~]#
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
[root@centos-2 ~]# ifconfigens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.92.131 netmask 255.255.255.0 broadcast 192.168.92.255inet6 fe80::98ac:e57d:f0d9:60fe prefixlen 64 scopeid 0x20<link>ether 00:0c:29:89:34:7f txqueuelen 1000 (Ethernet)RX packets 79 bytes 9694 (9.4 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 82 bytes 9396 (9.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 172.16.5.120 netmask 255.255.255.0 broadcast 172.16.5.255inet6 fe80::f61e:ec88:477e:e030 prefixlen 64 scopeid 0x20<link>ether 00:0c:29:89:34:89 txqueuelen 1000 (Ethernet)RX packets 59 bytes 4668 (4.5 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 25 bytes 2238 (2.1 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536inet 127.0.0.1 netmask 255.0.0.0inet6 ::1 prefixlen 128 scopeid 0x10<host>loop txqueuelen 1 (Local Loopback)RX packets 72 bytes 6464 (6.3 KiB)RX errors 0 dropped 0 overruns 0 frame 0TX packets 72 bytes 6464 (6.3 KiB)TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 [root@centos-2 ~]#

Have fun and Enjoy!

Leave a Reply

Your email address will not be published. Required fields are marked *