How to build a DHCP server

Source: Internet
Author: User
Today I have reviewed the establishment of DHCP servers. the establishment of DHCP is simpler than I thought. However, it is estimated that there will be many knowledge points, just like DNS, A series of knowledge points will be pulled out in depth. Now, let's get to the point where I am writing my review notes ....

 

DHCP server construction

I have reviewed the establishment of DHCP today. it is easier to set up DHCP than I think, but it is estimated that there will be many knowledge points, just like DNS, A series of knowledge points will be pulled out in depth. Now, let's get down to it. now I am writing my review notes.

1. what is DHCP?

Dhcp is a Dynamic Host Configuration Protocol that automatically allocates TCP/IP information such as ip addresses, subnet masks, gateways, and DNS. When there are a lot of computers in the LAN, we can't set up IP addresses and other information next to the host on one of them. it's not exhausting. Therefore, you only need to set up a DHCP server in the LAN. through this server, you can dynamically set IP addresses and other information for each computer in the LAN.

II. how DHCP works

(1) first, let's introduce several explanations:

DHCP client: an internet host that obtains network configuration parameters through DHCP, usually a common user workstation.

DHCP server: the DHCP server provides network parameters to the DHCP client's internet host.

DHCP/BOOTP relay proxy: the host or router that forwards DHCP messages between the DHCP client and the server

DHCP is designed based on the client/server model. DHCP clients and DHCP servers send and receive DHCP messages for communication.

Scope: a complete and continuous thin available IP address. the DHCP service manages network distribution, IP address allocation, and related configuration parameters through scope.

Exclusion scope: the exclusion scope is a finite address sequence excluded from the DHCP service within the scope. the exclusion scope ensures that any address in the scope is not provided to the client by the server on the network. To put it bluntly, some IP addresses in the scope are excluded so that they cannot be allocated to the client.

Address pool: an available address pool formed by the remaining addresses within the scope after the DHCP scope is defined and the scope of the application is excluded. the addresses in the address pool can be dynamically allocated to the DHCP client.

Lease: the time when the client can dynamically allocate IP addresses.

Reservation: creates a permanent address lease from the DHCP server to the client. reservation ensures that the same IP address is always used for specific hardware devices on the subnet.

(2) Working principle

After knowing some names, let's talk about its basic principles:

DHCP is generally a communication protocol in the LAN. when the client does not set any network configuration information, he wants to send a broadcast packet to all hosts in the Lan (dhcp discver packet is sent to ensure that the server can receive the request, the packet source address is set to 0.0.0.0, and the destination address is 255.255.255.255. dhcp discover information is sent in broadcast format .), Generally, after the host receives the packet, it will discard it directly. However, if there is a DHCP server in the LAN, it will automatically respond.

After the DHCP server listens to the dhcp discover broadcast from the client, it selects available IP addresses and other TCP/IP settings from the IP address range that has not been rented out and sends them to the client in the form of dhcp offer packets.

If the client receives responses from multiple DHCP servers on the network, the client selects the fastest dhcp offer and sends a dhcp request broadcast packet to the network, the IP address provided by the DHCP server. At the same time, the client will also send ARP broadcast packets to the network to query whether other machines on the network use the IP address. if the IP address is occupied, the client sends a dhcp decline packet to the DHCP server, rejects dhcp offer, and resends the dhcp request information.

Note: In fact, not all DHCP clients accept the response from the DHCP server unconditionally. the client can retain some of its own TCP/IP settings, such as the gateway and DNS address.

After the IP address is assigned to the client, the DHCP server sends a dhcp ack message to confirm that the IP lease takes effect and ends the entire DHCP process.

After the DHCP client successfully obtains the IP address from the server, it generally does not need to send the dhcp discover information, unless the lease has expired or the IP address is reset to 0.0.0.0. In this case, the client directly sends a dhcp request to the DHCP server with the IP address that has been rented. the DHCP server tries its best to make the client use the original IP address, the system will directly respond to dhcp ack and allow the client to continue using the IP address. If the address is invalid or used by another host, the server returns a dhcp nack packet to the client and asks the client to re-execute dhcp discover.

Note: after the client executes dhcp discover, if the DHCP server does not respond to the client's request, the client will randomly configure the local address using an IP address in the 169.254.0.0/16 segment.

The general process of OK is as shown above, which is also excerpted by me. it seems to be more detailed. Simply put, in a network environment with a DHCP server, the client sends a broadcast with the destination address 255.255.255.255 to all hosts in the network. when other clients receive this message, they directly discard it, after receiving the message, the DHCP server randomly selects an IP address from its address pool to the client sending the message to configure its network parameters. after the client receives the message, process the network parameters, including the Gateway, DNS, and so on, and send a confirmation message to the server, indicating that the parameter has been accepted, but not to other clients.

Note that the client is offline, whether it is to disable the network interface (ifdown), restart (reboot), shutdown (shutdown), and so on, at this time, the server will reclaim the IP address and put it in its standby zone for future use. However, based on the principles described above, we know that "the DHCP server will try its best to allow the client to use the original IP address. if there are no special circumstances, it will directly respond to the dhcp ack, allow the client to continue using this IP address. "when you restart the network service, you will often find that the IP address has not changed. this is the reason.

3. configure the DHCP server

First of all, you must install DHCP. if you have a yum repository, directly

Yum install dhcp can be installed. First, check whether my host has been installed:

[Root @ linux102 dhcpd] # rpm-qa dhcp

Dhcp-3.0.5-21.el5

OK. I have installed it. So let's get started with configuration. To configure a DHCP server, follow these steps:

First edit/etc/dhcpd. conf

Configuring dhcp is usually to configure/etc/dhcpd. conf file, but this file does not exist in general cases. we need to manually configure it. there are quite a lot of stuff here, and it is a lot of effort to make it happen one by one, so we can copy a ready-made configuration file to the etc directory and rename it:

Cp/usr/share/doc/dhcp-3.0.5/dhcpd. conf. sample/etc/dhcpd. conf

After copying, we will have this file under/etc.

Hcpd. conf includes global configuration and local configuration. The global configuration can contain parameters or options. this part takes effect for the entire DHCP server. The local configuration is usually represented by the declaration part. this part takes effect only for the local configuration, for example, only for a specific IP scope.

Dhcpd. conf file format:

# Global configuration

Parameter or option; # Global

# Local Configuration

Statement {

Parameter or option; # Local effect

}

Next, we will explain some common parameters and declarations for global and local configuration.

1. common parameters:

(1) ddns-update-style (none | interim | ad-hoc)

Purpose: define the supported DNS Dynamic update types.

None: Dynamic update is not supported.

Interim: indicates the DNS interactive update mode.

Ad-hoc: indicates the special dns update mode.

Because the IP address obtained by the DHCP client is always changing, it is difficult to handle the correspondence between the host name and IP address. In this case, DHCP can update the host name and IP address through ddns.

Note: This option is a required parameter. the configuration file must contain this parameter and be placed on the first line.

(2) gnore client-updates

Purpose: ignore client updates

(3) default-lease-time number (number)

Purpose: define the default IP lease time

(4) max-lease-time number (number)

Purpose: defines the maximum IP lease time of a client. when the client exceeds the lease

The maximum time that an IP address can be used when the IP address is not updated;

Note: (3) and (4) are all lease times in seconds. this parameter can be used in global configuration or local configuration.

2. common statements

Declarations are generally used to specify the IP scope and define the IP address pool allocated to the client.

The declaration format is as follows:

Statement {

Option or parameter;

}

The common statements are as follows:

(1) subnet network No. netmask subnet mask {......}

Purpose: define the scope and specify the subnet.

Note: the network number must be the same as that of the DHCP server.

(2) starting IP address of range ending IP address

Purpose: specify the dynamic IP address range.

Note: You can specify multiple ranges in the subnet declaration, but the IP ranges defined by multiple ranges cannot be repeated.

3. Introduction to common options

Optional parameters are usually used to configure optional parameters for the DHCP client, such as defining the DNS address and default gateway of the client. All options start with the option keyword.

Common options are as follows:

(1) option routers IP address

Purpose: specify the default gateway for the client.

(2) option subnet-mask subnet mask

Purpose: Set the subnet mask of the client.

(3) option domain-name-servers IP address

Purpose: specify the DNS server address for the client.

(4) option broadcasst-address broadcast address:

Set the broadcast address. If this parameter is not set, the system automatically calculates the broadcast address based on the principles of class A, B, and C.

Note: (1), (2), and (3) options can be used in global configuration or local configuration.

OK. after that, let's see what the configuration file looks like.

 

The figure shows the configuration on my own server. Basically there are so many things. You can also add them as needed.

Here are a few more statements about obtaining a fixed ip address of dhcp:

(1) host name {......}

Purpose: defines the reserved address.

Note: This item is usually used with the subnet declaration.

(2) hardware addresses of the hardware type

Purpose: define the network interface type and hardware address. The common type is ethernet and the address is MAC.

(3) fixed-address IP address

Purpose: define the IP address specified by the DHCP client.

Note: (2) and (3) can only be applied to the host declaration.

After configuration, it is basically ready for use. However, if there are multiple network interfaces on the server, errors often occur. For example, 192.168.0.0/24 is eth0.

But 192.168.1.0/24 is on eth1. if the DHCP server listens to two interfaces at the same time, the client on 192.168.1.0/24 sends a dhcp packet, in this case, the ip address may be obtained from the ip address range 192.168.0.0/24 (the server sets the ip address range to 0). to prevent such a problem, we 'd better set the dhcp listening port.

[Root @ linux102] # vi/etc/sysconfig/dhcpd

# Command line options here

DHCPDARGS = "eth0"

After configuration, start the dhcp server:

 

After the port is started, let's observe the port startup status:

 

DHCP enables port 67. we can see that port 67 has been enabled on the server.

On the server side, you can query the number of clients that have applied for an ip address and the ip address records here:

[Root @ linux102 ~] # Cat/var/lib/dhcpd. leases

Lease 192.168.0.101 {// IP address of the client

Starts 5 06:56:02;

Ends 5 12:56:02;

Tstp 5 12:56:02;

Binding state free;

Hardware ethernet 00: 0c: 29: 5d: 41: 62;

}

This is actually a lease database file. it is used to save a series of lease statements, including the client host name, MAC address, assigned IP address, and IP address validity period. This database file is an editable text file in ASCII format. Whenever a lease changes, a new lease record is added at the end of the file.

After DHCP is installed, the lease database file dhcpd. leases is an empty file.

IV. client configuration

In fact, the client is not configured, as long as it is set to dhcp to obtain the ip address. After the configuration, we can restart the network to obtain the ip address from the configured dhcp server. Now we can check whether all the relevant parameters of the client comply with the configuration:

1. whether the client dns meets

[Root @ linux101 ~] # Cat/etc/resolv. conf

; Generated by/sbin/dhclient-script

Search linux102

Nameserver 192.168.0.102

The dns of the client is already the dns address we have set. Please check it out.

Search linux102 is the "option domain-name" name we set on the server, and nameserver is the address set on our server. So they are exactly consistent.

2. check whether the ip address meets

[Root @ linux101 ~] # Ifconfig

Eth0 Link encap: Ethernet HWaddr 00: 0C: 29: 5D: 41: 62

Inet addr: 192.168.0.101 Bcast: 192.168.0.255 Mask: 255.255.255.0

On our server, the ip address bound to the client is 192.168.0.101.

3. observe the route

[Root @ linux101 ~] # Route-n

Kernel IP routing table

Destination Gateway Genmask Flags Metric Ref Use Iface

192.168.0.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0

169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0

0.0.0.0 192.168.0.1 0.0.0.0 UG 0 0 0 eth0

 

We can see that it is consistent with 192.168.0.1 on my server.

4. view the port

The port used by the Dhcp client is 68. now let's check:

5. view the lease records recorded by the client

 

[Root @ linux101 dhclient] # cat/var/lib/dhclient/dhclient-eth0.leases

Lease {

Lease {

Interface "eth0"; // The listening port

Fixed-address 192.168.0.101; // IP address

Option subnet-mask subnet mask 255.255.0; // The obtained subnet mask.

Option time-offset-18000;

Option routers 192.168.0.1; // route address

Option dhcp-lease-time 21600; // lease time

Option dhcp-message-type 5;

Option domain-name-servers 192.168.0.102; // dns address

Option dhcp-server-identifier 192.168.0.102;

Option nis-domain "domain.org ";

Option domain-name "linux102"; // dns host name

Renew 2 11:22:19; // The estimated time of the next update (renew)

Rebind 2 13:47:58;

Expire 2 14:32:58;

}

# This file records the DHCP information required by the adapter!

# It is almost similar to the/etc/dhcpd. conf you set.

 

 

OK. The entire process has been fully explained. now let's make a simple summary:

Basic DHCP server construction process:

(1) edit the main configuration file dhcpd. conf and specify the IP scope (specify one or more IP address ranges ).

(2) create a lease database file.

(3) reload the configuration file or restart the dhcpd service to make the configuration take effect.

DHCP workflow:

(1) the client sends a broadcast request to apply for an IP address from the server.

(2) after receiving the request, the server checks the main configuration file dhcpd. conf and checks whether a fixed IP address is set for the client based on the client's MAC address.

(3) if a fixed IP address is set for the client, the IP address is sent to the client. If no fixed IP address is set, the IP address in the address pool is sent to the client.

(4) after the client receives a response from the server, the client responds to the server and tells the server that the assigned IP address has been used.

(5) the server saves the lease information to the database.

 

 

 

This article is from the "linux study notes" blog

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.