一、DHCP技术概览
1、DHCP的简单概述与工作原理
2、DHCP服务器的安装和配置文件讲解
3、搭建一个简单的DHCP服务器,包括动态IP和静态IP的分配,并且开启DHCP服务自动加载
4、搭建一个多作用域的DHCP服务器(双网卡实现)
5、搭建一个超级作用域的DHCP服务器(共享作用域)
6、DHCP服务配置排错,检测配置文件
7、DHCP中继代理配置
8、DHCP客户端配置(Windows+Linux)
二、DHCP的简单概述与工作原理
- DHCP(Dynamic Host Configuration Protocal)就是动态主机配置协议,可以自动配置主机的 IP地址、子网掩码、网关及DNS等TCP/IP信息,其目的就是为了减轻TCP/IP网络的规划、管理和维护的负担,解决IP地址空间不足的情况。一般而言,当一个局域网内存在大量的主机,或者是存在相当多的移动设备,那么就适合搭建DHCP服务器。
- HDCP工作原理:
1、DHCP客户端在启动时发送一个dhcpdiscover的广播包,数据包源地址为0.0.0.0,而目的地址为255.255.255.255
2、当DHCP服务器收到广播包后会从地址池中选择可用的IP以dhcpoffer的广播包发送给客户端
3、DHCP客户端收到多台DHCP服务器相应时会采用第一个相应的IP,然后回复dhcprequest广播包
4、DHCP服务器收到dhcprequest广播包后会发送一个dhcpack消息,声明Ip租用成功
三、DHCP服务器的安装和配置文件讲解 - dhcp的安装
rpm -qa |grep dhcp / yum install dhcp
dhcp-3.0.5-18.el5.i386.rpm
dhcp-devel-3.0.5-18.el5.i386.rpm
dhcpv6-1.0.10-16.el5.i386.rpm
dhcpv6-client-1.0.10-16.el5.i386.rpm - dhcp的相关配置文件:
/etc/dhcpd.conf 主配置文件(主要文件)
/etc/sysconfig/dhcpd 辅助配置文件
/usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample dhcp服务器的配置文件模版。
/etc/init.d/dhcpd 启动脚本
/usr/sbin/dhcpd 守护进程
/var/lib/dhcpd/dhcpd.leases 服务器的租约文件
dhcp服务的启动和停止:
service dhcpd start|stop|restart
/etc/init.d/dhcpd start|status
[root@linuxidc ~]# cat /etc/dhcpd.conf //查看dhcp服务主配置文件,发现可用内容为空
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample //叫我们去查看模板文件
[root@linuxidc ~]# vim /etc/dhcpd.conf
进入编辑模式后,通过 r /usr/share/doc/dhcp*/dhcpd.conf.sample
把模板文件读取到主配置文件中,然后通过修改
参数就可以搞定配置文件,配置文件修改后就可以启用服务,所以关键在于该文件的修改!
具体内容如下:
#
ddns-update-style interim; //更新动态DNS,这里interim表示关闭动态更新
ignore client-updates; //忽略客户端更新DNS记录
subnet 192.168.0.0 netmask 255.255.255.0 //定义作用域
{
# — default gateway
option routers 192.168.0.1; //为客户端指定默认网关
option subnet-mask 255.255.255.0; //给客户端指定子网掩码
option nis-domain “domain.org”;
option domain-name “domain.org”;
option domain-name-servers 192.168.1.1; //为客户端指定DNS服务器地址
option time-offset -18000; # Eastern Standard Time
# option ntp-servers 192.168.1.1;
# option netbios-name-servers 192.168.1.1;
# — Selects point-to-point node (default is hybrid). Don’t change this unless
# — you understand Netbios very well
# option netbios-node-type 2;
range dynamic-bootp 192.168.0.128 192.168.0.254; //分配的IP范围,也即地址池
default-lease-time 21600; //默认租约时间,单位为秒
max-lease-time 43200; //最大租约时间,单位为秒
# we want the nameserver to appear at a fixed address
host ns {
next-server marvin.RedHat.com;
hardware ethernet 12:34:56:78:AB:CD; //指定分配静态IP的主机mac地址
fixed-address 207.175.42.254; //分配的静态IP
}
}
四、搭建一个简单的DHCP服务器,包括动态IP和静态IP的分配,并且开启DHCP服务自动加载
服务需求:
定义作用域范围为192.168.1.96~127/26,网关为192.168.1.65/26,域名为linuxidc.com,
域名服务器为192.168.1.1和202.96.128.86.保留主机静态IP如下:
boss(192.168.1.100,11:22:33:44:55:66)
[root@linuxidc ~]# ipcalc 192.168.1.127/26 -bmn
NETMASK=255.255.255.192
BROADCAST=192.168.1.127
NETWORK=192.168.1.64
虚拟网卡VMware Network Adapter VMnet1作测试获取IP,所以把虚拟机的网卡连接模式
选择为Host-only
650) this.width=650;” border=0>
关闭Windows的vmware dhcp服务:
进去后选择VMware DHCP Server ,右键关闭即可!
[root@linuxidc ~]# cat /etc/dhcpd.conf
#
ddns-update-style interim;
ignore client-updates;
subnet 192.168.1.64 netmask 255.255.255.192 {
# — default gateway
option routers 192.168.1.65;
option subnet-mask 255.255.255.192;
option nis-domain “domain.org”;
option domain-name “linuxidc.com“;
option domain-name-servers 192.168.1.1,202.96.128.86;
option time-offset -18000; # Eastern Standard Time
# option ntp-servers 192.168.1.1;
# option netbios-name-servers 192.168.1.1;
# — Selects point-to-point node (default is hybrid). Don’t change this unless
# — you understand Netbios very well
# option netbios-node-type 2;
range dynamic-bootp 192.168.1.96 192.168.1.127;
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at a fixed address
host boss {
next-server marvin.RedHat.com;
hardware ethernet 11:22:33:44:55:66;
fixed-address 192.168.1.100;
}
}
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
#
开启服务:
[root@linuxidc ~]# service dhcpd restart
关闭 dhcpd: [确定]
启动 dhcpd: [确定]
开机自动加载(2种方法):
1、可以通过如下命令设置开机自动加载:
[root@linuxidc ~]# chkconfig –level 3 dhcpd on
2、也可以通过文本界面设置:
[root@linuxidc ~]# ntsysv //敲此命令可显示出文本界面来
先禁用虚拟网卡,然后重新开启让其自动获取IP地址
服务需求:
给两个网段自动分配IP地址,分别是218.192.87.0/24 、218.192.88.0/24
2个网段的网关都指向DHCP的网卡IP,满足IP扩容的需求,同时不至于中断原始网络。
这样既可以保持原有IP地址的规划,又可以扩容现有的网络IP地址。
如果选择直接配置多作用域实现动态IP分配的任务,则必须要为DHCP服务器添加多块网卡,
并配置多个IP地址,否则DHCP服务器只能分配与其现有网卡IP地址对应网段的作用域。
[root@linuxidc ~]# ifconfig eth0 218.192.87.24 netmask 255.255.255.0
[root@linuxidc ~]# ifconfig eth1 218.192.88.24 netmask 255.255.255.0
重启网卡使其网卡配置生效:
service network restart
由于这里需要定义两个subnet xxx netmask xxx ,所以我们可以通过set nu 列出行号,然后用nyy把
subnet xxx netmask xxx 模版复制到下面使用,防止手写出错。
[root@linuxidc ~]# vim /etc/dhcpd.conf
[root@linuxidc ~]# cat /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet 218.192.87.0 netmask 255.255.255.0 {
# — default gateway
option routers 218.192.87.24;
option subnet-mask 255.255.255.0;
option nis-domain “linuxidc.com“;
option domain-name “domain.org”;
option domain-name-servers 192.168.1.1;
option time-offset -18000; # Eastern Standard Time
# option ntp-servers 192.168.1.1;
# option netbios-name-servers 192.168.1.1;
# — Selects point-to-point node (default is hybrid). Don’t change this unless
# — you understand Netbios very well
# option netbios-node-type 2;
range dynamic-bootp 218.192.87.1 218.192.87.254;
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at a fixed address
host ns {
next-server marvin.RedHat.com;
hardware ethernet 00:0C:29:36:BE:6D;
fixed-address 218.192,87.24;
}
}
subnet 218.192.88.0 netmask 255.255.255.0 {
# — default gateway
option routers 218.192.88.24;
option subnet-mask 255.255.255.0;
option nis-domain “linuxidc.com“;
option domain-name “domain.org”;
option domain-name-servers 192.168.1.1;
option time-offset -18000; # Eastern Standard Time
# option ntp-servers 192.168.1.1;
# option netbios-name-servers 192.168.1.1;
# — Selects point-to-point node (default is hybrid). Don’t change this unless
# — you understand Netbios very well
# option netbios-node-type 2;
range dynamic-bootp 218.192.88.1 218.192.88.254;
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at a fixed address
host ns {
next-server marvin.redhat.com;
hardware ethernet 00:0C:29:36:BE:77;
fixed-address 218.192,88.24;
}
}
[root@linuxidc ~]# service dhcpd restart
关闭 dhcpd: [确定]
启动 dhcpd: [确定]
然后查看获取到的IP地址,在Linux中可以通过cat /var/lib/dhcpd/dhcpd.leases查看分配出去的IP地址
对于多作用域设置,使用多网卡的方式,虽然可以达到扩展可用IP地址范围的目的,
但会增加网络拓扑的复杂性,并加大维护的难度。而如果想保持现有网络的结构,
并实现网络扩容,可以选择采用shared-network超级作用域。
服务需求:
不改变原有网络结构,使用一块网卡给不同网段自动分配IP地址,分别是218.192.87.0/24 、
218.192.88.0/24 ,但在这里就必须给网关分配多个IP,假如对应两个网段的网关分别是
218.192.87.1/24和218.192.88.1/24,把各自的作用域指向各自的网关,就可以实现不同
作用域的网段互访和访问外网的需求了。
必须在出口开启DHCP中继,把IP指向DHCP的网卡IP。一般来说,DHCP中继做在路由器上或者多层
交换机的SVI接口上。这里考验的是网络架构的知识体系了。
[root@linuxidc ~]# cat /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
shared-network linuxidc {
option subnet-mask 255.255.255.0;
default-lease-time 21600;
max-lease-time 43200;
subnet 218.192.87.0 netmask 255.255.255.0 {
option routers 218.192.87.1;
range dynamic-bootp 218.192.87.2 218.192.87.254;
}
subnet 218.192.88.0 netmask 255.255.255.0 {
option routers 218.192.88.1;
range dynamic-bootp 218.192.88.2 218.192.88.254;
}
}
[root@linuxidc ~]# service dhcpd restart
关闭 dhcpd: [确定]
启动 dhcpd: [确定]
[root@linuxidc ~]# netstat -antup |grep dhcpd
udp 0 0 0.0.0.0:67 0.0.0.0:* 3729/dhcpd
七、DHCP服务配置排错,检测配置文件
1、本机ip地址与subnet定义的作用域不在同一个网段。
2、定义的地址池范围不能超过subnet定义的子网ip地址范围。
3、多网卡接口时,必须定义启动接口。
4、定义多个域名服务器的ip地址时,以逗号为分隔符。
假设在 option routers 218.192.87.1; 最后去掉“;”
重启服务:
[root@linuxidc ~]# service dhcpd restart
关闭 dhcpd: [确定]
启动 dhcpd: [失败]
[root@linuxidc ~]# dhcpd
Internet Systems Consortium DHCP Server V3.0.5-RedHat
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
/etc/dhcpd.conf line 12: semicolon expected. //显示说第12行出错
range
^
Configuration file errors encountered — exiting
…..
或者你可以另外开启一个终端,使用如下命令:
[root@linuxidc ~]# tail -f /var/log/messages //开启对失败信息的显示
然后在原先的终端重启DHCP服务,那么在另外的那个终端就会显示报错信息
很可以是因为DHCP服务器的网卡没有开启MULTICAST(多点传送)功能。
只要在该网卡上开启MULTICAST(多点传送)功能即可。
做法:创建一个到地址255.255.255.255的路由,加这条路由命令到/etc/rc.d/rc.local
使得每次启动后自动运行
[root@linuxidc ~]# route add -host 255.255.255.255 dev eth0
[root@linuxidc ~]# vim /etc/rc.d/rc.local
[root@linuxidc ~]# cat /etc/rc.d/rc.local
#!/bin/sh
#
# This script will be executed *after* all the other init scripts.
# You can put your own initialization stuff in here if you don’t
# want to do the full Sys V style init stuff.
touch /var/lock/subsys/local
route add -host 255.255.255.255 dev eth0
如果提示“255.255.255.255:Unkown host” ,那么我们需要修改/etc/hosts文件,
并添加一条主机记录。
[root@linuxidc ~]# vim /etc/hosts
[root@linuxidc ~]# cat /etc/hosts
# Do not remove the following line, or various programs
# that require network functionality will fail.
127.0.0.1 linuxidc linuxidc.com
::1 localhost6.localdomain6 localhost6
255.255.255.255 dhcp
在DHCP中继代理计算机上安装dhcp-3.0.5-7.el5.i386.rpm软件包
[root@linuxidc ~]# rpm -qa |grep dhcp-3.0
dhcp-3.0.5-18.el5
[root@linuxidc ~]# vim /etc/sysconfig/dhcrelay
[root@linuxidc ~]# cat /etc/sysconfig/dhcrelay
# Command line options here
INTERFACES=”eth0″
DHCPSERVERS=”218.192.87.24″
重启代理服务使其生效:
[root@linuxidc ~]# service dhcrelay start
启动 dhcrelay: [确定]
[root@linuxidc ~]# netstat -antup |grep 67
tcp 0 52 ::ffff:218.192.87.24:22 ::ffff:218.192.87.4:1674 ESTABLISHED 3254/0
udp 0 0 0.0.0.0:67 0.0.0.0:* 4000/dhcrelay
[root@linuxidc ~]# dhcrelay 218.192.87.24
Internet Systems Consortium DHCP Relay Agent V3.0.5-RedHat
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
Listening on LPF/eth1/00:0c:29:36:be:77
Sending on LPF/eth1/00:0c:29:36:be:77
Listening on LPF/eth0/00:0c:29:36:be:6d
Sending on LPF/eth0/00:0c:29:36:be:6d
Sending on Socket/fallback
[root@linuxidc ~]# service dhcrelay status
dhcrelay (pid 4052 4000) 正在运行…
注意点:
中继代理计算机默认不转发DHCP客户机的请求,需要指定DHCP服务器的位置,所以通过如上
两个方法任选其一解决。
九、DHCP客户端配置(Windows+Linux)
释放IP地址:ipconfig /release
重新申请IP地址:ipconfig /renew
配置Linux客户端需要修改网卡配置文件,将BOOTPROTO项设置为BOOTPROTO=dhcp
[root@linuxidc ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0
[root@linuxidc ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]
DEVICE=eth0
BOOTPROTO=dhcp
HWADDR=00:0C:29:36:BE:6D
IPADDR=218.192.87.24
GATEWAY=218.192.86.1
ONBOOT=yes
然后使用如下命令自动获取IP:
[root@linuxidc ~]# dhclient
Internet Systems Consortium DHCP Client V3.0.5-RedHat
Copyright 2004-2006 Internet Systems Consortium.
All rights reserved.
……
若IP没有获取到,并且显示进程运行,可以使用kill命令结束进程,重新获取
[root@linuxidc ~]# killall dhclient
[root@linuxidc ~]# dhclient eth0