1、dhcp简介:
Dynamic Host Configuration Protocol,动态主机配置协议。是一个局域网的网络协议,使用UDP协议工作, 主要有两个用途:给内部网络或网络服务供应商自动分配IP地址,给用户或者内部网络管理员作为对所有计算机作中央管理的手段。
DHCP协议采用客户端/服务器模型,主机地址的动态分配任务由网络主机驱动。当DHCP服务器接收到来自网络主机申请地址的信息时,才会向网络主机发送相关的地址配置等信息,以实现网络主机地址信息的动态配置。
DHCP具有以下功能:
1. 保证任何IP地址在同一时刻只能由一台DHCP客户机所使用。
2. DHCP应当可以给用户分配永久固定的IP地址。
3. DHCP应当可以同用其他方法获得IP地址的主机共存(如手工配置IP地址的主机)。
4. DHCP服务器应当向现有的BOOTP客户端提供服务。
服务模式:C/S;
server:dhcp server(运行dhcp服务);
udp:监听67
client:dhcp client(运行dhcp程序);
udp:监听68
2、安装配置dhcp:
[[email protected] ~]# service iptables stop
[[email protected] ~]# setenforce 0 :关闭防火墙和selinux;
[[email protected] ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:B2:B7:26
inet addr:192.168.57.172 Bcast:192.168.57.255 Mask:255.255.255.128
inet6 addr: fe80::20c:29ff:feb2:b726/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:15489 errors:0 dropped:0 overruns:0 frame:0
TX packets:6489 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:10154545 (9.6 MiB) TX bytes:594358 (580.4 KiB)
eth1 Link encap:Ethernet HWaddr 00:0C:29:B2:B7:30
inet addr:1.1.1.1 Bcast:1.1.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:feb2:b730/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:336 errors:0 dropped:0 overruns:0 frame:0
TX packets:76 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:66858 (65.2 KiB) TX bytes:21624 (21.1 KiB)
:添加一块网卡eth1,留给dhcp服务使用。eth0位置管理使用;
[[email protected] ~]# yum -y install dhcp :安装dhcp服务;
[[email protected] ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y
[[email protected] ~]# :复制一个dhcpd配置文件示例到固定位置;
[[email protected] ~]# vim /etc/dhcp/dhcpd.conf
dhcpd.conf大致可以分为四个区域:(;分号结尾)
1、定义dhcpd自身的工作属性:
log-facility:日志facilify;
2、全局地址分配属性:option打头(全局有效,不影响subnet中的)
注意:(“作用范围越小,优先级越大;作用范围越大,优先级越小”)
option router :分配网关的;
全局的优先级小于作用域的优先级;
相当于默认配置;
3、子网配置:(“作用范围越小,优先级越大;作用范围越大,优先级越小”)
通常每个作用域通过一个subnet定义;
subnet NETWORK_ADDRESS netmask NETMASK {
地址池(range)、网关、dns
}
注意:作用域的优先级优先于全局优先级;
4、主机配置:
通常为某特定mac地址固定的分配一个固定地址;
host ‘HOST ID‘ {
mac ip地址
}
注意:保留的主机地址,不要出现在任何其他地址池当中,不然会冲突的;
实例1:定义一个dhcp地址池,使用1.1.1.0/24网段,下发给客户端的地址范围为:1.1.1.2-1.1.1.10,网关为1.1.1.1:
[[email protected] ~]# vim /etc/dhcp/dhcpd.conf
subnet 1.1.1.0 netmask 255.255.255.0 {
range 1.1.1.2 1.1.1.10;
option routers 1.1.1.1;
}
[[email protected] ~]# service dhcp restart :重启dhcp服务;
[[email protected] ~]# ps -aux | grep dhcp
Warning: bad syntax, perhaps a bogus ‘-‘? See /usr/share/doc/procps-3.2.8/FAQ
dhcpd 27398 0.0 0.2 49088 4044 ? Ss 23:03 0:00 /usr/sbin/dhcpd -user dhcpd -group dhcpd
root 27403 0.0 0.0 103256 844 pts/0 S+ 23:05 0:00 grep dhcp
[[email protected] ~]# :查看dhcp进程;
找了台windowns机器,设置网卡自动获得ip地址:
实例2:配置dhcp,为特定地址保留分配固定的地址;
1、确定被固定ip者的网卡mac地址,这里是0050-56c0-0002
2、编辑dhcp主配置文件:
[[email protected] ~]# vim /etc/dhcp/dhcpd.conf
host test {
hardware ethernet 00:50:56:c0:00:02;
fixed-address 1.1.1.10;
option routers 1.1.1.1;
option domain-name "test.com";
option domain-name-servers 202.96.209.133;
} 实现将1.1.1.10这个ip地址下发给mac地址为0050-56c0-0002的主机;
[[email protected] ~]# service dhcpd force-reload :重载dhcp配置文件;
重启下windows机器上的测试网卡;
[[email protected] ~]# tail -f /var/log/boot.log :动态观察dhcp服务ip下发过程;
Apr 1 23:38:24 localhost dhcpd: uid lease 1.1.1.2 for client 00:50:56:c0:00:02 is duplicate on 1.1.1.0/24
Apr 1 23:38:24 localhost dhcpd: DHCPDISCOVER from 00:50:56:c0:00:02 via eth1
Apr 1 23:38:24 localhost dhcpd: DHCPOFFER on 1.1.1.10 to 00:50:56:c0:00:02 via eth1
Apr 1 23:38:24 localhost dhcpd: uid lease 1.1.1.2 for client 00:50:56:c0:00:02 is duplicate on 1.1.1.0/24
Apr 1 23:38:24 localhost dhcpd: DHCPDISCOVER from 00:50:56:c0:00:02 via eth1
Apr 1 23:38:24 localhost dhcpd: DHCPOFFER on 1.1.1.10 to 00:50:56:c0:00:02 via eth1
Apr 1 23:38:24 localhost dhcpd: Dynamic and static leases present for 1.1.1.10.
Apr 1 23:38:24 localhost dhcpd: Remove host declaration test or remove 1.1.1.10
Apr 1 23:38:24 localhost dhcpd: from the dynamic address pool for 1.1.1.0/24
Apr 1 23:38:24 localhost dhcpd: uid lease 1.1.1.2 for client 00:50:56:c0:00:02 is duplicate on 1.1.1.0/24
Apr 1 23:38:24 localhost dhcpd: DHCPREQUEST for 1.1.1.10 (1.1.1.1) from 00:50:56:c0:00:02 via eth1
Apr 1 23:38:24 localhost dhcpd: DHCPACK on 1.1.1.10 to 00:50:56:c0:00:02 via eth1
ok,ip地址成功固定分配;
结束;