DHCP (Dynamic Host Configuration Protocol) 服务器最主要的工作是自动的分配所有的网络参数给网域内的任何一部计算机,让客户端的计算机可以在开机的时候就立即自动的设定好网络的参数值,这些参数值可以包括了 IP、netmask、network、gateway 与 DNS 的地址等等。
客户端取得 IP 参数的程序如下:
1,客户端:利用广播封包发送搜索 DHCP 服务器的封包。
2,服务器端:提供客户端网络相关的租约以供选择。
3,客户端:决定选择的 DHCP 服务器提供的网络参数租约并回报服务器。
4,服务器端:记录该次租约行为并回报客户端已确认的响应封包信息。
Client–> DHCPDISCOVER
DHCPOFFER <-- Server
Client--> DCHPREQUEST
DCHPACK <-- Server
Client--> DHCPREQUEST
DHCPACK <-- Server
<code> [root@localhost ~]# yum install dhcp #安装dhcp [root@localhost ~]# rpm -ql dhcp #查看其所安装的文件 /etc/dhcp /etc/dhcp/dhcpd.conf #主配置问价 /etc/dhcp/dhcpd6.conf #ipv6配置文件 /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/rc.d/init.d/dhcrelay6 /etc/sysconfig/dhcpd /etc/sysconfig/dhcpd6 /etc/sysconfig/dhcrelay /etc/sysconfig/dhcrelay6 /usr/bin/omshell /usr/sbin/dhcpd #主程序 /usr/sbin/dhcrelay 。。。。。 /usr/share/man/man8/dhcrelay.8.gz /var/lib/dhcpd /var/lib/dhcpd/dhcpd.leases #租约信息 /var/lib/dhcpd/dhcpd6.leases #ipv6的 配置文件中 ‘ # ’为批注符号,除了右括号 ")" 后面之外,其他的每一行设定最后都要以 ‘ ; ’ 做为结尾! 设定项目语法主要为: <参数代号> <设定内容> [root@localhost ~]# vim /etc/dhcp/dhcpd.conf # dhcpd.conf # # Sample configuration file for ISC dhcpd # # option definitions common to all supported networks… option domain-name "example.org"; #领域名 option domain-name-servers ns1.example.org, ns2.example.org; default-lease-time 600; #默认租约时间 max-lease-time 7200; #最大租约时间 # Use this to enble / disable dynamic dns updates globally. #ddns-update-style none; # 不要更新 DDNS 的设定 # If this DHCP server is the official DHCP server for the local # network, the authoritative directive should be uncommented. #authoritative; # Use this to send dhcp log messages to a different log file (you also # have to hack syslog.conf to complete the redirection). log-facility local7; # No service will be given on this subnet, but declaring it helps the # DHCP server to understand the network topology. subnet 10.152.187.0 netmask 255.255.255.0 { } # This is a very basic subnet declaration. subnet 10.254.239.0 netmask 255.255.255.224 { range 10.254.239.10 10.254.239.20; option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org; } # This declaration allows BOOTP clients to get dynamic addresses, # which we don't really recommend. subnet 10.254.239.32 netmask 255.255.255.224 { range dynamic-bootp 10.254.239.40 10.254.239.60; option broadcast-address 10.254.239.31; option routers rtr-239-32-1.example.org; } # A slightly different configuration for an internal subnet. subnet 192.168.0.0 netmask 255.255.255.0 { 自己配置的地方,要与自己的一直,建议使用ifconfig查看下再修改 #子网,地址池列表 range 192.168.0.0 192.168.0.30; #地址池范围,分配时从大到小分配 option subnet-mask 255.255.255.0; #掩码 option domain-name-servers 192.168.0.1; #dns服务器地址 option domain-name "feiyu.com"; option routers 192.168.0.1; #这就是预设路由 # option broadcast-address 10.5.5.31; default-lease-time 600; #默认租约长度,秒 max-lease-time 7200; #最大租约长度 } # Hosts which require special configuration options can be listed in # host statements. If no address is specified, the address will be # allocated dynamically (if possible), but the host-specific information # will still come from the host declaration. host easd { #host保留地址,后面的字符串没意义,自己定义的标示符 hardware ethernet 00:0C:29:f8:95:31; #mac地址 filename "vmunix.passacaglia"; server-name "toccata.fugue.com"; } # Fixed IP addresses can also be specified for hosts. These addresses # should not also be listed as being available for dynamic assignment. # Hosts for which fixed IP addresses have been specified can boot using # BOOTP or DHCP. Hosts for which no fixed address is specified can only # be booted with DHCP, unless there is an address range on the subnet # to which a BOOTP client is connected which has the dynamic-bootp flag # set. host eth2{ #自定义部分,给另一个虚拟机分配固定地址,mac地址要与其相同 ##host保留地址,后面的字符串没意义,自己定义的标示符 hardware ethernet 00:0C:29:11:2D:B5; #mac地址 fixed-address 192.168.1.10; #分配的固定地址 } host ftp{ hardware ethernet 00:99:29:A8:95:44; fixed-address 192.168.1.20; } # You can declare a class of clients and then do address allocation # based on that. The example below shows a case where all clients # in a certain class get addresses on the 10.17.224/24 subnet, and all # other clients get addresses on the 10.0.29/24 subnet. class "foo" { match if substring (option vendor-class-identifier, 0, 4) = "SUNW"; } shared-network 224-29 { subnet 10.17.224.0 netmask 255.255.255.0 { option routers rtr-224.example.org; } subnet 10.0.29.0 netmask 255.255.255.0 { option routers rtr-29.example.org; } pool { allow members of "foo"; range 10.17.224.10 10.17.224.250; } pool { deny members of "foo"; range 10.0.29.10 10.0.29.230; } } [root@localhost ~]# service dhcpd start #启动服务 Starting dhcpd: [ OK ] [root@localhost ~]# chkconfig dhcpd on [root@localhost ~]# netstat -tlnup | grep dhc #查看其所监听的端口UDP:67/udp,68/udp udp 0 0 0.0.0.0:67 0.0.0.0:* 25584/dhcpd udp 0 0 0.0.0.0:68 0.0.0.0:* 24097/dhclient udp 0 0 0.0.0.0:68 0.0.0.0:* 10322/dhclient udp 0 0 0.0.0.0:68 0.0.0.0:* 1793/dhclient [root@struggle ~]# service network restart #启动另一个虚拟机,重启网络查看是否分配了固定的地址,客户端必须设定为dhcp [root@localhost ~]# dhclient #使用此命令也可以获取静态地址,但只能使用使用一次 [root@localhost ~]# dhclient -d #第二次使用必须加上-d选项 可以在日志文件/var/log/messages查看其有关信息。 </code>