Linux网络管理18:DHCP服务器

​ 对于TCP/IP网络,要接入到本地网络中,每台计算机要有一个唯一的ip地址。若每台计算机的ip地址都需要管理员来分配、设置,在一个规模较大的网络中,任务极其繁重。

​ 在TCP/IP协议簇中提供了DHCP应用层协议。通过DHCP,可以对每台计算机的ip地址进行动态分配,从而降低网络管理复杂性。

一、DHCP简介

​ DHCP( Dynamic Host Configuration Protocol 动态主机配置协议),是TCP/IP协议簇中的一种,主要用来给网络中的计算机动态分配ip地址。这些被分配的ip地址都是在DHCP服务器中预先保留的地址集。
DHCP在管理网络配置方面的作用

  • 网络规模较大,使用DHCP可减少管理员工作量;

  • 对于移动pc,使用环境变动,造成ip地址变动,使用dhcp,自动接入。

    DHCP分为两部分服务器端客户端

    服务器端负责集中管理可动态分配的ip地址集,并负责处理客户端的dhcp请求,给客户端分配ip地址。

    客户端负责向服务器端发出请求ip地址的数据包,并获取服务器分配的ip地址,为客户端设置分配的ip地址。

    注:dhcp服务器在很多设备中已经内置。如:家用的光猫,路由器等

二、DHCP工作流程

​ 使用dhcp时,网络上必须有一台dhcp服务器,而其他计算机执行dhcp客户端。

​ 当dhcp客户端程序发出一个信息,请求一个动态ip地址时,dhcp服务器会从目

前配置中设置的ip地址集中提供一个可供使用的ip地址和子网掩码给客户端。工作流

程如下:

  1. 由于客户端最初不知道dhcp服务器的ip地址,因此设置使用dhcp的客户端将以广播方式向网络中发送dhcp discover信息,用来查找网络中是否存在dhcp服务器。这样网络中的每一台计算机都可收到该广播信息,但只有dhcp服务器才会做出响应。
  2. dhcp服务器 收到客户端发送的dhcp discover信息,将从dhcp服务器中还没有分配的ip地址中选择一个,并将ip地址、子网掩码、网关地址等信息发送给申请的客户端计算机。
  3. 由于网络中可能存在多台dhcp服务器,而这些dhcp服务器都可能向申请ip地址的客户端返回一个ip地址及相关信息,所以,需要设置一种选择方案,通常的规则是dhcp客户端接受第一个收到的ip地址分配信息。
  4. 客户端以广播方式发送出一个dhcp request信息,该信息中包含向它所选定的dhcp服务器请求ip地址的内容。这里使用广播方式来应答,是为了通知其他其他dhcp服务器,该客户端已选择了某台dhcp服务器所提供的IP地址。
  5. 当dhcp服务器收到dhcp客户端广播的dhcp request请求后,被使用的dhcp服务器将向dhcp客户端发送一个包含其所提供的ip地址和其他设置的dhcp ack信息,告诉dhcp客户端可以使用使用该ip地址,其他的dhcp服务器则收回曾提供的ip地址。而dhcp客户端便将收到的ip地址与客户端的网卡绑定。

通过以上步骤,完成了客户端动态分配ip地址的过程。

dhcp服务器向dhcp客户端分配ip地址称为出租,通常都设置为有租借期限,当期满后,dhcp服务器便会收回该ip地址。如果dhcp客户端要延长租约,则必须更新租约。
dhcp客户端启动和租约期限过半时,会自动向dhcp服务器发送更新租约的信息。

三、DHCP服务器安装

1.安装

首先查看是否安装dhcp:rpm -qa dhcp

若未安装,可使用yum命令:yum install dhcp

2.了解配置文件

​ 1.查看配置文件命令:cat /etc/dhcp/dhcpd.conf

#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
#   see 'man 5 dhcpd.conf'
#
You have new mail in /var/spool/mail/root

上面,第三行是可以选择的dhcp版本,可以将这个复制到dhcpd文件,并进行相应的配置。

​ 2.将/usr/share/doc/dhcp*/dhcpd.conf.sample的文件复制到当前dhcpd.conf

cp -p /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf

​ 3.再次查看配置文件:cat /etc/dhcp/dhcpd.conf

# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";   #指定网络的域名
option domain-name-servers ns1.example.org, ns2.example.org;  #指定域名解析服务器(DNS)的ip地址

default-lease-time 600; #默认租约时间,单位s
max-lease-time 7200;  #最大租约时间,单位s。过期续约,续约直接发送request包即可

# 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 syslog.conf to complete the redirection).
log-facility local7;  #日志设备类型为local7.一般日志设备类型包括mail、crontab。遇到此选项可以找到服务的日志记录路径

# No service will be given on this subnet, but declaring it helps the 
# DHCP server to understand the network topology.

subnet 10.152.187.0 netmask 255.255.255.0 {
}  #指定分配网段的ip地址和子网掩码,括号内是局部配置

# This is a very basic subnet declaration.

subnet 10.254.239.0 netmask 255.255.255.224 {
  range 10.254.239.10 10.254.239.20;#可以使用的地址池范围
  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
} #option routers 可以指定网关

# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.

subnet 10.254.239.32 netmask 255.255.255.224 {
  range dynamic-bootp 10.254.239.40 10.254.239.60;
  option broadcast-address 10.254.239.31;
  option routers rtr-239-32-1.example.org;
}

# A slightly different configuration for an internal subnet.
subnet 10.5.5.0 netmask 255.255.255.224 {
  range 10.5.5.26 10.5.5.30;
  option domain-name-servers ns1.internal.example.org;# 该网段的域名,可以省略
  option domain-name "internal.example.org";# 网段DNS
  option routers 10.5.5.1;# 指定网关
  option broadcast-address 10.5.5.31;# 指定广播地址
  default-lease-time 600;# 租约时间
  max-lease-time 7200;# 最大租约时间
}

# 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;# 主机的mac地址
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}

# 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 fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;# 分配的固定ip地址
}

# 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 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.

class "foo" {
  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}

shared-network 224-29 {
  subnet 10.17.224.0 netmask 255.255.255.0 {
    option routers rtr-224.example.org;
  }
  subnet 10.0.29.0 netmask 255.255.255.0 {
    option routers rtr-29.example.org;
  }
  pool {
    allow members of "foo";
    range 10.17.224.10 10.17.224.250;
  }
  pool {
    deny members of "foo";
    range 10.0.29.10 10.0.29.230;
  }
}

3.配置dhcp服务器

配置实例
局域网络,在该网络中使用dhcp服务器,要对dhcp服务器进行配置设置:

  • 网络中ip网段为192.168.232.0(虚拟机上的ipv4地址)
  • 子网掩码:255.255.255.0
  • 动态分配的ip地池区间:192.168.232.180~192.168.232.190
  • dns服务器的ip地址为:114.114.114.114
  • 默认网关为:192.168.232.1
  • 有一台计算机的ip地址有特殊要求,设置ip地址为192.168.0.199

根据以上条件,修改配置文件。

注:以上的配置一定要与虚拟机的ip地址设置。

subnet 192.168.232.0(这个地方,后面的数字必须为0) netmask 255.255.255.0{
  range 192.168.232.60 192.168.232.80;(这是整个范围)
  option routers 192.168.232.1;(最后的数字必须为1)
  default-lease-time 600;
  max-lease-time 7200;

host mm{
  hardware ethernet 00:8C:29:AB:B2:6H;
  fixed-address 192.168.232.70;
}
}

注:可以使用sudo dhcpd -t命令查看是否出现问题。

4.创建两台克隆虚拟机进行配置

创建完之后,可以使用命令:cat /etc/sysconfig/network-scripts/ifcfg-eth0查看每个虚拟机的网络配置文件,然后使用ifconfig查看每个虚拟机的ip地址即可发现规律。

四、客户租约文件

​ 运行dhcp服务器程序时还需要一个名为dhcpd.leases的客户租约文件,该文件保存所有已经分配出去的ip地址。该文件位于 /var/lib/dhcpd/dhcpd.leases。
​ 若不是通过rpm安装的dhcp服务器程序,则必须创建一个空文件dhcpd.leases。
​ 若当前网络中还未使用dhcp服务, dhcpd.leases文件是无内容的,使用一段时间之后,可查看文件内容。

​ 查看命令:cat /var/lib/dhcpd/dhcpd.leases

dhcpd.leases文件格式:leases address{statement}

​ address表示分配出去的ip地址, statement保存该ip地址出租的时间、使用该ip地址的硬件mac地址信息等。

# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.1.1-P1

server-duid "\000\001\000\001&_\336\003\000\014)\273\262n";
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值