【Linux部署DHCP服务】

文章目录

  • 一、DHCP服务简介
    • [1.1 DHCP服务简介](#1.1 DHCP服务简介)
    • [1.2 DHCP的优点](#1.2 DHCP的优点)
    • [1.3 DHCP的工作过程](#1.3 DHCP的工作过程)
    • [1.4 DHCP客户机自动更新IP租约](#1.4 DHCP客户机自动更新IP租约)
  • 二、配置系统环境
    • [2.1 修改/etc/hosts](#2.1 修改/etc/hosts)
    • [2.2 关闭防火墙](#2.2 关闭防火墙)
    • [2.3 关闭selinux](#2.3 关闭selinux)
  • 三、服务端部署DHCP服务
    • [3.1 安装DHCP软件包](#3.1 安装DHCP软件包)
    • [3.2 配置文件目录](#3.2 配置文件目录)
    • [3.3 配置DHCP](#3.3 配置DHCP)
    • [3.4 启动DHCP服务](#3.4 启动DHCP服务)
    • [3.5 打开日志文件](#3.5 打开日志文件)
  • 四、配置客户端
    • [4.1 修改网络配置文件](#4.1 修改网络配置文件)
    • [4.2 重启网络服务](#4.2 重启网络服务)
    • [4.3 查看IP](#4.3 查看IP)
    • [4.4 查看网关](#4.4 查看网关)
    • [4.5 查看路由](#4.5 查看路由)
    • [4.6 查看dns](#4.6 查看dns)
  • 总结

提示:以下是本篇文章正文内容,下面案例可供参考

一、DHCP服务简介

1.1 DHCP服务简介

DHCP(Dynamic Host Configuration Protocol),动态主机配置协议,DHCP 协议主要是用来自动为局域网中的客户机分配 TCP/IP 信息的网络协议,并完成每台客户机的 TCP/IP 协议配置。当我们将局域网中客户机IP地址设置为动态获取方式时,DHCP服务器就会根据DHCP协议给客户机自动分配IP地址,使得客户机能够使用这个IP地址。

DHCP的前身是BOOTP协议(Bootstrap Protocol),BOOTP被创建出来为连接到网络中的设备自动分配地址,后来被DHCP取代了,DHCP比BOOTP更加复杂,功能更强大。所有的IP网络参数(包括IP地址、网关和DNS服务器地址等)都由DHCP服务器集中管理,并负责处理客户端的DHCP要求;而客户端则会使用从服务器分配的IP网络参数。

1.2 DHCP的优点

(1)减少管理员的工作量;

(2)避免IP冲突;

(3)减少收入错误的可能;

(4)能方便地更改网络的IP网段;

(5)移动计算机后不用重新配置网络信息;

(6)提高IP地址的利用率。

1.3 DHCP的工作过程


(1)请求DHCP服务器

客户端在局域网广播发送 DHCP discovery包,寻找DHCP服务器,即向255.255.255.255发送特定的广播信息,每一台安装了TCP/IP协议的主机都会有这个广播地址,但只有DHCP服务器才会做出回应。
(2)提供IP地址

服务器收到DHCP discovery包后,向客户机发送一个包含分配的IP地址和其它设置的 DHCP offer 包,目的告诉客户机,我能为你提供IP地址。
(3)选择IP地址

客户机收到DHCP offer提供信息包后,(如果客户机在一段时间内没有接收到DHCP服务器发出dhcp offer包,则会重新发送dhcp discover。如果广播区域内不止一台dhcp服务器,则由客户机决定使用哪个)客户机选择第一个接收到的提供信息包,以广播的方式再发送一个DHCP request请求信息包,客户机向服务器请求IP地址。
(4)IP地址提供确认

服务端收到客户机回应的DHCP request 请求信息包之后,便向客户机发送一个DHCP ack 确认信息包,告诉客户机可以使用它提供的IP地址。
(5)重新登录

以后DHCP客户机每次使用此IP地址时,就不需要再发送DHCP discovery包了,而是直接发送一个包含前一次所分配的IP地址的DHCP request请求信息。当DHCP服务器收到这一信息以后,它会继续让客户机使用此IP地址,并回答一个DHCP ack确认信息。如果此IP地址已无法再分配给原来的客户机使用时,则DHCP服务器给客户机回答一个DHCP nack否认信息。当原来的客户机收到此DHCP nack否认信息后,它就必须重新发送DHCP discovery寻找信息来请求新的IP地址。
(6)更新租约

DHCP服务器向客户机出租的IP地址都有一个租借期限,期满后DHCP服务器便会收回出租的IP地址。如果客户机要延长其IP租约,必须更新其IP租约。客户机启动时和IP租约期限过一半时,客户机都会自动向DHCP服务器发送更新其IP租约的信息。

1.4 DHCP客户机自动更新IP租约

客户机租约期限已过去50%,自动尝试更新租约;当期限过去87.5% 时,发出广播再次更新租约;若租约已经到期(100%),客户机必须立即停止使用当前的IP地址。然后客户机开始新的DHCP租约过程,尝试租用新的IP地址。

二、配置系统环境

2.1 修改/etc/hosts

c 复制代码
#进入配置文件
vim /etc/hosts
#添加DHCP服务器IP地址和主机名/别名
192.168.8.88   DHCP        

2.2 关闭防火墙

c 复制代码
systemctl stop firewalld             --临时关闭防火墙 
systemctl disable firewalld          --永久关闭防火墙

2.3 关闭selinux

c 复制代码
#获取当前selinux状态:
getenforce 
#备注:Enforcing为开启,Permissive为关闭
#临时关闭:
setenforce 0
永久关闭:
#  vim  /etc/selinux/config
    SELINUX=disabled                   --将enforcing改为disabled
#  reboot                                  --重启系统永久生效

三、服务端部署DHCP服务

3.1 安装DHCP软件包

c 复制代码
yum  -y  install  dhcp*    

3.2 配置文件目录

c 复制代码
配置文件目录:/etc/dhcp/dhcpd.conf
分给客户端IP的记录文件:/var/lib/dhcpd/dhcpd.leases

3.3 配置DHCP

把系统默认的样例复制到/etc/dhcp/dhcpd.conf文件里
根据您的网络地址 修改A slightly different configuration for an internal subnet.下对应的IP地址

c 复制代码
#  cp  /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example  /etc/dhcp/dhcpd.conf
cp:是否覆盖"/etc/dhcp/dhcpd.conf"? y           --输y覆盖原文件

配置vim /etc/dhcp/dhcpd.conf

c 复制代码
#
# Sample configuration file for ISC dhcpd
#

# option definitions common to all supported networks...
option domain-name "example.org";								 --DNS域名
option domain-name-servers ns1.example.org, ns2.example.org;		--DNS的域服务器

default-lease-time 600;									--默认的租约时间,秒为单位
max-lease-time 7200;									--最大的租约时间,秒为单位

# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;				--表示要不要把IP地址的更新情况告诉DNS服务器,默认是关闭的

# 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;					   --定义日志服务,可以在日志配置文件中查看具体日志位置,默认是:/var/log/boog.log,但是在/var/log/messages里面也会记录dhcp日志

# 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 {
#}

# 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;
}

# 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 192.168.32.0 netmask 255.255.255.0 {			--分配的网段及子网掩码,代表只在192.168.32.x这个C类网段里生效,子网掩码设为255.255.255.0
  range 192.168.32.10 192.168.32.50;					--代表给客户机分配可用的IP范围,起始到结束,尽量不要包含DHCP服务器的IP地址
  option domain-name-servers 192.168.32.130; 			--配置要分配给客户端的DNS地址,这里我们设为DHCP服务器的IP地址
  option domain-name "internal.example.org";
  option routers 192.168.32.130; 						--配置分配给客户机的路由网关,这里我们设为DHCP服务器的IP地址
  option broadcast-address 192.168.32.255;			--给客户机指定广播地址,配置192.168.32.x这个网段的广播地址
  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;
  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;
}

# 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.4 启动DHCP服务

c 复制代码
#  systemctl  restart  dhcpd.service        --重启DHCP服务
#  systemctl enable dhcpd.service        --设置为开机自启动
#  netstat -anlp|grep dhcpd             --查看dhcp服务端口为67

3.5 打开日志文件

客户端获取IP操作会记录在里面

c 复制代码
tail -f /var/log/messages

四、配置客户端

4.1 修改网络配置文件

c 复制代码
vim  /etc/sysconfig/network-scripts/ifcfg-ens33
BOOTPROTO=dhcp                                 --网卡指定方式改为dhcp

4.2 重启网络服务

c 复制代码
#重启网卡
systemctl  restart  network.service

#重启这个服务,每次重启网络服务时,系统都会重新去DHCP服务器上获取IP地址
systemctl  restart  NetworkManager

4.3 查看IP

c 复制代码
ifconfig

4.4 查看网关

c 复制代码
route -n | grep UG

4.5 查看路由

4.6 查看dns

c 复制代码
cat  /etc/resolv.conf

总结

使用虚拟机做该实验,常见的问题::

局域网里面出现多个 DHCP 服务器,比如vmware 里面没有把vmnet1 网段的dhcp 服务关闭,解决方法:

使用vmware 的Virtuan Network Editor 来设置vmnet1 的网段,关闭该网段的DHCP 服务(我实验主机的网络环境是 host-only/仅主机 模式,所以各位关闭的时候要注意下自己实验主机使用的是什么网络)

相关推荐
小鱼啊小鱼7 分钟前
【Linux】基于Exynos4412的U-Boot引导程序移植
linux
编程就是如此13 分钟前
CentOS 系统磁盘扩容并挂载到根目录(/)的详细步骤
linux·centos
一只小爪子27 分钟前
SQL 语句说明
运维·服务器·数据库·sql·mysql
normaling1 小时前
十,软件包管理
linux
·薯条大王1 小时前
Node.js介绍
服务器·node.js
派阿喵搞电子1 小时前
在Ubuntu下交叉编译 Qt 应用程序(完整步骤)
linux·运维·ubuntu
知北游天2 小时前
Linux:基础IO---软硬链接&&动静态库前置知识
linux·运维·服务器
云途行者2 小时前
GitLab 17.x 在 Ubuntu 24.04 上安装配置
linux·ubuntu·gitlab
汤姆和杰瑞在瑞士吃糯米粑粑2 小时前
【操作系统学习篇-Linux】进程
linux·运维·学习
清风~徐~来2 小时前
【Linux】进程创建、进程终止、进程等待
android·linux·运维