DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP地址,给用户或者内部网络管理员作为对所有计算机作中央管理的手段,在RFC 2131中有详细的描述。DHCP有3个端口,其中UDP67和UDP68为正常的DHCP服务端口,分别作为DHCP Server和DHCP Client的服务端口;546号端口用于DHCPv6 Client,而不用于DHCPv4,是为DHCP failover服务,这是需要特别开启的服务,DHCP failover是用来做“双机热备”的。这里借用百度百科DHCP的信息,更多理论参照这个。
实验环境
系统:CentOS 6.5 IP:192.168.88.254
客户端:WinXP
安装软件
Installed:
dhcp.x86_64 12:4.1.1-51.P1.el6.centos //主包
Dependency Updated: //通过yum 安装会解析到依赖包,如果rpm安装记得也安装上去
dhclient.x86_64 12:4.1.1-51.P1.el6.centos
dhcp-common.x86_64 12:4.1.1-51.P1.el6.centos
默认安装完成后配置文件是空的,如
[root@server ~]# cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp*/dhcpd.conf.sample # see 'man 5 dhcpd.conf' # [root@server ~]#
通过文件中的说明,告诉我们,在/usr/share/doc/dhcp*/dhcpd.conf.sample 这里有模板文件可能参考。我们直接复制过来
[root@server ~]# ll /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample -rw-r--r--. 1 root root 3262 8月 13 2002 /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample [root@server ~]#
可以看的,我的DHCP版本是4.1.1,文件的所有者和所属组都是root。
[root@server ~]# ll /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample -rw-r--r--. 1 root root 3262 8月 13 2002 /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample [root@server ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y [root@server ~]#
dhcpd.conf.sample文件的所有内容如下
[root@server ~]# 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; # 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 10.5.5.0 netmask 255.255.255.224 { range 10.5.5.26 10.5.5.30; option domain-name-servers ns1.internal.example.org; option domain-name "internal.example.org"; option routers 10.5.5.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 passacaglia { hardware ethernet 0:0:c0:5d:bd:95; 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 fantasia { hardware ethernet 08:00:07:26:c0:a5; fixed-address fantasia.fugue.com; } # 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@server ~]# vim /etc/dhcp/dhcpd.conf option domain-name "rhce.ce"; option domain-name-servers server.rhce.ce; default-lease-time 600; max-lease-time 7200; ddns-update-style none; authoritative; log-facility local7; //上面的配置是全局配置,如果在subnet中没有指明则按这个配置。 subnet 192.168.88.0 netmask 255.255.255.0 { //声明一个网段 range 192.168.88.1 192.168.88.250; //声明一个地址段 option domain-name-servers rhce.ce; //声明DNS服务器 option domain-name "server.rhce.ce"; //声明DNS服务器名称 option routers 192.168.88.254; //声明网关 option broadcast-address 192.168.88.255; //广播地址 default-lease-time 600; //默认有效时间600秒 max-lease-time 7200; 最大时间7200秒 } host passacaglia { hardware ethernet 0:0:c0:5d:bd:95; filename "vmunix.passacaglia"; server-name "toccata.fugue.com"; } host WindowsXP { //为客户端做地址保留 hardware ethernet 08:00:07:26:c0:a5; fixed-address 192.168.88.10; }
按照上面的配置,我在service dhcpd restart时出错了,查看message发现了一行错误
Can't chown new lease file: Operation not permitted
网上看了好多文章都说是没有权限造成的,在操作过程中发现其实是selinux导致的。并不需要修改/etc/init.d/dhcpd ,我也不觉得修改很我,那样话DHCP就有了root的权限了。
默认情况下是不会出现这样的问题的,我中间也换了一台虚拟机测试,完全没有问题,selinux也是开着的,所以搞不明白,后来我通过cat /var/log/audit/audit.log |grep AVC |grep dhcp 查看到有很多相关日志,后来我安装了selinux-policy 、setroublshoot 等工具准备查看,结果就运行了一个audit2allow -i /var/log/message DHCP就自己好了,我也在找原因。。。
可以看到我的DHCP已经拿到IP地址了。
可以看到拿到的IP地址,以及解析出来的域名,关于DNS的配置可以参考我的另一个文章
http://changelee.blog.51cto.com/4581245/1837754
====================================================
DHCP的一些简单配置到这里完成了,剩下的就是一些日志及DHCP状态的查看
主要的日志还是/var/log/message
我们可以在启动DHCP之前新开一个窗口,使用
tailf -n 30 /var/log/messages
去查看相关日志信息,还可以看到客户端获取地址的日志
[root@server etc]# 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-P1 server-duid "\000\001\000\001\037C1\307\000\014)DT\035"; lease 192.168.88.10 { starts 0 2016/08/14 06:42:18; ends 0 2016/08/14 06:52:18; cltt 0 2016/08/14 06:42:18; binding state active; next binding state free; hardware ethernet 00:0c:29:c5:14:7a; uid "\001\000\014)\305\024z"; client-hostname "xp-a4354dd3e21d"; } lease 192.168.88.10 { starts 0 2016/08/14 06:42:18; ends 0 2016/08/14 06:42:30; tstp 0 2016/08/14 06:42:30; cltt 0 2016/08/14 06:42:18; binding state free; hardware ethernet 00:0c:29:c5:14:7a; uid "\001\000\014)\305\024z"; } lease 192.168.88.10 { starts 0 2016/08/14 06:42:33; ends 0 2016/08/14 06:52:33; cltt 0 2016/08/14 06:42:33; binding state active; next binding state free; hardware ethernet 00:0c:29:c5:14:7a; uid "\001\000\014)\305\024z"; client-hostname "xp-a4354dd3e21d"; } [root@server etc]#
关于更多的DHCP配置可能通过
man dhcpd.conf
去查看更多的配置
另外加一台命令,可以远程开机,前提是被开机的电脑支持
ether-wake -i eth0 -p 00:11:22:33:44:55
转载于:https://blog.51cto.com/changelee/1837778