DHCP(Dynamic Host Configuration Protocol,动态主机配置协议)通常被应用在大型的局域网络环境中,主要作用是集中的管理、分配IP地址,使网络环境中的主机动态的获得IP地址、 Gateway地址、DNS服务器地址等信息,并能够提升地址的使用率。
C/S 模式
Server:DHCP Server(运行dhcp服务)UDP服务:67
Client:DHCP Client(运行dhcp程序) UDP服务:68
udp:适合发送较小的数据报文,且对时效性要求较高
当客户端获取不到有效地址时候 dhcp会发一个 169.254.X.X 作为本地地址
安装配置:
[root@marvin ~]# yum install dhcp
配置文件:
[root@marvin dhcp]# pwd /etc/dhcp [root@marvin dhcp]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample dhcpd.conf
配置文件说明:
[root@marvin dhcp]# vim dhcpd.conf #log-facility local7; #日志/var/log/boot.log #全局地址分配属性:option开头 #option domain-name #域名搜索列表,类似于/etc/resolv.conf中的searchoption #domain-name-servers #域名服务器地址,多个的话用逗号隔开 #option routers #网关 #option broadcasst-address #广播地址 #option subnet-mask #子网掩码 #default-lease-time #默认租约期限,一般这项生效(以秒为单位) #max-lease-time #最大租约期限(以秒为单位) #range START_IP END_IP #地址池(可以在subnet中指定多个range,但多个range所定义IP范围不能重复) #子网配置:通常每个作用域通过一个subnet定义 #subnet NETWORK_ADDR netmask NETMASK { # range # option routers # } #主机配置:通常为某特定MAC地址固定的分配一个地址 # host ‘HOST ID‘ { # hardware ethernet 08:00:07:26:c0:a5; # fixed-address IP; # }
服务进程:
[root@marvin ~]# /etc/init.d/dhcpd restart Shutting down dhcpd: [ OK ] Starting dhcpd: [ OK ] [root@marvin ~]# ps -ef |grep dhcp dhcpd 1952 1 0 03:53 ? 00:00:00 /usr/sbin/dhcpd -user dhcpd -group dhcpd
服务端口:
[root@marvin ~]# ss -lnup State Recv-Q Send-Q Local Address:Port Peer Address:Port UNCONN 0 0 *:67 *:* users:(("dhcpd",1952,7)) UNCONN 0 0 *:68 *:* users:(("dhclient",2066,6))
demo:
[root@marvin dhcp]# vim dhcpd.conf #全局变量 option domain-name "marvin"; option domain-name-servers 114.114.114.114,8.8.8.8; option routers 192.168.1.1; option broadcast-address 192.168.1.255; default-lease-time 600; max-lease-time 7200; log-facility local7; subnet 192.168.1.0 netmask 255.255.255.0{ range 192.168.1.201 192.168.1.240; host sherry{ #根据主机mac配置固定ip hardware ethernet 00:0c:29:3c:e2:60;#sherry主机的mac地址 fixed-address 192.168.1.221; #给sherry主机分配固定ip } }
log:
服务排错:
[root@marvin /]# tail -f /var/log/messages May 19 06:24:47 marvin dhcpd: Dynamic and static leases present for 192.168.1.221. May 19 06:24:47 marvin dhcpd: Remove host declaration sherry or remove 192.168.1.221 May 19 06:24:47 marvin dhcpd: from the dynamic address pool for 192.168.1.0/24 May 19 06:24:47 marvin dhcpd: DHCPREQUEST for 192.168.1.221 from 00:0c:29:3c:e2:60 via eth1 May 19 06:24:47 marvin dhcpd: DHCPACK on 192.168.1.221 to 00:0c:29:3c:e2:60 via eth1 May 19 06:29:43 marvin dhcpd: Dynamic and static leases present for 192.168.1.221. May 19 06:29:43 marvin dhcpd: Remove host declaration sherry or remove 192.168.1.221 May 19 06:29:43 marvin dhcpd: from the dynamic address pool for 192.168.1.0/24 May 19 06:29:43 marvin dhcpd: DHCPREQUEST for 192.168.1.221 from 00:0c:29:3c:e2:60 via eth1 May 19 06:29:43 marvin dhcpd: DHCPACK on 192.168.1.221 to 00:0c:29:3c:e2:60 via eth1
服务log:
[root@marvin /]# tail -f /var/log/boot.log May 19 06:24:47 marvin dhcpd: Dynamic and static leases present for 192.168.1.221. May 19 06:24:47 marvin dhcpd: Remove host declaration sherry or remove 192.168.1.221 May 19 06:24:47 marvin dhcpd: from the dynamic address pool for 192.168.1.0/24 May 19 06:24:47 marvin dhcpd: DHCPREQUEST for 192.168.1.221 from 00:0c:29:3c:e2:60 via eth1 May 19 06:24:47 marvin dhcpd: DHCPACK on 192.168.1.221 to 00:0c:29:3c:e2:60 via eth1 May 19 06:29:43 marvin dhcpd: Dynamic and static leases present for 192.168.1.221. May 19 06:29:43 marvin dhcpd: Remove host declaration sherry or remove 192.168.1.221 May 19 06:29:43 marvin dhcpd: from the dynamic address pool for 192.168.1.0/24 May 19 06:29:43 marvin dhcpd: DHCPREQUEST for 192.168.1.221 from 00:0c:29:3c:e2:60 via eth1 May 19 06:29:43 marvin dhcpd: DHCPACK on 192.168.1.221 to 00:0c:29:3c:e2:60 via eth1
原文地址:http://9173436.blog.51cto.com/9163436/1775052