Linux搭建DHCP服务器实现自动分配IP地址
Posted 白-胖-子
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linux搭建DHCP服务器实现自动分配IP地址相关的知识,希望对你有一定的参考价值。
DHCP服务
- Dynamic Host Configuration Protocol,动态主机配置协议
DHCP服务的协议和端口
- UDP协议,C/S模式,
- DHCP server:67/udp,
- DHCPv4 client :68/udp,
- DHCPv6 client:546/udp
DHCP工作原理
类似于投简历找工作 到达租期的一半重新续约,达到7/8时再去续约
-
DHCP通讯过程基于广播实现
-
DHCP服务器打开67端口并进行监听
-
客户端监听UDP的68端口,在网卡启动前发送广播请求
-
DHCP广播机制存在跨路由问题
-
DHCP使用中继代理机制,硬件路由器和三层交换机接口要配置agent,
-
可以替客户端将DHCP请求单播到DHCP服务器,从而实现跨网段
DHCP八种报文
DHCP DISCOVER:客户端到服务器
DHCP OFFER :服务器到客户端
DHCP REQUEST:客户端到服务器
DHCP ACK :服务器到客户端
DHCP NAK:服务器到客户端,通知用户无法分配合适的IP地址
DHCP DECLINE :客户端到服务器,指示地址已被使用
DHCP RELEASE:客户端到服务器,放弃网络地址和取消剩余的租约时间
DHCP INFORM:客户端到服务器, 客户端如果需要从DHCP服务器端获取更为详细的配置信息,则
发送Inform报文向服务器进行请求,极少用到
DCHP常用架构
- 两台服务器做主备
- 地址池二八分配
关闭或隔离本网中已有DHCP服务器
- 实现DHCP服务前,先将网络已有DHCP服务,
- 如:vmware中的DHCP关闭,访止冲突
DHCP服务器实现
- dchp可以通过多个软件实现
- 这里我们使用系统自带的
- dhcp(CentOS 7 之前版本)
- dhcp-server(CentOS 8 中的包名)
- DHCP搭建三步走
- 安装DHCP服务器软件
- 按需修改配置文件
- 启动并设为开机启动
安装DHCP服务器软件
- 在CentOS 8中进行安装
-
检查DHCP服务是否已安装
rpm -q dchp-server 未安装软件包 dchp-server
-
查看DHCP安装包信息
yum info dhcp-server
yum info dhcp-server BaseOS 1.0 kB/s | 3.9 kB 00:03 AppStream 52 kB/s | 4.3 kB 00:00 EPEL 52 kB/s | 4.7 kB 00:00 extras 19 kB/s | 1.5 kB 00:00 centosplus 17 kB/s | 1.5 kB 00:00 可安装的软件包 名称 : dhcp-server 时期 : 12 版本 : 4.3.6 发布 : 41.el8 架构 : x86_64 大小 : 530 k 源 : dhcp-4.3.6-41.el8.src.rpm 仓库 : BaseOS 概况 : Provides the ISC DHCP server URL : http://isc.org/products/DHCP/ ## 官方网站 协议 : ISC 描述 : DHCP (Dynamic Host Configuration Protocol) is a protocol which allows : individual devices on an IP network to get their own network : configuration information (IP address, subnetmask, broadcast address, : etc.) from a DHCP server. The overall purpose of DHCP is to make it : easier to administer a large network. : : This package provides the ISC DHCP server.
-
安装DHCP服务器
yum -y install dhcp-server
systemctl status dhcpd ● dhcpd.service - DHCPv4 Server Daemon Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled) Active: inactive (dead) Docs: man:dhcpd(8) man:dhcpd.conf(5)
-
查看安装后的文件列表
关注几个重要的文件,比如主程序文件、配置文件、安装目录等rpm -ql dhcp-server
rpm -ql dhcp-server /etc/NetworkManager /etc/NetworkManager/dispatcher.d /etc/NetworkManager/dispatcher.d/12-dhcpd /etc/dhcp /etc/dhcp/dhcpd.conf ## 主配置文件 /etc/dhcp/dhcpd6.conf ## IPv6配置文件 …… /usr/lib/systemd/system/dhcpd.service ## 主程序服务 /usr/lib/systemd/system/dhcpd6.service ## IPv6主程序 /usr/sbin/dhcpd ## 主程序文件 /usr/share/doc/dhcp-server …… /usr/share/doc/dhcp-server/dhcpd.conf.example ## 配置模板 /usr/share/doc/dhcp-server/dhcpd6.conf.example## 配置模板 …… /var/lib/dhcpd ##分配地址库文件的目录 /var/lib/dhcpd/dhcpd.leases ## IPv4地址库 /var/lib/dhcpd/dhcpd6.leases ## IPv6地址库
修改DHCP配置文件
- DHCP服务刚装好是无法直接启动的
- 安装后如果起不来,看日志
- 安装后配置文件是空的
cat /etc/dhcp/dhcpd.conf # # DHCP Server Configuration file. # see /usr/share/doc/dhcp-server/dhcpd.conf.example # see dhcpd.conf(5) man page #
-
备份源文件,并将模板文件copy过来
mv /etc/dhcp/dhcpd.conf /etc/dhcp/dhcpd.conf.bak`date +%F` cp /usr/share/doc/dhcp-server/dhcpd.conf.example /etc/dhcp/dhcpd.conf
-
修改copy过来的模板文件
vim /etc/dhcp/dhcpd.conf
需要修改的选项
- domain-name
# option definitions common to all supported networks... option domain-name "example.org";## 自己的域名 ## DNS服务器地址 option domain-name-servers ns1.example.org, ns2.example.org;
修改为:
option domain-name "timonium.co";## 自己的域名 ## DNS服务器地址 option domain-name-servers 114.114.114.114, 8.8.8.8;
- 租期
长租期可以减少服务器压力
default-lease-time 600; ## 默认租期,比较短 max-lease-time 7200;## 最大租期
修改为更长时间
default-lease-time 86400; ## 默认租期,比较短 max-lease-time 106400;## 最大租期
- 修改默认地址池
# 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 { }
修改为当前网段
subnet 10.0.0.0 netmask 255.255.255.0 {
range 10.0.0.180 10.0.0.200;
option routers 10.0.0.2;
}
启动服务并设为开机启动
systemctl enable --now dhcpd.service
[07:14:27 root@C8-88[ ~]#systemctl enable --now dhcpd.service
[07:14:30 root@C8-88[ ~]#systemctl status dhcpd.service
● dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2021-05-05 07:14:30 CST; 48s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 3432 (dhcpd)
Status: "Dispatching packets..."
Tasks: 1 (limit: 11337)
Memory: 5.4M
CGroup: /system.slice/dhcpd.service
└─3432 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
May 05 07:14:30 C8-88 dhcpd[3432]:
May 05 07:14:30 C8-88 dhcpd[3432]: No subnet declaration for eth0 (no IPv4 addresses).
May 05 07:14:30 C8-88 dhcpd[3432]: ** Ignoring requests on eth0. If this is not what
May 05 07:14:30 C8-88 dhcpd[3432]: you want, please write a subnet declaration
May 05 07:14:30 C8-88 dhcpd[3432]: in your dhcpd.conf file for the network segment
May 05 07:14:30 C8-88 dhcpd[3432]: to which interface eth0 is attached. **
May 05 07:14:30 C8-88 dhcpd[3432]:
May 05 07:14:30 C8-88 dhcpd[3432]: Sending on Socket/fallback/fallback-net
May 05 07:14:30 C8-88 dhcpd[3432]: Server starting service.
May 05 07:14:30 C8-88 systemd[1]: Started DHCPv4 Server Daemon.
客户端测试
- 手动启动DHCP客户端程序
dhclient -d
- 可以查看到获取IP的详细情况
[root@c7-53 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:50:04:8d brd ff:ff:ff:ff:ff:ff
inet 10.0.0.53/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe50:48d/64 scope link
valid_lft forever preferred_lft forever
[root@c7-53 ~]# dhclient -d
Internet Systems Consortium DHCP Client 4.2.5
Copyright 2004-2013 Internet Systems Consortium.
All rights reserved.
For info, please visit https://www.isc.org/software/dhcp/
Listening on LPF/eth0/00:0c:29:50:04:8d
Sending on LPF/eth0/00:0c:29:50:04:8d
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6 (xid=0x51e6bfe1)
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x51e6bfe1)
DHCPOFFER from 10.0.0.88
DHCPACK from 10.0.0.88 (xid=0x51e6bfe1)
bound to 10.0.0.180 -- renewal in 693 seconds.
- 客户端日志位置
/var/lib/dhclient/dhclient.leases
服务器端查看日志确认分配IP
tail -f /var/lib/dhcpd/dhcpd.leases
[07:15:18 root@C8-88[ ~]#tail -f /var/lib/dhcpd/dhcpd.leases
lease 10.0.0.180 {
starts 2 2021/05/04 23:17:42; ## 租期开始时间
ends 2 2021/05/04 23:44:22;##租期结束时间
cltt 2 2021/05/04 23:17:42;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:50:04:8d;
}
- 至此,DHCP服务器已经搭建完成
IP地址和MAC地址的静态绑定
- 固定IP地址可以防止IP冲突
- 固定地址有利于服务器互相访问
- 修改DHCP服务器配置文件相关内容实现
# 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.example.com"; }
- 参考并修改为
host webServer1 {
hardware ethernet 0:0:c0:5d:bd:95;
fixed-address 10.0.0.188;
filename "web1";
server-name "web1.timonium.co";
}
- 重启服务后生效
以上是关于Linux搭建DHCP服务器实现自动分配IP地址的主要内容,如果未能解决你的问题,请参考以下文章
LINUX系统服务器上搭建DHCP服务,实现两大基本功能:1,自动分配ip;2,手工指定ip