一,DHCP 服务器 rpm 包的配置
大家好!
这次我们来学习 Linux 系统中的 DHCP 服务器的配置。主要目标有两个:
1,配置 DHCP 服务器给客户端计算机分配 IP 地址,网络地址,DNS 和网关地址
2,为某台客户机保留 IP 地址
下面我们开始了!首先我们检查是否已经安装了 DHCP 服务器端的软件
[root@localhost ~]# rpm -qa | grep dhcp 查询 DHCP 没有安装
-8
[root@localhost ~]# mount /media/cdrom/ 挂载 AS4 第 4 张盘
我们服务器上要安装的是 ,那么下面我们来安装。
[root@localhost ~]# rpm -ivh /media/cdrom/RedHat/RPMS/
DHCP 服务器的配置文件为/etc/,默认情况下此文件不存在,不过当 DHCP 软件包安装之后会提
供一个配置模板:/usr/share/doc/
[root@localhost RPMS]# rpm -ql dhcp
/usr/share/doc/ (模版配置文件)
[root@localhost RPMS]# cp /usr/share/doc/ /etc/
将模板拷贝到/etc/目录下命名为 ,然后在该文件上进行相关的配置即可。
到现在为止,我们的准备工作已经 OK,下面我们就正式开始架设 DHCP 服务器
实例:
目前我的内部网段设定为 这一段,且默认网关为 ,此外,DNS 主机的 IP
为 ,所分配的网段内的子网掩码是 我想要让每个使用者默认租约时间为 21600s,最
大租约时间为 43200s;局域网内所有主机的域名为“” 我只想要分配的 IP 只有 到
这几个,其它的 IP 则保留下来; 我的主机的 MAC 是 00:0C:29:F7:DB:70 ,我要给主机
名称为 crazylinux IP 为 这个。
以下为修改之后的 文件的内容:
[root@localhost RPMS]# cat /etc/
ddns-update-style interim;
ignore client-updates; #dhcp 服务器不要为它分配 ip 地址更新本地域名的 dns 记录
subnet netmask { #申明分配 ip 的子网范围
# --- default gateway
option routers ; #定义网关
option subnet-mask ; #定义子网掩码
option nis-domain ""; #定义 nis 域名
option domain-name ""; #定义 dns 服务器域名
option domain-name-servers ; #定义 dns 服务器的 ip
option time-offset -18000; # Eastern Standard Time
# option ntp-servers ;
# option netbios-name-servers ;
# --- 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 ; #分配 ip 地址的范围
default-lease-time 21600; #定义的默认租约时间 6 小时
max-lease-time 43200; #定义的最大租约时间 12 小时
# we want the nameserver to appear at a fixed address
host crazylinux { #定义为客户机绑定 ip 的主机名
hardware ethernet 00:0C:29:F7:DB:70; #所绑定主机的 mac 地址
fixed-address ; #所绑定的 ip
}
}
完成之后重启服务:service dhcpd restart,若服务启动成功则可以使用一台客户机进行测试。这样一台最简
单的 DHCP 服务器就架设完毕。
3,租约文件
在 DHCP 服务器上,/var/lib/dhcp/ 文件中存放着 DHCP 客户租期数据库。只要 DHCP 服务器能
够成功启动,该数据库就可以自动创建。并且,所有通过该 DHCP 服务器分配到地址的客户机的地址信息
都会存储于该文件中。
该租期数据库文件经常被重建,但是不应该手工修改。
另外如果主机上安装了两个网卡,但是只想让 DHCP 服务在其中的一个网卡上监听,则需要配置 DHCP 服
务器只在那个设备上启动。在/etc/sysconfig/dhcpd 中,把网卡接口的名称添加到 DHCPDARGS 列表中。
#Command line options here
最后在客户端上使用 netconfig 命令设置客户端自动获取地址,完成后重启服务即可。
[root@localhost RPMS]# cat /var/lib/dhcp/ 在服务器上查看已经分配出去的 IP
# All times in this file are in UTC (GMT), not your local timezone. This is
# not a bug, so please don't ask about it. There is no portable way to
# store leases in the local timezone, so please don't request this as a
# feature. If this is inconvenient or confusing to you, we sincerely
# apologize. Seriously, though - don't ask.
# The format of this file is documented in the (5) manual page.
# This lease file was written by
lease {
starts 6 2007/03/17 04:36:31;
ends 6 2007/03/17 10:36:31;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:67:57:c1;
}
lease {
starts 6 2007/03/17 04:37:13;
ends 6 2007/03/17 10:37:13;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:ef:cc:aa;
uid "\001\000\014)\357\314\252";
client-hostname "cheshi-eeb7e489";
}
嘿嘿 成功。。。
二,DHCP 服务器源码包配置
还是和上面的 rpm 包一样,我们这次用源码包来设置 dhcp 服务器,在配置之前首先准备工作:
软件:
gcc
1,先安装 gcc
[root@localhost cdrom]# rpm -ivh gcc* --aid
2,安装 dhcp 源码包
首先确定系统上没安装 rpm 包
[root@localhost ]# rpm -q dhcp
package dhcp is not installed
[root@localhost pub]# tar -zxvf -C /usr/local/src/
[root@localhost pub]# cd /usr/local/src/
[root@localhost ]# ./configure
[root@localhost ]# make
[root@localhost ]# make install
[root@localhost ]# find / -name #查找 dhcp 主配置文件
/etc/
/usr/local/etc/
/usr/local/src/
[root@localhost ]# cp /usr/local/etc/ /etc/
3,dhcp 服务器的配置
[root@localhost ]# cat /etc/
#
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
#option domain-name "";
#option domain-name-servers , ;
#default-lease-time 600;
#max-lease-time 7200;
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
# Use this to send dhcp log messages to a different log file (you also
# have to hack to complete the redirection).
log-facility local7;
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
#subnet netmask {
#}
# This is a very basic subnet declaration.
#subnet netmask {
# range ;
# option routers , ;
#}
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
#subnet netmask {
# range dynamic-bootp ;
# option broadcast-address ;
# option routers ;
#}
# A slightly different configuration for an internal subnet.
subnet netmask {
range ;
option domain-name-servers ;
option domain-name "";
option routers ;
option subnet-mask ;
option broadcast-address ;
default-lease-time 21600;
max-lease-time 43200;
}
# Hosts which require special configuration options can be listed in
# host statements. If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific information
# will still come from the host declaration.
#host passacaglia {
# hardware ethernet 0:0:c0:5d:bd:95;
# filename "";
# server-name "";
#}
# Fixed IP addresses can also be specified for hosts. These addresses
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot using
# BOOTP or DHCP. Hosts for which no fixed address is specified can only
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp flag
# set.
host crazylinux {
hardware ethernet 00:0C:29:3B:68:25;
fixed-address ;
}
# You can declare a class of clients and then do address allocation
# based on that. The example below shows a case where all clients
# in a certain class get addresses on the subnet, and all
# other clients get addresses on the subnet.
class "foo" {
match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}
shared-network 224-29 {
subnet netmask {
option routers ;
}
subnet netmask {
option routers ;
}
pool {
allow members of "foo";
range ;
}
pool {
deny members of "foo";
range ;
}
}
4,服务启动
[root@localhost ]# touch /var/db/
[root@localhost ]# dhcpd
三,RH AS4 dhcp 中继代理
如图所示:
1、 如图所示,配置 dhcp server ip 地址,eth0:;网关为:
2、 安装 dhcp server ,用 linux RPM 包安装;
3、 编辑 /etc/ 内容如下:
ddns-update-style interim;
ignore client-updates;
option subnet-mask ;
option nis-domain "";
option domain-name "";
option time-offset -18000; # Eastern Standard Time
default-lease-time 259200;
max-lease-time 777600;
option domain-name-servers ,;
option broadcast-address ;
#网通 DNS 服务器地址
#下面配置超级作用域
Client DHCP Relay
DHCP Server
Eth1:
Eth0:
Eth0:
shared-nework mynet{
subnet netmask {
option routers
range dynamic-bootp ;
}
subnet netmask {
option routers ;
range dynamic-bootp ;
}
}
4、 编辑/etc/sysconfig/dhcpd ,DHCPDARGS=eth0
5、 启动 dhcp 服务,关闭防火墙。
6、 配置,dhcp relay ip 如下:eth0:,网关可不设;eth1: ,网关为:
7、 启动 ip_forward : echo “1”>/proc/sys/net/ipv4/ip_forward(我在 rhel4 中实验不需要此步骤)
或者在/etc/ 中查看 _forward = 1
#sysctl –p 立即生效
8、 安装 dhcp 软件
9、 编辑:/etc/sysconfig/dhcrelay
INTERFACES=”eth0 eth1”
DHCPSERVERS=””
10, 启动 dhcrelay:service dhcrelay start, 关闭防火墙
11, 用 XP 做 client 去自动获取 ip
12, 查看 dhcp server 租期数据库:/var/lib/dhcp/,可以看到已经租出的 ip 信息
13, 在 XP 可以获取到 ip。
四,使用 ether-wake 实现远程自动开机
1, 首先在客户机bios中设置网络唤醒功能
2, 连网
3, 记录客户机的MAC地址,关机
在dhcp主机中下载
开始编译:
#gcc -0 –Wall –o ether-wake
#./ether-wake 11:22:33:44:55:66 (唤醒主机)
#./ether-wake –u (查看帮助)
五,DHCP 原理
两台连接到互联网上的电脑相互之间通信,必须有各自的IP地址,但由于现在的IP地址资源有限,宽
带接入运营商不能做到给每个报装宽带的用户都能分配一个固定的IP地址(所谓固定IP就是即使在你不上
网的时候,别人也不能用这个IP地址,这个资源一直被你所独占),所以要采用DHCP方式对上网的用户进
行临时的地址分配。也就是你的电脑连上网,DHCP服务器才从地址池里临时分配一个IP地址给你,每次上
网分配的IP地址可能会不一样,这跟当时IP地址资源有关。当你下线的时候,DHCP服务器可能就会把这个
地址分配给之后上线的其他电脑。这样就可以有效节约IP地址,既保证了你的通信,又提高IP地址的使用
率。
要申请固定IP上网,资费高很多的,所以对于普通客户,一般都是通过DHCP动态分配地址的。如果你不是
商业用户,基本上是必须装DHCP才能上网的。
DHCP(Dynamic Host Configure Protocol),
是被广泛应用于TCP/IP协议的网络中的动态主机配置协议,使用dhcp协议,有很多简单、易用的优点,主
要表现在:网络管理员可以验证IP地址和其它配置参数,而不用去检查每个主机;DHCP不会同时租借相同
的IP地址给两台主机;DHCP管理员可以约束特定的计算机使用特定的IP地址;可以为每个DHCP作用域设
置很多选项;客户机在不同子网间移动时不需要重新设置IP地址。
dhcp协议的工作流程
1、DHCP客户机寻找DHCP服务器的阶段,可以称为发现阶段:DHCP客户机以广播方式(因为DHCP服务
器的IP地址对于客户机来说是未知的)发送DHCP discover发现信息来寻找DHCP服务器,即向地址
发送特定的广播信息。网络上每一台安装了TCP/IP协议的主机都会接收到这种广播信息,
但只有DHCP服务器才会做出响应。 udp 68 udp 67
2、DHCP服务器提供IP地址的阶段,可以称为提供阶段:在网络中接收到DHCP discover发现信息的DHCP
服务器都会做出响应,它从尚未出租的IP地址中挑选一个分配给DHCP客户机,向DHCP客户机发送一个包
含出租的IP地址和其他设置的DHCP offer提供信息
3、DHCP客户机选择某台DHCP服务器提供的IP地址的阶段,可以称为选择阶段:如果有多台DHCP服务器
向DHCP客户机发来的DHCP offer提供信息,则DHCP客户机只接受第一个收到的DHCP offer提供信息,然
后它就以广播方式回答一个DHCP request请求信息,该信息中包含向它所选定的DHCP服务器请求IP地址的
内容。之所以要以广播方式回答,是为了通知所有的DHCP服务器,他将选择某台DHCP服务器所提供的IP
地址。
4、DHCP服务器确认所提供的IP地址的阶段,可以称为确认阶段:当DHCP服务器收到DHCP客户机回答的
DHCP request请求信息之后,它便向DHCP客户机发送一个包含它所提供的IP地址和其他设置的DHCP ack
确认信息,告诉DHCP客户机可以使用它所提供的IP地址。然后DHCP客户机便将其TCP/IP协议与网卡绑定,
另外,除DHCP客户机选中的服务器外,其他的DHCP服务器都将收回曾提供的IP地址
5、以后DHCP客户机每次重新登录网络时,就不需要再发送DHCP discover发现信息了,而是直接发送包含
前一次所分配的IP地址的DHCP request请求信息。当DHCP服务器收到这一信息后,它会尝试让DHCP客户
机继续使用原来的IP地址,并回答一个DHCP ack确认信息。如果此IP地址已无法再分配给原来的DHCP客
户机使用时(比如此IP地址已分配给其它DHCP客户机使用),则DHCP服务器给DHCP客户机回答一个
DHCP nack否认信息。当原来的DHCP客户机收到此DHCP nack否认信息后,它就必须重新发送DHCP
discover发现信息来请求新的IP地址。
6、DHCP服务器向DHCP客户机出租的IP地址一般都有一个租借期限,期满后DHCP服务器便会收回出租的
IP地址。如果DHCP客户机要延长其IP租约,则必须更新其IP租约。DHCP客户机启动时和IP租约期限过一
半时,DHCP客户机都会自动向DHCP服务器发送更新其IP租约的信息。如果该客户机在租约规定的时间内
一直占用dhcp分配的地址并且在线,则该地址将一直被该客户机所使用。
由于时间匆忙,作者水平有限,难免有不足之处,敬请批评指正,谢谢大家:
--crazylinux