Set up a DHCP server under CentOS 6.4

Source: Internet
Author: User
Tags ldap


How DHCP works

DHCP is usually used as a communication protocol in the local area network. It transmits broadcast packets to all hosts in the entire physical network segment through a client. If a DHCP server exists in the local area network, in response to the client's IP parameter requirements. Therefore, the DHCP server and client must be in the same physical network segment.


650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0424353X1-0.jpg "title =" @ V) W $ ~ Glflmle'7a11606z(e0.jpg "alt =" 130706566.jpg"/>

The process of obtaining IP parameters from the client is simplified as follows:

1 client: Use broadcast packets to send packets to explore DHCP server packets

If the client network is set to use the DHCP protocol to obtain the IP address (automatically obtain the IP address in windows), when the client starts up or restarts the network, the client host sends a UDP packet for finding the DHCP server to all computers in the physical network segment. The destination IP address of the packet is 255.255.255.255. Therefore, generally, the host directly discards the packet after receiving the packet. However, if a DHCP server exists in the LAN, subsequent behavior will begin.


2. SERVER: provides lease related to the client network for selection.

After receiving the requirements from this client, the DHCP server performs the following operations on the client's hardware address (MAC) and its own set data:

Check whether the user has used an IP address in the login file of the server. If there is an IP address that is not currently in use, provide the IP address to the client;

If the configuration file provides an additional fixed IP address for the MAC, the fixed IP address is provided to the client;

If the two conditions are not met, the unused IP parameters are randomly used to the client and recorded.


In short, the server mainly responds to the MAC address of the client in the response packet information. At this time, the server retains the lease and starts to wait for the response from the client.


3. Client: decide the network parameter lease provided by the selected DHCP server and return the server:

Because there may not be only one DHCP server in the LAN, the client can accept only one set of network parameter leases. Therefore, the client must select whether to accept the lease of relevant network parameters provided by the server. After deciding to use the network parameter lease of this server, the client starts to use this set of network parameters to set its own network environment. In addition, the client sends a broadcast packet to all hosts in the physical network segment, notifying that the lease of the server has been accepted. If there are more than two DHCP servers, the unaccepted servers will reclaim the IP lease. The accepted DHCP server will continue to perform the following actions.


4. SERVER: record the lease behavior and return the response packets confirmed by the client:

When the server receives the confirmation from the client, the server returns the confirmation response packet, notifies the client of the term of the network parameter lease, and starts the lease timing. The lease will expire and be canceled. You can think like this:

Offline client: whether it is to disable the network interface (ifdown), restart (reboot), shutdown (shutdown), and so on, are considered offline. At this time, the Server will take back the IP address, and put it in the Server's standby zone, waiting for future use.

Client lease expiration: the IP address previously provided by the DHCP server has a validity period. When the client uses this IP address to reach the deadline specified, and does not submit a DHCP Application again, you need to remove the IP address. This will cause a disconnection. However, you can request an IP address to be assigned to the DHCP server again.


Benefits of the lease term:

This prevents IP addresses from being occupied by some users, but this user is in the idle status.


Since there is a lease time, does it mean that the IP address I obtained using DHCP needs to manually obtain a new IP address at a certain time point. Currently, most DHCP client programs have to apply for IP addresses again based on the lease time. That is to say, your DHCP client has re-applied to update the lease time before the lease expires. Unless the DHCP server goes down, the obtained IP address can be used continuously.


Multiple DHCP servers in the same physical network segment

When there are more than two DHCP servers, which server will provide DHCP requests from my client computer. No one knows, because on the network, it is often the first come first served, and the DHCP response is the same. When Server1 first responds, you use the network parameter content provided by Server1. If Server2 is used, you use the Server2 parameter to set your client PC.


When to set up a DHCP server

1. scenarios with a considerable number of mobile devices

2. The number of computers in the region is quite large.


It is not recommended that you use a DHCP host.

1. Computers in the network, many machines are actually used as hosts, with few user needs

2. In more extreme cases, only 3-4 computers are supported. Setting up DHCP does not make much sense.

3. When most network adapters in the network you manage belong to old models and do not support DHCP

4 many users have high information knowledge and do not need to set up DHCP.



Next, let's take a look at the dhcp configuration file.


DHCP server settings

Install server software


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


Check which important files are provided by the dhcp Software

[root@centos ~]# rpm -ql dhcp/etc/dhcp/etc/dhcp/dhcpd.conf/etc/dhcp/dhcpd6.conf/etc/openldap/schema/dhcp.schema/etc/portreserve/dhcpd/etc/rc.d/init.d/dhcpd/etc/rc.d/init.d/dhcpd6/etc/rc.d/init.d/dhcrelay/etc/sysconfig/dhcpd/etc/sysconfig/dhcpd6/etc/sysconfig/dhcrelay/usr/bin/omshell/usr/sbin/dhcpd/usr/sbin/dhcrelay/usr/share/doc/dhcp-4.1.1/usr/share/doc/dhcp-4.1.1/3.0b1-lease-convert/usr/share/doc/dhcp-4.1.1/IANA-arp-parameters/usr/share/doc/dhcp-4.1.1/README.ldap/usr/share/doc/dhcp-4.1.1/api+protocol/usr/share/doc/dhcp-4.1.1/dhclient-tz-exithook.sh/usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample/usr/share/doc/dhcp-4.1.1/dhcpd6.conf.sample/usr/share/doc/dhcp-4.1.1/draft-ietf-dhc-ldap-schema-01.txt/usr/share/doc/dhcp-4.1.1/ms2isc/usr/share/doc/dhcp-4.1.1/ms2isc/Registry.perlmodule/usr/share/doc/dhcp-4.1.1/ms2isc/ms2isc.pl/usr/share/doc/dhcp-4.1.1/ms2isc/readme.txt/usr/share/doc/dhcp-4.1.1/sethostname.sh/usr/share/doc/dhcp-4.1.1/solaris.init/usr/share/man/man1/omshell.1.gz/usr/share/man/man5/dhcpd.conf.5.gz/usr/share/man/man5/dhcpd.leases.5.gz/usr/share/man/man8/dhcpd.8.gz/usr/share/man/man8/dhcrelay.8.gz/var/lib/dhcpd/var/lib/dhcpd/dhcpd.leases/var/lib/dhcpd/dhcpd6.leases


/Etc/dhcp. conf

This is the main configuration file of the dhcp server. This file may not exist in some Linux distributions. If you are sure you have installed the dhcp software but cannot find this file, you can create it manually.

Tips: in fact, the dhcp software will include an example file when releasing it. You can use "rpm-ql dhcp" to query dhcpd. conf. sample the sample file and copy it to/etc/dhcp/dhcpd. after conf, modify it manually. This makes setting easier!


/Usr/sbin/dhcp

Start the entire dhcp daemon execution file. Use man dhcpd to view the most detailed execution method.


/Var/lib/dhcp/dhcpd. leases

The START and expiration dates of the lease establishment between the DHCP server and the client are recorded in this file.


Main configuration file/etc/dhcp/dhcpd. conf syntax

In fact, DHCP settings are very simple. You only need to set dhcpd. conf to start. However, you must note the following specifications when editing this file:

1 # annotation symbol

2 except for the right brace ")", each other row must end with a semicolon;

3. Set the project syntax to <parameter code> <set content> for example, default-lease-time 250000;

4. Certain configuration items must be set using option. The basic method is option. <parameter code> <set content>

For example, option domain-name "yao.domain.com ";


In fact, dhcpd. the configuration in conf is mainly divided into two major projects, one is the overall setting of server operation (Global), and the other is the IP setting mode (dynamic or static ), the following items are set for each project:


Global)

If your dhcpd only manages the network of one region, many network parameters except IP can be placed in the overall set area, this includes the lease term, the IP address of the DNS host, the IP address of the router, and the type of Dynamic DNS update. When some settings are not specified in the fixed IP address and dynamic IP address, the overall setting value prevails. These parameters are named as follows:


Default-lease-time (unit: seconds by default ):

The user's computer requires a lease time of a specific length. However, if the user does not require a lease time in particular, this is the default lease time.


Max-lease-time:

This setting value is the maximum lease time that the user can require. That is to say, if the lease time required by the user exceeds the set value, the value prevails;


Option domain-name "domain name ":

If you set a search google.com in/etc/resolv. conf, this means that when you look for the host name, the DNS system will automatically add the domain name to you.


Option domain-name-servers IP1, IP2:

This setting parameter can be changed to the/etc/resolv. conf file of the client.


Ddns-update-style type:

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 use ddns to update the Host Name and IP address. This is not discussed here, so you can set it to none.


Ignore client-updates:

The client can update DNS information through the dhcpd server. I will not talk about this here, but set it to ignore.


Option routers router address:

Set the IP address of the router


IP Address Setting Mode (dynamic or static)

Because dhcpd provides IP Parameters for LAN, you must specify a zone network before setting the IP address. The following parameters are used to specify the network type:

Subnet NETWORK_IP netmask NETMASK_IP {...}


Range IP1 IP2:

In this area network, a continuous IP group is assigned to the dynamic IP address setting. For example, the 100 IP addresses from 192.168.100.101 to 192.168.100.200 are opened for Dynamic Allocation, that is, range 192.168.100.101 192.168.100.200;


Host name {...};

This host is the set value of the fixed IP address corresponding to the fixed MAC address. You can give the host name a try again. However, MAC and fixed IP addresses must be specified in braces. How can we set these two values?

1 hardware ethernet hardware address:

Use the fixed hardware address on the network card to set the IP address. This setting is only valid for this hardware address.

2 fixed-address IP address:

Assign a fixed IP Address



How to Set up a lan dhcp server in the lab room of a school


The Configuration File Settings are as follows:

[Root @ centos ~] # Vim/etc/dhcp/dhcpd. conf # fixed-address 192.168.246.200; # DHCP Server Configuration file. # see/usr/share/doc/dhcp */dhcpd. conf. sample # see 'man 5 dhcpd. conf' # ddns-update-style none; # Do not update DDNS settings ignore client-updates; # ignore the client's DNS update function default-lease-time 259200; # default lease is 3 days max-lease-time 518400; # maximum lease is 6 days option routers 192.168.246.254; # Default Gateway option domain-name "yao. gateway "; # Give a domain name option domain-name-servers 218.85.157.99, 218.85.152.99; # DNS settings # dynamically allocated IPsubnet 192.168.246.0 netmask 255.255.255.0 {range 192.168.246.1 192.168.246.250; # allocated IP Range # static IP host yao {hardware ethernet 00: 26: 9E: E7: 81: 9C; # client Nic MAC fixed-address 192.168.246.200; # IP }}


Start the service and observe the port

[root@centos ~]# /etc/init.d/dhcpd startStarting dhcpd: [  OK  ][root@centos ~]# chkconfig dhcpd on[root@centos ~]# netstat -plunt | grep dhcpudp        0      0 0.0.0.0:67                  0.0.0.0:*                             4156/dhcpd


/Var/log/messages can view the output information of the login file

[root@centos ~]# tail -30 /var/log/messagesOct  3 00:03:50 centos kernel: usb 2-2: New USB device found, idVendor=17ef, idProduct=600eOct  3 00:03:50 centos kernel: usb 2-2: New USB device strings: Mfr=0, Product=2, SerialNumber=0Oct  3 00:03:50 centos kernel: usb 2-2: Product: Lenovo Optical MouseOct  3 00:03:50 centos kernel: usb 2-2: configuration #1 chosen from 1 choiceOct  3 00:03:50 centos kernel: input: Lenovo Optical Mouse as /devices/pci0000:00/0000:00:1d.0/usb2/2-2/2-2:1.0/input/input410Oct  3 00:03:50 centos kernel: generic-usb 0003:17EF:600E.0192: input,hidraw0: USB HID v1.11 Mouse [Lenovo Optical Mouse] on usb-0000:00:1d.0-2/input0Oct  3 00:04:18 centos dhcpd: Internet Systems Consortium DHCP Server 4.1.1-P1Oct  3 00:04:18 centos dhcpd: Copyright 2004-2010 Internet Systems Consortium.Oct  3 00:04:18 centos dhcpd: All rights reserved.Oct  3 00:04:18 centos dhcpd: For info, please visit https://www.isc.org/software/dhcp/Oct  3 00:04:18 centos dhcpd: WARNING: Host declarations are global.  They are not limited to the scope you declared them in.Oct  3 00:04:18 centos dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config fileOct  3 00:04:18 centos dhcpd: Wrote 0 deleted host decls to leases file.Oct  3 00:04:18 centos dhcpd: Wrote 0 new dynamic host decls to leases file.Oct  3 00:04:18 centos dhcpd: Wrote 0 leases to leases file.Oct  3 00:04:18 centos dhcpd: Listening on LPF/eth0/48:02:2a:f0:8f:07/192.168.246.0/24Oct  3 00:04:18 centos dhcpd: Sending on   LPF/eth0/48:02:2a:f0:8f:07/192.168.246.0/24Oct  3 00:04:18 centos dhcpd: Sending on   Socket/fallback/fallback-netOct  3 00:04:25 centos dhcpd: DHCPDISCOVER from 30:e4:db:31:fc:40 via eth0Oct  3 00:04:26 centos dhcpd: DHCPOFFER on 192.168.246.1 to 30:e4:db:31:fc:40 ( ) via eth0Oct  3 00:04:26 centos dhcpd: DHCPREQUEST for 192.168.246.1 from 30:e4:db:31:fc:40 ( ) via eth0Oct  3 00:04:26 centos dhcpd: DHCPACK on 192.168.246.1 to 30:e4:db:31:fc:40 ( ) via eth0


IP address of the internal host

After using DHCP, I don't know which PC is connected to my host. How do I enter/etc/hosts and add all possible computer IP addresses to this file.

[root@centos ~]# vim /etc/hosts127.0.0.1   localhost localhost.localdomain localhost4localhost4.localdomain4::1         localhost localhost.localdomain localhost6localhost6.localdomain6192.168.246.200 yao

Here we only add one, so we will not be lazy.


A better solution is to set up an internal DNS server, so that other internal Linux servers do not have to change/etc/hosts to get the corresponding IP address of each host and host name.



DHCP Client settings

The client is Linux

[Root @ clientlinux ~] # Vim/etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE = eth0NM_CONTROLLED = noONBOOT = yesBOOTPROTO = dhcp <= It's him! Specify this one! [Root @ clientlinux ~] #/Etc/init. d/network restart


If the correct DHCP host is found in the execution result, several files may be changed:

/Etc/resolv. conf


#3. Check the commands on the client! [Root @ clientlinux ~] # Netstat-tlunp | grep dhcProto Recv-Q Send-Q Local Address Foreign Address State PID/Program name udp 0 0 0.0.0.0: 68 0.0.0.0: * 1694/dhclient # No error! There is indeed a small program monitoring the online status of DHCP success!


/Var/lib/dhclient * client lease information



The client is Windows

The IP address and DNS are both automatically obtained. It should be difficult!

650) this. width = 650; "src =" http://www.bkjia.com/uploads/allimg/131228/0424352c3-1.jpg "title =" h0g1_1ok_y1_j71_0eu1_wr78ss.jpg "alt =" 165715998.jpg"/>

This is done!



Advanced observation and use of DHCP server


Check the lease File

[root@centos ~]# cat /var/lib/dhcpd/dhcpd.leases# The format of this file is documented in the dhcpd.leases(5) manual page.# This lease file was written by isc-dhcp-4.1.1-P1lease 192.168.246.41 {  starts 3 2013/10/02 16:24:23;  ends 6 2013/10/05 16:24:23;  tstp 6 2013/10/05 16:24:23;  cltt 3 2013/10/02 16:24:23;  binding state active;  next binding state free;  hardware ethernet 60:eb:69:bc:14:12;  uid "\001`\353i\274\024\022";  client-hostname "sjx041";}server-duid "\000\001\000\001\031\337\000\202H\002*\217\007";

The client will take the initiative to record the lease information, the server side is recorded in this place, from this file we will know how many clients have applied for an IP address from the DHCP server.



Scripts with fixed IP addresses for a large number of PCs

#!/bin/bashread -p "Every client obtains the static IP (y/n)" ynread -p "How many PC in the network?" numif [ "$yn" = "y" ];then    for site in $(seq 1 ${num})  do       siteip="192.168.246.${site}"       allip="$allip $siteip"       ping -c 1 -w 1 $siteip > /dev/null 2>&1       if [ "$?" == "0" ];then           okip="$okip $siteip"       else            errorip="$errorip $siteip"            echo "$siteip is down"       fi done[ -f /etc/dhcp/dhcpd.conf ] && rm -f /etc/dhcp/dhcpd.conf      for site in $allip   do      pcname=pc$(echo $site | cut -d '.' -f 4)      mac=$(arp -n | grep "$site" | awk '{print $3}')      echo "  host $pcname {"      echo "         hardware ethernet ${mac};"      echo "         fixed-address     ${site};"      echo "  }"      echo "  host  $pcname {"  >> /etc/dhcp/dhcpd.conf      echo "         hardware ethernet ${mac};" >>  /etc/dhcp/dhcpd.conf      echo "         fixed-address     ${site};" >>  /etc/dhcp/dhcpd.conf      echo "  }"  >>  /etc/dhcp/dhcpd.conf  donefi    echo "Finished."

If the computer you manage is linux, use ifconfig eth0 and your IP address to set the corresponding IP address.

If the computer you manage is Windows, use netsh interface ip set address xxx to set the IP address.



Use ether-wake to enable automatic remote boot

If you want a host to start over the network, you must perform the following operations on the client computer:

1. First, you must set the "Network wake-up" function in the BIOS. Otherwise, it will be useless!

2. You must connect the host to the network and the power supply is also connected.

3. Copy the MAC address of the host, shut down the host, and wait for the Network to wake up.


The ether-wake command is in the net-tools Package.


Assume that the MAC address of the client host is 11: 22: 33: 44: 55: 66 and the host is connected to eth1, so you want to wake up the host, just do this:

[Root @ www ~] # Ether-wake-I eth1 11: 22: 33: 44: 55: 66 # For more functions, refer to [root @ www ~]. # Ether-wake-u


DHCP response speed and network switch settings

The switch can detect and resist packet exceptions.

Switch. Most of them are set to subprojects such as "L2 Features" --> "Spanning Tree" --> "STP Port Settings.


The main purpose of STP is to defend against broadcast storms. If a broadcast storm is detected, the switch port is disabled. After this function is started, it will slowly enter the operational status, so it will happen slowly. A better switch supports RSTP (Rapid spanning tree protocol), which is faster.


This article from the "technology lies in persistence" blog, please be sure to keep this source http://minilinux.blog.51cto.com/4499123/1304134


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.