您的位置:首页 > 运维架构 > Linux

Linux-DHCP服务的配置

2010-04-13 13:19 381 查看
DHCP服务的配置。
DHCP简介:
DHCP动态主机配置协议,在一个小型局域网里可以通过手工指定ip地址的方式,但如有大量的主机,如果还是手工指定,那是一个很大的工作量,而且也容易出错,这时,就可以通过配置一台DHCP服务器来解决,为每台主机分配一个ip地址。DHCP作用域是指DHCP服务器可分配给DHCP客户端的IP地址范围,一个DHCP服务器至少要一个作用域,如有多个作用域,作用域之间的IP地址不能重叠。
DHCP的工作原理:
DHCP工作过程分为四个步骤:
第一步(DHCPDISCOVER):客户端发送广播查找可以给自己提供IP地址的DHCP服务器,
第二步(DHCPOFFER):DHCP服务器发送广播提供一个可用的IP地址,并在地址池中将该地址打上标记,以防再次分配。
第三步(DHCPREQUEST):客户端收到广播后再次发送一个广播请求该地址,如果有多台DHCP响应,则第一个收到的优先。
第四步(DHCPACK):DHCP服务器收到广播后再发送一个广播,确认该地址分配给这台主机使用,并在地址池中将该地址打上标记,以防再次分配。
DHCP的基本配置:
第一步:安装软件:

[root@localhost ~]# rpm -ivh /misc/cd/Server/dhcp-3.0.5-18.el5.i386.rpm

第二步:编辑配置文件:
[root@localhost ~]# vi /etc/dhcpd.conf
# see /usr/share/doc/dhcp*/dhcpd.conf.sample
当进入到配置文件里面时,这里是空的,有如上一行的提示,说可以参考这个文件来编写配置文件,那就复制一份再编辑。

[root@localhost ~]# cp -rvf /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
[root@localhost ~]# vi /etc/dhcpd.conf

ddns-update-style interim; //配置DHCP和DNS为互动更新模式,
ignore client-updates; //忽略客户端的更新。

subnet 192.168.100.0 netmask 255.255.255.0 { //定义作用域

option routers 192.168.100.1; //默认路由
option subnet-mask 255.255.255.0; //子网掩码

option nis-domain "rhel.com"; //NIS域名
option domain-name "rhel.com"; //DNS域名
option domain-name-servers 192.168.100.10; //DNS服务器地址

option time-offset -18000; # Eastern Standard Time //为客户端设置与格林威治的时间偏移
option ntp-servers 192.168.1.1; //网络时间服务器地址
option netbios-name-servers 192.168.1.1; //指定WINS服务器地址

range dynamic-bootp 192.168.100.128 192.168.100.254; //动态分配的地址范围
default-lease-time 21600; //默认租约时间
max-lease-time 43200; //最大租约时间

host abc { //指定特定主机
hardware ethernet 00:0C:29:FD:44:C8; //硬件地址
fixed-address 192.168.100.30; //IP地址
}
}

如有多个要绑定的主机时可以把所有主机信息用group{ }包括起来,这样更方便管理。格式如下 :
group {
……这里不可以添加一些佤全局选项,这样就不用在每个里面写了。
host abc {
……
……
}
host xyz {
……
……
}
}
[root@localhost ~]# service dhcpd restart

测试:

在linux下:
首先让地址从DHCP获取:
[root@localhost ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=dhcp

[root@localhost ~]# service network restart //重启一下网络服务,这里不能使用ifdown和ifup,因为要配置路由信息。
[root@localhost ~]# ifconfig
eth0 Link encap:Ethernet HWaddr 00:0C:29:FD:44:C8
inet addr:192.168.100.30 Bcast:192.168.100.255 Mask:255.255.255.0

这就是绑定的IP地址。
[root@localhost ~]# cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search rhel.com
nameserver 192.168.100.10
这里的DNS服务器地址也获取到了。
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 0 0 0 eth0
0.0.0.0 192.168.100.1 0.0.0.0 UG 0 0 0 eth0
默认路由也获取到了。查看一下端口:
[root@localhost ~]# netstat -tlunp | grep 68
udp 0 0 0.0.0.0:68 0.0.0.0 16150/dhclient

查看客户端的租约文件:
lease {
interface "eth0";
fixed-address 192.168.100.30; //固定地址
option subnet-mask 255.255.255.0; //子网掩码
option time-offset -18000; //时间偏移
option routers 192.168.100.1; //默认路由
option dhcp-lease-time 21600; //租约时间
option dhcp-message-type 5;
option domain-name-servers 192.168.100.10;
option dhcp-server-identifier 192.168.100.20;
option nis-domain "rhel.com";
option domain-name "rhel.com";
renew 5 2010/4/9 09:01:40;
rebind 5 2010/4/9 11:34:53;
expire 5 2010/4/9 12:19:53;
}

windows下:

把IP地址设成自动获取。
C:\Documents and Settings\Administrator>ipconfig /all
Connection-specific DNS Suffix . : rhel.com
Physical Address. . . . . . . . . : 00-0C-29-D3-C6-B3
Dhcp Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 192.168.100.253
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.100.1
DHCP Server . . . . . . . . . . . : 192.168.100.20
DNS Servers . . . . . . . . . . . : 192.168.100.10
Lease Obtained. . . . . . . . . . : 2010年4月9日 14:44:12
Lease Expires . . . . . . . . . . : 2010年4月9日 20:44:12

基本配置完成。

DHCP超级作用域。
比如现在有一个C类的作用域,但随着网络的扩大,现在一个C类网络地址已经不够用了,那别的主机怎么得到IP地址呢?经过测试,直接再添加一个作用域发现第二个作用域的地址申请不到,那解决的办法呢?如下:
shared-network supper {

}
用这个把所有的作用域括起来,就可以实现了。shared-network 后面的supper是自己定义的超级作用域名称,可以随便。很简单吧?

DHCP保留地址。
在DHCP里为某个主机绑定地址已经知道了,但怎么去保留某一段地址来留着备用呢?如有这样一个要求:DHCP提供192.168.100.0 /24这上网段的地址,但现在想192.168.100.2-192.168.100.99,192.168.100.101-192.168.100.254这些地址是不能分出去的,要保留使用。(这是为了实验,这样就只有两个IP地址能分配了,能够证明。)实现如下:

range dynamic-bootp 192.168.100.1 192.168.100.1;
range dynamic-bootp 192.168.100.100 192.168.100.100;
其实原理很简单,就是把可以分出去的地址写出来,没写的自然就不会被分出去了,这样就实现了地址的保留。
DHCP中继代理。
如果有两个子网,中间隔着一台路由器,而只有一台DHCP服务器,因为主机向DHCP服务器申请地址时使用的是广播,路由器又隔离广播,那另一个子网的主机怎么获得IP地址呢?有一种支持RFC1542的路由器可以实现,但不是这种路由器那就没办法了吗?当然不是,这时,就需要用到DHCP中继代理了。

首先配置DHCP服务器:

[root@rhel ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.10.10
NETMASK=255.255.255.0
GATEWAY=192.168.10.1 //这个网关一定要。

[root@rhel ~]# service network restart
安装DHCP软件:
[root@rhel ~]# rpm -ivh /misc/cd/Server/dhcp-3.0.5-18.el5.i386.rpm
编辑配置文件:
[root@rhel ~]# vi /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;

option time-offset -18000;
default-lease-time 21600;
max-lease-time 43200;
shared-network supper {
subnet 192.168.10.0 netmask 255.255.255.0 {
option routers 192.168.10.1; //必需配置网关地址。
option subnet-mask 255.255.255.0;
range dynamic-bootp 192.168.10.2 192.168.10.2;
}

subnet 192.168.20.0 netmask 255.255.255.0 {
option routers 192.168.20.1;
option subnet-mask 255.255.255.0;
range dynamic-bootp 192.168.20.2 192.168.20.3;
}
}

[root@rhel ~]# service dhcpd restart

接下来配置DHCP中继代理服务器:

DHCP中继代理服务器需要有两块网卡。eth0用于和DHCP服务器相连,eth1用于和内网相连。
[root@rhel3 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.10.1 //这里的地址是DHCP服务器的网关地址。
NETMASK=255.255.255.0

[root@rhel3 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
BOOTPROTO=static
IPADDR=192.168.20.1 //这里的地址是第二个作用域的地址。
NETMASK=255.255.255.0

[root@rhel3 ~]# service network restart

安装DHCP软件(必需):
[root@rhel3 ~]# rpm -ivh /misc/cd/Server/dhcp-3.0.5-18.el5.i386.rpm
编辑这个文件,开启转发功能:
[root@rhel3 ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward = 1 //把这里改为1,默认为0。
执行为条命令使其生效:
[root@rhel3 ~]# sysctl -p
[root@rhel3 ~]# dhcrelay 192.168.10.10 //后面是DHCP的IP地址。

好了,配置完成,测试。

DHCP常用命令。
ipconfig /release //windows下释放IP地址
ipconfig /renew //windows下重新获取IP地址
service network restart //linux重启网络服务

DHCP综合配置实例。
目标:
当网络中的DHCP服务器出现问题,另一台服务器可以继续承担分配ip地址,主服务器的地址为192.168.10.10,辅服务器的地址为192.168.10.11,分配的地址范围是192.168.10.100-192.168.10.254,默认租约为1天,最大租约为2天。默认路由为192.168.10.1,DNS服务器地址为192.168.20.20,DNS域名为rehl.com。WINS服务器地址为192.168.20.21。
其中,192.168.10.201保留给计算机名为rhel3,MAC地址为00:0C:29:2B:5B:2F的主机使用,并配置DNS服务器地址为219.146.0.130,192.168.20.20。192.168.10.202保留给计算机名为rhel4,MAC地址为00-0C-29-D3-C6-B3的主机使用,并配置DNS服务器地址为219.146.0.130,192.168.20.20。

192.168.10.150-192.168.10.160地址保留备用。

配置过程:

主服务器配置:
[root@rhel ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=none
IPADDR=192.168.10.10
NETMASK=192.168.10.11

[root@rhel ~]# service network restart
[root@rhel ~]# rpm -ivh /misc/cd/Server/dhcp-3.0.5-18.el5.i386.rpm
[root@rhel ~]# vi /etc/dhcpd.conf
ddns-update-style interim;
ignore client-updates;
subnet 192.168.10.0 netmask 255.255.255.0 {
default-lease-time 86400;
max-lease-time 172800;
option routers 192.168.10.1;
option domain-name "rhel.com";
option domain-name-servers 192.168.20.20;
option netbios-name-servers 192.168.20.21;
range dynamic-bootp 192.168.10.100 192.168.10.140;
range dynamic-bootp 192.168.10.161 192.168.10.200;
group {
option domain-name-servers 219.146.0.130,192.168.20.20;
host rhel3 {
hardware ethernet 00:0C:29:2B:5B:2F;
fixed-address 192.168.10.201;
}
host rhel4 {
hardware ethernet 00:0C:29:D3:C6:B3;
fixed-address 192.168.20.202;
}
}
}

[root@rhel ~]# service dhcpd restart

辅服务器的配置:
[root@rhel2 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
BOOTPROTO=static
IPADDR=192.168.10.11
NETMASK=255.255.255.0

[root@rhel2 ~]# service network restart
[root@rhel2 ~]# rpm -ivh /misc/cd/Server/dhcp-3.0.5-18.el5.i386.rpm

[root@rhel2 ~]# vi /etc/dhcpd.conf

ddns-update-style interim;
ignore client-updates;
subnet 192.168.10.0 netmask 255.255.255.0 {
default-lease-time 86400;
max-lease-time 172800;
option routers 192.168.10.1;
option domain-name "rhel.com";
option domain-name-servers 192.168.20.20;
option netbios-name-servers 192.168.20.21;
range dynamic-bootp 192.168.10.203 192.168.10.254; //除了这里不一样,其他都都一样。
group {
option domain-name-servers 219.146.0.130,192.168.20.20;
host rhel3 {
hardware ethernet 00:0C:29:2B:5B:2F;
fixed-address 192.168.10.201;
}
host rhel4 {
hardware ethernet 00:0C:29:D3:C6:B3;
fixed-address 192.168.20.202;
}
}
}

[root@rhel2 ~]# service dhcpd restart

实验完成。测试。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息