dhcp服务器Ubuntu部署

dhcp服务器

    • 环境准备
    • 安装部署
      • centos测试
    • Ubuntu脚本部署

服务端部署:Ubuntu22
客户端部署:centos

环境准备

勾掉dhcp
dhcp服务器Ubuntu部署_第1张图片
网卡配置静态

network:
  ethernets:
    ens33:
      dhcp4: no
      addresses:
        - 192.168.200.160/24
      routes:
        - to: default
          via: 192.168.200.2
      nameservers:
        addresses:
          - 114.114.114.114
          - 8.8.8.8
    ens38:
      dhcp4: no
      addresses:
        - 192.168.200.170/24
      routes:
        - to: 192.168.200.0/24
          via: 192.168.200.2
      nameservers:
        addresses:
          - 114.114.114.114
          - 8.8.8.8
  version: 2

安装部署

安装软件包

apt install -y isc-dhcp-server

修改配置文件

vim /etc/dhcp/dhcpd.conf

参数解释,配置文件中自带,解开注释即可

subnet [网络号] netmask [子网掩码] {...}
  作用:定义作用域,指定子网掩码
  range [起始IP地址] [结束IP地址]
  作用:指定动态获取IP地址范围,可以声明多个range但范围不能重合。
  option routers [IP地址]
  作用:为客户端指定默认网关,可以全局可以局部
  option domain-name [域名]
  作用:为客户端指定默认的域,可以全局可以局部
  option domain-name-servers [IP地址]
  作用:为客户端指定默认DNS服务器地址,可以全局可以局部
  default-lease-time [数字]
  作用:默认最小租约期限(单位秒),可以全局可以局部
  max-lease-time [数字]
  作用:最长租约期限(单位秒),可以全局可以局部

具体配置如下

subnet 192.168.200.0 netmask 255.255.255.0 {
  range 192.168.200.20 192.168.200.100;
  option domain-name-servers 192.168.200.2;
  option netbios-name-servers 192.168.200.2;
  option subnet-mask 255.255.255.0;
  option routers 192.168.200.2;
  option broadcast-address 192.168.200.254;
  default-lease-time 600;
  max-lease-time 7200;
}

启动DHCP服务

systemctl start isc-dhcp-server

查看服务状态

root@huhy:~# systemctl status isc-dhcp-server
● isc-dhcp-server.service - ISC DHCP IPv4 server
     Loaded: loaded (/lib/systemd/system/isc-dhcp-server.service; enabled; vendor preset: enabled)
     Active: active (running) since Thu 2022-11-03 13:51:28 UTC; 4s ago
       Docs: man:dhcpd(8)
   Main PID: 4886 (dhcpd)
      Tasks: 4 (limit: 4538)
     Memory: 4.5M
        CPU: 8ms
     CGroup: /system.slice/isc-dhcp-server.service
             └─4886 dhcpd -user dhcpd -group dhcpd -f -4 -pf /run/dhcp-server/dhcpd.pid -cf /etc/dhcp/dhcpd.conf

Nov 03 13:51:28 huhy sh[4886]: Listening on LPF/ens38/00:0c:29:1a:93:d3/192.168.200.0/24
Nov 03 13:51:28 huhy sh[4886]: Sending on   LPF/ens38/00:0c:29:1a:93:d3/192.168.200.0/24
Nov 03 13:51:28 huhy dhcpd[4886]: Sending on   LPF/ens38/00:0c:29:1a:93:d3/192.168.200.0/24
Nov 03 13:51:28 huhy dhcpd[4886]: Listening on LPF/ens33/00:0c:29:1a:93:c9/192.168.200.0/24
Nov 03 13:51:28 huhy sh[4886]: Listening on LPF/ens33/00:0c:29:1a:93:c9/192.168.200.0/24
Nov 03 13:51:28 huhy sh[4886]: Sending on   LPF/ens33/00:0c:29:1a:93:c9/192.168.200.0/24
Nov 03 13:51:28 huhy sh[4886]: Sending on   Socket/fallback/fallback-net
Nov 03 13:51:28 huhy dhcpd[4886]: Sending on   LPF/ens33/00:0c:29:1a:93:c9/192.168.200.0/24
Nov 03 13:51:28 huhy dhcpd[4886]: Sending on   Socket/fallback/fallback-net
Nov 03 13:51:28 huhy dhcpd[4886]: Server starting service.
root@huhy:~#

centos测试

网卡修改为动态,然后重启

[root@huhy ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=dhcp
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=stable-privacy
NAME=ens33
UUID=52c22627-a236-404e-99a6-ed79ff9fd392
DEVICE=ens33
ONBOOT=yes
#IPADDR=192.168.200.150
#GATEWAY=192.168.200.2
#PREFIX=24
#DNS1=8.8.8.8
#DNS2=114.114.114.114

dhcp服务器Ubuntu部署_第2张图片
如图已经自动获取到Ubuntu设置的dhcp服务器网段20开始

Ubuntu脚本部署

#!/bin/bash

#设置Vmware网络设置为静态模式,虚拟机网卡配置为静态模式
#测试虚拟机为动态模式,查看是否获取到网段

apt install -y isc-dhcp-server

ip=192.168.200

cat >> /etc/dhcp/dhcpd.conf << eof
subnet $ip.0 netmask 255.255.255.0 {
  range $ip.20 $ip.100;
  option domain-name-servers $ip.2;
  option netbios-name-servers $ip.2;
  option subnet-mask 255.255.255.0;
  option routers $ip.2;
  option broadcast-address $ip.254;
  default-lease-time 600;
  max-lease-time 7200;
eof

systemctl start isc-dhcp-server

systemctl status isc-dhcp-server

你可能感兴趣的:(Linux,服务器,ubuntu,linux)