Chapter 4 DHCP service and Chapter 4 dhcp Service

Source: Internet
Author: User

Chapter 4 DHCP service and Chapter 4 dhcp Service

 

The predecessor of DHCP is BOOTP. In the NIC configuration of Linux, BOOTP is displayed. DHCP introduces a concept not available in bootp: lease. The IP address allocated by bootp is permanent, and the IP address allocated by dhcp is valid for a certain period of time.

[root@xuexi vsftpd]# grep -i bootproto /etc/sysconfig/network-scripts/ifcfg-eth0BOOTPROTO=dhcp

DHCP can automatically allocate IP addresses, subnet masks, gateways, and DNS.

The DHCP client uses port 68, the server uses port 67, and the UDP application layer protocol.

DHCP generally does not assign IP addresses to servers, because they need to use a fixed IP address, so DHCP generally only assigns IP addresses to hosts in the office environment.

DHCP servers and clients must be in the same LAN. When assigning IP addresses to clients, DHCP servers and clients must be broadcast multiple times. However, DHCP can also assign IP addresses to hosts in other CIDR blocks, as long as the routers connected to the two CIDR blocks can forward DHCP configuration requests, but this requires the router to configure the relay function.

DHCP client request process (4-step request process)

1) Search phase: the client sends packets in broadcast mode and searches for DHCP servers. At this time, all machines in the network segment receive messages, and only the DHCP server returns messages.

2) Provision phase: Many DHCP servers return packet information and find an IP address from the address pool to provide it to the client. Because the client does not have an IP address at this time, the returned information is also returned in broadcast mode.

3) select phase: select a DHCP server and use the IP address it provides. Then, send a broadcast packet to tell the DHCP server that the DHCP server and IP address have been selected. After that, DHCP servers that are not selected can allocate IP addresses to other hosts.

The client selects the first received IP address. The speed at which the IP is first sent to the client is uncontrollable. However, if the authoritative option is enabled in the configuration file, it indicates that the server is an authoritative server, and other DHCP servers will fail. If the authoritative option is configured on multiple servers, the competition mechanism will continue; configuring a fixed IP address for the client through the MAC address also takes precedence over normal dynamic DHCP allocation. In addition, the Windows DHCP server responds faster to Windows clients than Linux.

4) confirmation phase: the DHCP server receives a response, sends an IP packet to the client, confirms the lease, and specifies the lease duration.

If the DHCP server needs to provide services across network segments, it is also a four-step request, but each step has an additional unicast communication between the router and the DHCP server.

1) the client sends packets in broadcast mode and searches for DHCP servers. All machines, including routers, receive packets, and the routers are configured with relay. After a message is searched, the routers are unicast to the DHCP server. 2) the DHCP server returns the information unicast to the router, and the routers then broadcast it to the client; 3) The client selects the IP address provided by the DHCP server and broadcasts the information to the DHCP server. The router is unicast to the DHCP server. 4) when the DHCP server receives the information, the confirmation information is unicast to the router, the router is unicast to the client.

Therefore, DHCP's 4-step request:

Client --> DHCPDISCOVER # broadcast: the Client discovers the DHCP Server DHCPOFFER <-- Server # broadcast: the Server provides an IP address to the Client.
Client --> DCHPREQUEST # broadcast: Client requests are sent using the provided IP address DCHPACK <-- Server # unicast: Server confirms, creates a lease, and other information.

Renewal Process:

Client --> DHCPREQUEST # unicast: continue to request to use the provided IP address DHCPACK <-- Server # unicast: Confirm renewal

DHCP serverWhen the service is not provided across network segments,Its own IP address must be in the same network as all IP addresses in the address pool.

DHCP serverWhen providing services across CIDR blocks,Its own IP address must be in the same network as some IP addresses in the address pool, and the other must be provided to other network segments. If your IP address is not in your network, but only the IP address of another network segment is provided, it is better to set the DHCP server in the network that requires the DHCP service.

When a computer is moved from one subnet to another, the DHCP server is different. Because the old lease still exists, the lease will be renewed first. The new DHCP server will definitely reject the lease renewal request, at this time, the four-step request will start again.

Some machines want to always use a fixed IP address, that is, a static IP address. In addition to manual configuration, the DHCP server can also implement this function. The DHCP server can assign a fixed IP address (Reserved IP address) to the Machine Based on the MAC address. Even if the system is restarted or reinstalled, the IP address assigned based on the MAC address is not changed.

If a new DHCP server is created in the network segment of a normally connected DHCP server due to lab exercises, but the DHCP server cannot access the Internet, what will happen? The client that uses DHCP to allocate an address will have at least a lease renewal request. If no lease renewal is successful or a new computer joins the network, perform the four-step request, A request may be sent to the DHCP server that cannot be connected to the Internet. In particular, the Windows DHCP server responds faster to Windows clients than to Linux.

Install and configure the DHCP service
[Root @ xuexi ~] # Yum-y install dhcp [root @ xuexi ~] # Rpm-ql dhcp/etc/dhcp/dhcpd. conf # DHCP configuration file/etc/sysconfig/dhcpd/usr/sbin/dhcpd # DHCP Service Program/usr/sbin/dhcrelay # relay command program, it is used to provide DHCP Service/var/lib/dhcpd across network segments. leases # store lease information (such as IP) and lease information (such as lease term)/usr/share/doc/dhcp-4.1.1/dhcpd. conf. sample # sample file of the configuration file

Copy dhcpd. conf. sample to/etc /.

[root@xuexi ~]# cp /usr/share/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcpd.conf 

Some configuration items in dhcpd. conf are as follows.

# End ddns-update-style none with a semicolon in each line; # It is almost disabled for Dynamic dns. That is, ignore it. Ignore client-updates; # Related to the above, regardless of its authoritative # declared as an authoritative server
Next-server marvin.redhat.com; # The file server that provides the boot program specified in the PXE Environment
# An address pool must be configured in the DHCP configuration file, which is in the same network segment as the IP address of the DHCP server. subnet 10.5.5.0 netmask has fixed bandwidth limit {range 10.5.26 10.5.5.30; # address pool option domain-name-servers ns1.internal.example.org; # specify the DNS server address for the client, which can be multiple, up to three option domain-name "internal.example.org"; # specify the DNS name for the client, if it is defined, it will overwrite the client/etc/resolv. configuration option routers 10.5.5.1; # The default route in conf is actually the gateway option broadcast-address 10.5.5.31; # broadcast address default-lease-time 600; # default lease duration max-lease-time 7200; # maximum lease duration}
# The following is the reserved IP address bound to the MAC address. The reserved IP address cannot be the host fantasia {# fixed IP Address Configuration in the address pool. The host is followed by an identifier and does not make sense for hardware ethernet 08: 00: 07: 26: c0: a5; fixed-address 192.168.100.3; # assign a fixed IP address based on the MAC address}

If dhcp is not allowed to modify the content in/etc/resolv. conf, add the option PEERDNS = no in the NIC configuration file/etc/sysconfig/network-scripts/ifcfg-ethX.

 

How can I obtain dynamically allocated addresses on the client?

Method 1: service network restart

However, it is very troublesome to restart the network every time. You can use the Client Command dhclient.

Method 2: directly execute the dhclient command

This method will display the step information to be displayed in the four requests and the Final allocated address, so it is a good tool for understanding dhcp work.

However, this method can only be used once. The second command will prompt that the process is being executed because dhclient is a process. You can kill the process and then execute dhclient, or use the dhclient-d option.

Method 3: dhclient-d

How to obtain IP addresses again

 

1.1 how to obtain an ip address again

The same ip address is obtained by default every time you restart the NIC. It is troublesome to change the ip address. There are ". leases" files in the/var/lib/dhclient/directory, clear them or delete the corresponding Nic parts of these files, and then restart the network to get a new dynamic IP address.

[root@xuexi ~]# cat /var/lib/dhclient/dhclient-eth0.leases lease {  interface "eth0";  fixed-address 192.168.100.16;  option subnet-mask 255.255.255.0;  option routers 192.168.100.2;  option dhcp-lease-time 1800;  option dhcp-message-type 5;  option domain-name-servers 192.168.100.2;  option dhcp-server-identifier 192.168.100.254;  option broadcast-address 192.168.100.255;  option domain-name "localdomain";  renew 3 2017/02/15 12:28:27;  rebind 3 2017/02/15 12:42:39;  expire 3 2017/02/15 12:46:24;}

Alternatively, add "DHCPRELEASE = yes" to/etc/sysconfig/network-scripts/ifcfg-eth0 ".

When ifdown eth0 is run, a dhcprelase message is sent. Check that the/etc/sysconfig/network-scripts/ifdown-eth script actually calls the dhclient command, you can use the following command.

/sbin/dhclient -r eth0

Back to series article outline: http://www.cnblogs.com/f-ck-need-u/p/7048359.html

Reprinted please indicate the source: Success!

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.