今天折腾了dhcp主从服务器
从官方http://www.isc.org/software/dhcp下载了dhcp-4.2.3-P2.tar.gz
然后开了两台虚拟机,分别编译安装
shell命令用红色注明,其他为系统输出
1、编译安装
tar -xvzf dhcp-4.2.3-P2.tar.gz -C /usr/local/src
cd /usr/local/src/dhcp-4.2.3-P2
./configure –prefix=/usr/local/dhcp4
make && make install
2、设置配置文件dhcpd.conf
主服务器dhcpd.conf的配置如下,该Server eth0接口ip为192.168.0.2
cp /usr/local/dhcp4/etc/dhcpd.conf /etc/
vim /etc/dhcpd.conf
cat /etc/dhcpd.conf
default-lease-time 200;
max-lease-time 400;
log-facility local7;
failover peer “my” {
primary;
address 192.168.153.2;
port 647;
peer address 192.168.153.3;
peer port 647;
max-response-delay 30;
max-unacked-updates 10;
load balance max seconds 3;
mclt 1800;
split 129;
}
subnet 192.168.153.0 netmask 255.255.255.0 {
pool {
failover peer “my”;
range 192.168.153.4 192.168.153.254;
}
}
由该配置也可以看出来从服务器ip为192.168.0.3。
从服务器配置如下:
[root@localhost ~]#cat /etc/dhcpd.conf
default-lease-time 200;
max-lease-time 400;
log-facility local7;
failover peer “my” {
secondary;
address 192.168.153.3;
port 647;
peer address 192.168.153.2;
peer port 647;
max-response-delay 30;
max-unacked-updates 10;
load balance max seconds 3;
}
subnet 192.168.153.0 netmask 255.255.255.0 {
pool {
failover peer “my”;
range 192.168.153.4 192.168.153.254;
}
}
可以看出,不同点在从服务器没有 mclt 和 split 字段属性。为主服务器特有,然后是主服务器有primary声明,从有相应的secondary,其他基本一致。
3、启动Master和Slave Server并测试客户端平滑切换
由于我们编译安装在/usr/local/dhcp4/
而默认启动dhcp的时候是从根下etc读取dhcpd.conf文件
所以才有之前cp /usr/local/dhcp4/etc/dhcpd.conf /etc/ 这里步骤
当然你不想这么多也是可以的,只不过每次启动的时候要指定配置文件所在
[root@localhost ~]#/usr/local/dhcp4/sbin/dhcpd -cf /usr/local/dhcp4/etc/dhcpd.conf
每次启动的时就不会去/etc找配置文件啦。
然后是编译安装有个问题,这样启动还是会报错如下:
[root@localhost ~]#/usr/local/dhcp4/sbin/dhcpd
Internet Systems Consortium DHCP Server 4.2.3-P2
Copyright 2004-2012 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Can’t open lease database /var/db/dhcpd.leases: No such file or directory –
check for failed database rewrite attempt!
Please read the dhcpd.leases manual page if you
don’t know what to do about this.
那我们手动touch即可
[root@localhost ~]# touch /var/db/dhcpd.leases
[root@localhost ~]# /usr/local/dhcp4/sbin/dhcpd
Internet Systems Consortium DHCP Server 4.2.3-P2
Copyright 2004-2012 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Wrote 0 leases to leases file.
Listening on LPF/eth0/00:0c:29:42:c5:f8/192.168.153.0/24
Sending on LPF/eth0/00:0c:29:42:c5:f8/192.168.153.0/24
Sending on Socket/fallback/fallback-net
failover peer my: I move from recover to startup
[root@localhost ~]#netstat -anp | grep dhc
tcp 0 0 192.168.153.3:647 0.0.0.0:* LISTEN 3425/dhcpd
tcp 0 0 192.168.153.3:647 192.168.153.2:58360 ESTABLISHED 3425/dhcpd
udp 0 0 0.0.0.0:11905 0.0.0.0:* 3425/dhcpd
udp 0 0 0.0.0.0:67 0.0.0.0:* 3425/dhcpd
udp 0 0 :::50794 :::* 3425/dhcpd
raw 0 0 0.0.0.0:1 0.0.0.0:* 7 3425/dhcpd
unix 2 [ ] DGRAM 11298 3425/dhcpd
[root@localhost ~]#
可以看到这样dhcp就启动了包括67号端口和647(主从通信端口,自定义的)
启动正常后,从服务器也同样按这样来,然后是打开一个客户端,即另一台linux虚拟机测试:
可以看到客户端接收到服务端的DHCPREQUEST包
并且客户端发出请求192.168.153.101地址的数据包
但是没有收到服务端的offer(DHCPOFFERS)
4、解决碰到的问题
然后我一直在配置文件中找错…两个小时过去了
我先监控日志文件/var/log/messages
然后再运行dhclient
[root@localhost ~]#tail -f /var/log/messages
<span style=”color:#000000;”>Mar 4 00:12:23 localhost dhcpd: Failover CONNECT to my rejected:Connection rejected, time mismatch too great.
</span>
大致意思就是错误的连接,时间不匹配太大
于是想到时区时间问题就把所有主从服务器包括客户端的时间全部统一了
然后重启服务就搞定了
5、留下来的疑问
这个时间不匹配究竟是谁和谁呢?我表示很疑惑
于是先把主从的时间 统一了
主:
[root@localhost ~]#date Sun Mar 4 00:24:21 CST 2012 [root@localhost ~]#cp /usr/share/zoneinfo/Asia/Chongqing /etc/localtime cp: overwrite `/etc/localtime'? y [root@localhost ~]#date Sun Mar 4 00:24:41 CST 2012 [root@localhost ~]#
[root@localhost ~]#date
Sat Mar 4 00:24:31 CST 2012
[root@localhost ~]#cp /usr/share/zoneinfo/Asia/Chongqing /etc/localtime
cp: overwrite `/etc/localtime’? y
[root@localhost ~]#date
Sat Mar 4 00:24:43 CST 2012
…
还是老样子…和上面一样的错误
strong>于是初步下定结论,时间不同步是服务器和客户端之间的时间不同步
然后修改客户端时区,选一个与服务器不同时区的地区
服务器是 亚洲/重庆
那么客户端我就选日本吧
再次运行dhclient还是如此
Mar 4 00:32:50 localhost dhcpd: Failover CONNECT to my rejected: Connection rejected, time mismatch too great.
重启两台DHCP Server,然后再次运行dhclient
Mar 5 00:02:16 localhost dhcpd: DHCPDISCOVER from 00:0c:29:5e:f5:d7 via eth0: not responding (recover wait)
echo “” > /var/db/dhcpd.leases
killall dhcpd
. .bash_profile
重启了dhcpd服务,然后再次使用客户端获取IP就没问题了
原来是租约文件影响了结果
那么之前的结论就无效了
实验环境已经破坏
本人也不想去整个时区错误了。
总之时区、租约文件都会影响到客户端获取IP。
折腾久了别忘了清掉dhcpd.leases文件即可。
重启了dhcpd服务重启了dhcpd服务