DHCP(Dynamic Host Configuration Protocol)服务是一个局域网的网络协议。指的是由服务器或网络设备控制一段IP地址范围,客户机登录服务器时就可以自动获得服务器分配的IP地址和子网掩码。在小型网络环境中一般有路由器来提供DHCP服务,今天我们主要讨论一下Linux系统环境中的DHCP服务。
步骤一:安装服务
1.准备网络环境:2台Linux主机,一台为服务器端另一台为客户端。在服务器端配好yum仓库
2.安装DHCP服务
# yum -y install dhcp
Running Transaction
Installing : 12:dhcp-4.1.1-38.P1.el6.x86_64 1/1
rhel-packages/productid | 1.7 kB 00:00 ...
Verifying : 12:dhcp-4.1.1-38.P1.el6.x86_64 1/1
Installed:
dhcp.x86_64 12:4.1.1-38.P1.el6
步骤二::配置DHCP配置文件
#vim /etc/dhcp/dhcpd.conf
为某台客户端保留IP地址
host pc205{ //定义保留的主机名(宣告)
hardware ethernet 08:00:07:26:c0:a5; //mac地址
fixed-address 192.168.4.129; //保留的IP地址
}
3.开启DHCPD服务
#service dhcpd start
正在启动 dhcpd: [确定]
步骤三:验证服务
查看服务端口是否启用
[root@svr5 dhcp]# netstat -anput|grep dhcp
udp 0 0 0.0.0.0:67 0.0.0.0:* 3530/dhcpd
在客户端上重新获取IP地址
# dhclient
或者通过以下命令来验证
# dhclient -d //验证命令
Internet Systems Consortium DHCP Client 4.1.1-P1
Copyright 2004-2010 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/00:0c:29:49:ba:0a //以下是DHCP工作原理
Sending on LPF/eth0/00:0c:29:49:ba:0a
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 7 (xid=0x6ecab92b) //寻找DHCP服务器
DHCPOFFER from 192.168.4.5 //DHCP服务器提供IP地址
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x6ecab92b) //请求使用
DHCPACK from 192.168.4.5 (xid=0x6ecab92b) //同意使用
bound to 192.168.4.20 -- renewal in 3110 seconds.
故障排除:
现象1.
Jan 27 13:07:55 srv5 dhcpd: ^
Jan 27 13:07:55 srv5 dhcpd: /etc/dhcp/dhcpd.conf line 13: unexpected end of file
Jan 27 13:07:55 srv5 dhcpd: range 192.168.4.120 192.168.4.150;
Jan 27 13:07:55 srv5 dhcpd: ^
Jan 27 13:07:55 srv5 dhcpd: Configuration file errors encountered -- exiting
原因:编辑配置文件是缺少大括号
故障2
Jan 27 13:10:07 srv5 dhcpd: ^
Jan 27 13:10:07 srv5 dhcpd: /etc/dhcp/dhcpd.conf line 11: semicolon expected.
Jan 27 13:10:07 srv5 dhcpd: opton domain-name-server
Jan 27 13:10:07 srv5 dhcpd: ^
Jan 27 13:10:07 srv5 dhcpd: Configuration file errors encountered -- exiting
原因:参数配置错误,少了一个S。正确参数为:opton domain-name-servers
故障3
Jan 27 13:15:58 srv5 dhcpd: default-lesae-time 7200;
Jan 27 13:15:58 srv5 dhcpd: ^
Jan 27 13:15:58 srv5 dhcpd: Configuration file errors encountered -- exiting
原因:参数配置错误,正确参数为:default-lease-time
总结:
配置这个服务所有的错误都是因为拼写错误,所以以后再这方面要注意
当出现错误无法启动服务时,可以查看log文件寻找相关的错误信息。文件路径在:/var/log/messges
在拍错时,开始的时候只查看messages的后面十行显示的错误信息