Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1146279
  • 博文数量: 309
  • 博客积分: 6093
  • 博客等级: 准将
  • 技术积分: 3038
  • 用 户 组: 普通用户
  • 注册时间: 2008-02-03 17:14
个人简介

linux学习记录

文章分类

全部博文(309)

文章存档

2014年(2)

2012年(37)

2011年(41)

2010年(87)

2009年(54)

2008年(88)

分类:

2010-08-25 15:31:23

                      RHCE学习笔记

 

下面是关于linux下面配置DHCP服务的讨论,

 

DHCP介绍

DHCP的全名为Dynamic Host Configuration Protocol(动态主机配置协议)

其目的是给DHCP客户端分发网络参数的,当在网络中有一台DHCP服务器,那么客户端就会自动的到DHCP服务器上获取网络参数。

 

DHCP的工作原理

关于DHCP工作过程中的数据包,

DHCPDISCOVER      DHCP发现包

DHCPOFFER             DHCP提供包

DHCPREQUEST        DHCP请求包

DHCPACK                DHCP确认包

简单概述:

首先,DHCP客户端会发送一个全网广播的DHCP发现包,寻找DHCP服务器。

当网络中的DHCP服务器收到DHCP客户端的请求,就会在自己的地址池中随即分发一个ip地址给DHCP客户端。当DHCP客户端确定使用这个ip地址的时候,就会发送一个DHCP的确认包给DHCP服务器。

 

DHCP的地址租约

DHCP的地址租约有两种方式:

限定租期

DHCP客户端向DHCP服务器租用到IP地址,客户端可以使用该IP地址一段时间,当租用期快到了的时候,客户端必须想DHCP服务器提出续约请求,请求成功后,可以继续使用该IP地址,如果客户端没有续约或续约不成功,服务器就会将该IP地址收回,分发给其他DHCP客户端使用。

永久租用

DHCP客户端的MAC地址与IP地址绑定,那么绑定的DHCP客户端就可以永久的使用这个IP地址,其他计算机自动获取不到的。

 

下面是关于DHCP这个服务的属性,

DHCP的相关软件包

Dhcp

DHCP的守护进程

/usr/sbin/dhcpd

DHCP的脚本

/etc/init.d/dhcpd

DHCP的端口

67(bootps)   68(bootpc)

DHCP的配置文件

/etc/dhcp.conf    /var/lib/dhcpd/dhcpd.leases

 

下面来具体搭建DHCP服务

第一步,安装软件包

[root@localhost ~]#

[root@localhost ~]# yum -y install dhcp

Loaded plugins: rhnplugin, security

This system is not registered with RHN.

RHN support will be disabled.

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package dhcp.i386 12:3.0.5-21.el5 set to be updated

--> Finished Dependency Resolution

 

Dependencies Resolved

 

================================================================================

 Package        Arch           Version                   Repository        Size

================================================================================

Installing:

 dhcp           i386           12:3.0.5-21.el5           Server           866 k

 

Transaction Summary

================================================================================

Install          1 Package(s)         

Update        0 Package(s)        

Remove       0 Package(s)        

 

Total download size: 866 k

Downloading Packages:

dhcp-3.0.5-21.el5.i386.rpm                               | 866 kB     00:00    

Running rpm_check_debug

Running Transaction Test

Finished Transaction Test

Transaction Test Succeeded

Running Transaction

  Installing     : dhcp                                                     1/1

 

Installed:

  dhcp.i386 12:3.0.5-21.el5                                                    

 

Complete!

[root@localhost ~]#

DHCP的软件包就安装成功了,

 

第二步,复制模板文件

首先我们来查看一下DHCP的配置文件,

[root@localhost ~]# vim /etc/dhcpd.conf

#

# DHCP Server Configuration file.

#   see /usr/share/doc/dhcp*/dhcpd.conf.sample 

#

~  

这个文件提示我们去在/usr/share/doc/dhcp*/dhcpd.conf.saple是个模板文件。

现在去查看一下这个文件,

[root@localhost ~]#

[root@localhost ~]# cd /usr/share/doc/dhcp-3.0.5/

[root@localhost dhcp-3.0.5]#

[root@localhost dhcp-3.0.5]# ls | grep dhcpd.conf.sample

dhcpd.conf.sample

[root@localhost dhcp-3.0.5]#

现在把这个文件复制成dhcpd.conf文件。

[root@localhost dhcp-3.0.5]#

[root@localhost dhcp-3.0.5]# cp dhcpd.conf.sample /etc/dhcpd.conf

cp: overwrite `/etc/dhcpd.conf'? y

[root@localhost dhcp-3.0.5]#

 

第三步,编辑DHCP的配置文件

ddns-update-style interim;

ignore client-updates;

 

subnet 192.168.0.0 netmask 255.255.255.0 {

 

# --- default gateway

        option routers                         192.168.0.254;

        option subnet-mask                 255.255.255.0;

 

        option nis-domain                      " domain.org";

        option domain-name                   " example.com";

        option domain-name-servers        192.168.0.254;

 

        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 192.168.0.100 192.168.0.250;

        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;

                fixed-address 207.175.42.254;

        }

}

关于语法参数的解释,

ddns-update-style interim;

定义所支持的DNS的动态更新类型(必选)

ignore client-updates;

忽略客户端更新DNS记录

subnet 192.168.0.0 netmask 255.255.255.0

定义作用域是192.168.0.0/24

option routers  192.168.0.254

为客户端定义网关地址

option subnet-mask

网关的子网掩码

option nis-domain   "domain.org";

指定Nis的域名,没有定义

option domain-name  "example.com"

指定DNS的域名

option domain-name-servers  192.168.0.254;

指定DNS Serverip地址

option time-offset    -18000; # Eastern Standard Time

这个是时区的设置

range 192.168.0.100  192.168.0.250;

定义DHCP的地址池

default-lease-time   21600;

默认最小的租约期是21600S

max-lease-time 43200;

最大的租约期是43200S

好了,DHCP的服务器基本就配置成功了,可以提供工作了。

现在我们重启下服务,

[root@localhost ~]#

[root@localhost ~]# service dhcpd restart

Shutting down dhcpd:                                       [  OK  ]

Starting dhcpd:                                                 [  OK  ]

[root@localhost ~]#

服务启动成功,

现在我们到DHCP客户端上面去测试下,

[root@localhost ~]# vim /etc/sysconfig/network-scripts/ifcfg-eth0

# Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE]

DEVICE=eth0

BOOTPROTO=dhcp

HWADDR=00:0C:29:75:91:EC

ONBOOT=yes

设置成自动获取

[root@localhost ~]#

[root@localhost ~]# service network restart

Shutting down interface eth0:                                [  OK  ]

Shutting down loopback interface:                        [  OK  ]

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0: 

Determining IP information for eth0... done.

                                                                           [  OK  ]

[root@localhost ~]#

[root@localhost ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:75:91:EC 

          inet addr:192.168.0.250  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe75:91ec/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:12703 errors:0 dropped:0 overruns:0 frame:0

          TX packets:10237 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:7179801 (6.8 MiB)  TX bytes:1122632 (1.0 MiB)

          Interrupt:67 Base address:0x2024

 

[root@localhost ~]#

可以看到,DHCP客户端就获取到了一个IP地址。

现在我们来查看一下日志的信息

[root@localhost ~]# tail -f /var/log/messages

Mar 13 14:24:18 localhost dhcpd: DHCPDISCOVER from 00:0c:29:75:91:ec via eth0

Mar 13 14:24:18 localhost dhcpd: DHCPREQUEST for 192.168.0.161 (192.168.0.1) from 00:0c:29:75:91:ec via eth0

Mar 13 14:24:18 localhost dhcpd: DHCPACK on 192.168.0.161 to 00:0c:29:75:91:ec via eth0

Mar 13 14:24:19 localhost dhcpd: DHCPOFFER on 192.168.0.250 to 00:0c:29:75:91:ec via eth0

日志信息里面很清楚的显示了整个DHCP分配IP地址的过程。

现在我们去地址的租约文件里面看看,

[root@localhost dhcpd]# cat dhcpd.leases

# 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 dhcpd.leases(5) manual page.

# This lease file was written by isc-dhcp-V3.0.5-RedHat

lease 192.168.0.250 {

  starts 6 2010/03/13 11:33:33;

  ends 6 2010/03/13 17:33:33;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:75:91:ec;

}

这个里面就有192.168.0.250的租约的相关信息,

刚才是限定租期,现在我们来做永久租期。

永久租期主要是通过MAC地址和IP地址进行绑定,从而实现永久租期。

/etc/dhcpd.conf文件里面就可以定义。

现在我们给192.168.0.10这个IP地址做绑定,

        host station10 {

                hardware ethernet 00:0C:29:75:91:EC;

                fixed-address 192.168.0.10;

        }

语法参数解释

hardware ethernet 00:0C:29:75:91:EC;

这个是代表需要绑定计算机的MAC地址

fixed-address 192.168.0.10;

这个是需要保留的IP地址

注意,这个保留的IP地址不可以在定义的地址池范围内,否则没有意义。

设置完成,现在重启下DHCP服务,

[root@localhost ~]#

[root@localhost ~]# service dhcpd restart

Shutting down dhcpd:                                        [  OK  ]

Starting dhcpd:                                                  [  OK  ]

[root@localhost ~]#

服务启动成功,

[root@localhost ~]#

[root@localhost ~]# service network restart

Shutting down interface eth0:                                [  OK  ]

Shutting down loopback interface:                        [  OK  ]

Bringing up loopback interface:                            [  OK  ]

Bringing up interface eth0: 

Determining IP information for eth0... done.

                                                                           [  OK  ]

[root@localhost ~]# ifconfig eth0

eth0      Link encap:Ethernet  HWaddr 00:0C:29:75:91:EC 

          inet addr:192.168.0.10  Bcast:192.168.0.255  Mask:255.255.255.0

          inet6 addr: fe80::20c:29ff:fe75:91ec/64 Scope:Link

          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1

          RX packets:12714 errors:0 dropped:0 overruns:0 frame:0

          TX packets:10297 errors:0 dropped:0 overruns:0 carrier:0

          collisions:0 txqueuelen:1000

          RX bytes:7182082 (6.8 MiB)  TX bytes:1139723 (1.0 MiB)

          Interrupt:67 Base address:0x2024

 

[root@localhost ~]#

OK,可以看到,IP地址就变成了192.168.0.10了。

 

当计算机的IP地址不能够及时释放出去,就必须先停止DHCP服务,然后删除/var/lib/dhcpd目录下面的租约文件,然后启动DHCP服务,那么DHCP服务就可以正常工作了。

 

linux下面DHCP的基本配置的讨论就是这么多了。

阅读(523) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~