如果没有安装,则通过yum方式执行安装,命令操作如下:
[root@localhost ~]# yum-y install dhcp
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_6412:4.1.1-38.P1.el6 will be installed
--> Finished Dependency Resolution
.. ..
Installed:
dhcp.x86_64 12:4.1.1-38.P1.el6
Complete!
默认的配置文件/etc/dhcp/dhcpd.conf内无任何有效配置,但是dhcp包提供了样例配置文件,可以直接复制过来使用:
[root@localhost ~]# cat/etc/dhcp/dhcpd.conf //查看默认的配置内容
# DHCP Server Configuration file.
# see/usr/share/doc/dhcp*/dhcpd.conf.sample
# see ‘man 5 dhcpd.conf‘
#
cp/usr/share/doc/dhcp*/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
//复制样例配置文件
cp:是否覆盖“/etc/dhcp/dhcpd.conf”? y //覆盖原有文件
拷贝过来的文件内容仅供参考,执行的操作及有效配置内容如下所示:
[root@localhost ~]# vim/etc/dhcp/dhcpd.conf
ddns-update-style interim;
default-lease-time28800; //默认租约8小时
max-lease-time 86400; //最大租约24小时
option domain-name"tarena.com"; //默认搜索域
option domain-name-servers192.168.1.5,202.106.0.20; //DNS服务器
subnet 192.168.1.0 netmask 255.255.255.0{ //定义作用域#网段声明
range 192.168.1.50 192.168.1.100; //IP范围1
range 192.168.1.120 192.168.1.200; //IP范围2
option subnet-mask 255.255.255.0; //分配的子网掩码
option routers 192.168.1.1; //分配的默认网关地址
}
启动并确认dhcpd服务的运行状态,dhcpd服务默认在UDP 67端口提供地址分配服务,而客户机发送请求的端口是UDP 68,操作及结果如下所示:
[root@localhost ~]# service dhcpd start
启动 dhcpd: [确定]
[root@localhost ~]# netstat -anpu | grep dhcpd
udp 0 0 0.0.0.0:67 0.0.0.0:*13255/dhcpd //服务于UDP 67端口
如果只是希望测试一下dhcpd服务是不是好用,可以直接使用带-d选项的dhclient工具,表示调试模式。运行时会占用当前终端,输出DHCP会话结果,比如,以下操作及输出可看出eth0已经成功从DHCP服务器获得地址192.168.1.200:
[root@localhost ~]#dhclient -d eth0
Internet Systems Consortium DHCP Client4.1.1-P1
Copyright 2004-2010 Internet SystemsConsortium.
All rights reserved.
For info, please visithttps://www.isc.org/software/dhcp/
Listening on LPF/eth0/00:0c:29:82:09:e9
Sending on LPF/eth0/00:0c:29:82:09:e9
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255port 67 interval 5 (xid=0x5a5bc3a0)
DHCPOFFER from 192.168.1.2
DHCPREQUEST on eth0 to 255.255.255.255 port67 (xid=0x5a5bc3a0)
DHCPACK from 192.168.1.2 (xid=0x5a5bc3a0)
bound to 192.168.1.200 -- renewal in 12945seconds.
按Ctrl+c快捷键可结束调试,返回命令行后检查eth0网卡的IP地址,应该已经自动配置为通过DHCP方式获得的192168.1.200,操作及结果如下所示:
[root@localhost ~]# ifconfig eth0
eth0 Link encap:Ethernet HWaddr00:0C:29:82:09:E9
inet addr:192.168.1.200 Bcast:192.168.1.255Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe82:9e9/64Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500Metric:1
RX packets:3497 errors:0 dropped:0overruns:0 frame:0
TX packets:807 errors:0 dropped:0overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:328697 (320.9 KiB) TX bytes:122802(119.9 KiB)
检查客户机默认网关地址,应该已设置为192.168.1.1:
[root@localhost ~]# route-n
Kernel IP routing table
Destination Gateway Genmask Flags MetricRef Use Iface
192.168.1.0 0.0.0.0 255.255.255.0 U 0 0 0eth0
0.0.0.0 192.168.1.1 0.0.0.0 UG 0 0 0 eth0
检查客户机的DNS服务器地址配置,应该为192.168.1.5和202.106.0.20,默认搜索域是tarena.com,结果如下所示:
[root@localhost ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search tarena.com
nameserver 192.168.1.5
nameserver 202.106.0.20
如果希望客户机一直采用DHCP方式来配置网络,应该修改网卡配置文件。使用system-config-network,或者直接vim改ifcfg-eth0文件,打开文件后将其中的“BOOTPROTO=none”修改为“BOOTPROTO=dhcp”:
[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
# Intel Corporation 82545EM GigabitEthernet Controller (Copper)
DEVICE=eth0
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
然后重启network服务即可,相关操作及结果如下所示:
[root@localhost ~]# service network restart
正在关闭接口 eth0: [确定]
关闭环回接口: [确定]
弹出环回接口: [确定]
弹出界面 eth0:
正在决定 eth0 的 IP 信息...完成。
[root@localhost ~]#ifconfig eth0 | grep "inet addr"
inet addr:192.168.1.200 Bcast:192.168.1.255Mask:255.255.255.0
找到subnet作用域配置段,在其中添加host 主机名 { ... }配置段,并重启dhcpd服务。使用“hardwre ethernet”声明来指定已知的网络打印机的MAC地址,根据此MAC地址进行识别,使用“fixed-address”声明来定义要分配的固定IP地址,如下所示:
[root@localhost ~]# vim/etc/dhcp/dhcpd.conf
.. ..
subnet 192.168.1.0 netmask 255.255.255.0 {
.. ..
host printer { //为printer配置保留地址
hardware ethernet 00:c0:c3:22:46:81; //已知网络打印机的MAC地址
fixed-address 192.168.1.8;
}
}
[root@localhost ~]#service dhcpd restart
关闭 dhcpd: [确定]
启动 dhcpd: [确定]
---------------------------------------------------------------------
[root@svr1 ~]# vim /etc/dhcpd.conf
host pc205 {
hardware ethernet00:0C:29:4A:8C:27;
fixed-address 192.168.4.20
---------------------------------------------------------------------
沿用前面的客户机,通过临时修改MAC地址的方式模拟为实验要求的网络打印机。修改并确认MAC地址的操作如下所示:
[root@localhost ~]#ifconfig eth0 | grep HWaddr //查看原MAC地址
eth0 Link encap:Ethernet HWaddr00:0C:29:82:09:E9
[root@localhost ~]#ifconfig eth0 hw ether 00:c0:c3:22:46:81 //修改MAC
[root@localhost ~]#ifconfig eth0 | grep HWaddr //查看新MAC地址
eth0 Link encap:Ethernet HWaddr00:C0:C3:22:46:81
再次启用dhclient -d调试,可以看到IP地址已经变成192.168.1.8。因为MAC地址模拟成网络打印机的了,DHCP服务器就以预先为其保留的IP进行分配了,如下所示:
[root@localhost ~]# dhclient -d eth0
.. ..
DHCPREQUEST on eth0 to 255.255.255.255 port67 (xid=0x7effd760)
DHCPNAK from 192.168.1.2 (xid=0x7effd760)
DHCPDISCOVER on eth0 to 255.255.255.255port 67 interval 3 (xid=0x3be3edf1)
DHCPOFFER from 192.168.1.2
DHCPREQUEST on eth0 to 255.255.255.255 port67 (xid=0x3be3edf1)
DHCPACK from 192.168.1.2 (xid=0x3be3edf1)
bound to 192.168.1.8 -- renewal in 13129seconds.
测试完毕后,执行以下操作恢复原来的MAC地址:
[root@localhost ~]#ifconfig eth0 hw ether 00:0c:29:82:09:e9
查看DHCP客户机的/var/lib/dhclient/dhclient-eth0.leases文件,其中记录了曾经获取过的IP租约信息,如下所示:
[root@localhost ~]# cat/var/lib/dhclient/dhclient-eth0.leases
.. ..
lease {
interface "eth0";
fixed-address 192.168.1.200;
option subnet-mask 255.255.255.0;
option routers 192.168.1.1;
option dhcp-lease-time 28800;
option dhcp-message-type 5;
option domain-name-servers 192.168.1.5,202.106.0.20;
option dhcp-server-identifier 192.168.1.2;
option domain-name "tarena.com";
renew 2 2013/9/17 10:55:12;
rebind 2 2013/9/17 14:26:04;
expire 2 2013/9/17 15:26:04;
}
查看DHCP服务器端的/var/lib/dhcpd/dhcpd.leases文件,其中记录了已经分配出去的IP租约(不包括保留地址)相关信息,如下所示:
[root@localhost ~]# cat/var/lib/dhcpd/dhcpd.leases
.. ..
lease 192.168.1.200 {
starts 2 2013/09/17 07:26:04;
ends 2 2013/09/17 15:26:04;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:82:09:e9;
}
原文地址:http://315805980.blog.51cto.com/2027806/1555249