DHCP:Dynamic Host Configure Protocol 动态主机配置协议
广播地址:FF-FF-FF-FF-FF-FF
广播IP 地址:255.255.255.0
DHCP的作用:
负责为客户机动态分配TCP/IP信息
IP地址
子网掩码
默认网关
首先DNS服务器(rhel6.3分配IP地址是从小到大)
为什么要采用DHCP:
减小管理员的工作量
减小输入错误的可能
避免IP冲突
提高了IP地址的利用率
DHCP服务的原理:
当IP地址不是固定绑定到哪台主机时,并且需要在客户机上先配置静态的ip,然后再改为动态获取,这时则为二个广播二个单播,否则都是广播
客户机请求IP地址(DHCPDiscover)广播
服务器响应请求(DHCPOffer)-->单播,发Offter之前,会发一个广播,确认所分配的IP地址是不是静态配置的
客户机选择IP地址(DHCPRequest)广播
服务器确认租约(DHCPAck)单播
续约的时候发的是request包,时间大概在租期的80%的时间,如果续租不到,就重新发discover包,重新到别的dhcp服务器获得ip地址
安装dhcpd服务的过程:
当使用wireshark进行抓包时,如果想要看到DHCP四条动态时,则需要删除租约文件,并且重新刷新服务,目的是清除以前分配的IP地址
[root@teacher dhcpd]# rm -rf /var/lib/dhcpd/dhcpd.leases :服务器租约文件
service dhcpd restart
[root@station ~]# cat /var/lib/dhclient/dhclient-eth0.leases :客服机也是有租约文件
lease {
interface "eth0";
fixed-address 10.0.0.63;
filename "pxelinux.0";
[root@station ~]# vim /etc/dhcp/dhcpd.conf DHCP配置文件,设置相应的网段,掩码等相关信息
[root@lichao520 dhcp]# cat dhcpd.conf
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 10.5.5.0 netmask 255.255.255.0 {
range 10.5.5.26 10.5.5.130;
option domain-name-servers 8.8.8.8;
option domain-name "sxkeji.com.cn";
option routers 10.5.5.254;
}
host fantexi {
hardware ethernet 00:0c:29:11:22:ab;
fixed-address 10.5.5.88;
}
[root@station ~]# cat /etc/sysconfig/dhcpd
在这个文件里可以设置在哪个网卡上提供DHCP服务,不写的话,开启了DHCP服务的网卡默认都是
# Command line options here
DHCPDARGS=
dhclient -r eth0:释放ip地址
dhclient eth0 : 获得ip,要求网卡配置文件里采用dhcp方式获得ip
如果在/tec/dhcp/dhcp.d中定义了设备类型为local7,在进行dhcpd服务时可以在/var/log/messages和/var/log/boot.log查看相关信息
查看log,因为dhcpd.conf配置文件里定义的设备类型为local7,同时/etc/rsyslog.conf文件里规定的log文件的路径在/var/log/boot.log
dhcpd.conf
log-facility local7;
==
vim /etc/rsyslog.conf
# Save boot messages also to boot.log
local7.* /var/log/boot.log
windows里的机器
ipconfig /release :释放ip---》169.254开头的ip,说明已经没有有效ip了
ipconfig /renew :获得ip
tail -f /var/log/boot.log 对dhcpd服务进行监听
May 30 10:43:50 lichao520 dhcpd: DHCPDISCOVER from 00:0c:29:11:22:ab (PC-20130503LURF) via eth0
May 30 10:43:51 lichao520 dhcpd: DHCPOFFER on 10.0.29.10 to 00:0c:29:11:22:ab (PC-20130503LURF) via eth0
May 30 10:43:51 lichao520 dhcpd: DHCPREQUEST for 10.0.29.10 (192.168.1.1) from 00:0c:29:11:22:ab (PC-20130503LURF) via eth0
May 30 10:43:51 lichao520 dhcpd: DHCPACK on 10.0.29.10 to 00:0c:29:11:22:ab (PC-20130503LURF) via eth0
May 30 10:43:57 lichao520 dhcpd: DHCPINFORM from 10.0.29.10 via eth0: not authoritative for subnet 10.0.29.0
May 30 10:44:00 lichao520 dhcpd: DHCPINFORM from 10.0.29.10 via eth0: not authoritative for subnet 10.0.29.0
====
netstat -antup|grep dhcp :查看监听端口
udp 0 0 0.0.0.0:67 0.0.0.0:* 4591/dhcpd
n :numeric
a:all
p:program
t:tcp
u:udp
0.:67 表在所有的ip地址上67端口提供dhcp服务
[root@lichao520 ~]# chkconfig --list dhcpd
dhcpd 0:关闭1:关闭2:关闭3:关闭4:关闭5:关闭6:关闭
[root@lichao520 ~]# chkconfig dhcpd on 开机自动运行
[root@lichao520 ~]# chkconfig --list dhcpd
dhcpd 0:关闭1:关闭2:启用3:启用4:启用5:启用6:关闭
注意事项:
做dhcpd实验时
1.建议禁用NetworkManager服务
2.建议关闭防火墙 iptables -F
3.关闭selinux
service NetworkManager stop 停止服务
chkconfig NetworkManager off 设置开机不启动
vmnet0 ---》bridged
vmnet1--》hostonly
vmnet8--》nat
用rpm安装dhcpd
前提虚拟机设置为:hostonly和将虚拟机里的NAT取消
1)cd Packages/
2)rpm -ivh dhcp-4.1.1-31.P1.el6.i686.rpm
3)rpm -ql dhcp|grep sample
/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
4)cat /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample >>/etc/dhcp/dhcpd.conf
5)[root@localhost dhcp]# cat /etc/dhcp/dhcpd.conf
default-lease-time 600;
max-lease-time 7200;
log-facility local7;
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.2 192.168.0.244;
option domain-name-servers 8.8.8.8;
option routers 192.168.0.1;
}
host passacaglia {
hardware ethernet 0:0:c0:5d:bd:95;
filename "vmunix.passacaglia";
server-name "toccata.fugue.com";
}
6)配置静态IP 地址:192.168.0.2
7)service network restart
8)ifconfig eth0
9)service dhcpd start
10)在windows下配置静态IP 地址,之后开启wireshark,之后将IP地址,改为自动获取