Centos7一键部署DHCP服务脚本

本文介绍了DHCP协议在大型网络中的作用,包括地址管理和共存策略,以及在CentOS7.9系统中使用VMwareWorkstationPro部署DHCP服务的过程,包括自定义网段、配置文件和脚本部署步骤。
摘要由CSDN通过智能技术生成

DHCP通常被应用在大型的局域网络环境中,主要作用是集中的管理、分配IP地址,使网络环境中的主机动态的获得IP地址、Gateway地址、DNS服务器地址等信息,并能够提升地址的使用率。

DHCP协议采用客户端/服务器模型,主机地址的动态分配任务由网络主机驱动。当DHCP服务器接收到来自网络主机申请地址的信息时,才会向网络主机发送相关的地址配置等信息,以实现网络主机地址信息的动态配置。DHCP具有以下功能:

1. 保证任何IP地址在同一时刻只能由一台DHCP客户机所使用。

2. DHCP应当可以给用户分配永久固定的IP地址。

3. DHCP应当可以同用其他方法获得IP地址的主机共存(如手工配置IP地址的主机)。

4. DHCP服务器应当向现有的BOOTP客户端提供服务。

一、系统环境 

平台:VMware Workstation Pro

centos 7.9系统

DHCP的配置文件中自定义的网段要与虚拟机的网卡是一致。

网络使用的是NAT模式 (后面windows设置要与此处设置的模式一致)

DHCP一键部署脚本:

#!/bin/bash
#function: DHCP部署
#author: 20230810 LINGH
#####root判断#####
if
  [  "$USER"  != "root"   ]
then
   echo "错误:非root用户,权限不足!"
  exit  0
fi
############防火墙与高级权限##########
systemctl stop firewalld && systemctl disable firewalld  && echo "防火墙已经关闭"
sed -i 's/SELINUX=*/SELINUX=disabled/g'  /etc/selinux/config  && echo "关闭selinux"
################安装DHCP###############
yum -y install dhcp*
if
  [ $? =  0 ];then
  echo  "依赖包安装成功"
 else
   echo  "依赖包安装失败"
   exit 1
 fi
sleep 3
###########配置服务##########
sed -i "s/BOOTPROTO=dhcp/BOOTPROTO=static/g"    /etc/sysconfig/network-scripts/ifcfg-ens33
read -ep "请输入本机ip:" IPADDR
read -ep "请输入网关IP:" GATEWAY
read -ep "请输入分配地址池开始IP:" A
read -ep "请输入分配地址池结束IP:" B
cat >> /etc/sysconfig/network-scripts/ifcfg-ens33 << EOF
IPADDR=$IPADDR           #本机ip
NETMASK=255.255.255.0         #子网掩码
GATEWAY=$GATEWAY           #网关IP
DNS=$GATEWAY         #DNS服务器 
EOF
##########重启网络##########
#systemctl restart network
ifdown ens33  && ifup ens33
#########获取ip网络位#############
IP=$(hostname -I | awk '{print $1}' | awk -F '.' '{print $1"."$2"."$3}')
#############修改DHCP配置###########
cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak
cat > /etc/dhcp/dhcpd.conf  << EOF
# 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;

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 syslog.conf 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 $IP.0 netmask 255.255.255.0 {          # 定义网络地址和子网掩码
range $A $B;                  # 指定IP地址池的范围
  option domain-name-servers $GATEWAY;           # DNS服务器(自建的DNS或所在地区的DNS)
  option domain-name "zrs.com";                       # 域(比如访问www,就会自动补齐www.zrs.com)
  option routers $GATEWAY;                       # 网关IP
  option broadcast-address $IP.255;           # 广播地址
  default-lease-time 600;
  max-lease-time 7200;
}

# 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 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";
  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;
  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;
  }
}
EOF
##############启动DHCP服务##########
systemctl start dhcpd  &&  systemctl enable dhcpd  &&  systemctl status dhcpd
if
  [ $? =  0 ];then
  echo  "启动成功,请关闭虚拟编辑器的DHCP"
  echo  "DHCP服务ip地址分配为:" $A $B
 else
   echo  "启动失败"
   exit 1
 fi
sleep 3

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值