【Linux】搭建 DHCP 服务器

Posted by 西维蜀黍 on 2021-04-10, Last Modified on 2021-09-21

Installing DHCP Server

Before proceeding towards installing a DHCP server, first update the packages by running the following command in Terminal:

$ sudo apt-get update

Then run the following command in the Terminal to install DCHP server:

$ sudo apt-get install isc-dhcp-server -y

Configuring DHCP Server

When the installation completes, edit the file /etc/default/isc-dhcp-server to define the interfaces DHCPD should use to serve DHCP requests, with the INTERFACES option.

For example, if you want the DHCPD daemon to listen on eth0, set it like so:

INTERFACES="eth0"

DHCP configuration file is located at /etc/dhcp/dhcpd.conf. We can open this file by running the following command in Terminal

$ sudo vim /etc/dhcp/dhcpd.conf

Define Misc

To specify the domain name servers, add the following line:

option domain-name "example.lab";
option domain-name-servers 8.8.8.8, 8.8.4.4;

To specify lease time:

default-lease-time 3600; 
max-lease-time 7200;

Uncomment authoritative; because it will be the only DHCP server on the network.

authoritative;

Defining the Subnet

subnet 192.168.10.0 netmask 255.255.255.0 {
        option routers                  192.168.10.1;
        option subnet-mask              255.255.255.0;
        option domain-search            "sw.lan";
        option domain-name-servers      192.168.10.1;
        range   192.168.10.10   192.168.10.100;
        range   192.168.10.110   192.168.10.200;
}

Apply the changes by running the command:

sudo systemctl restart isc-dhcp-server.service

Test

Test a client

Check to see if a client on the same network with its adapter set to DHCP can obtain an IP address. As you can see in the screenshot below, a Windows 10 virtual machine on the same network has been assigned an IP address of 10.1.1.135.

Running the dhcp-lease-list command also confirms this.

ubuntu@ubuntu:~$ dhcp-lease-list

Reference