智一面初中级LInux运维工程师在线评测:http://www.gtalent.cn/exam/interview/eUrdXoILlsGnh6At
DHCP实现
注意:实现DHCP服务前,先将网络已有DHCP服务,如:vmware中的DHCP关闭,访止冲突
DHCP服务的实现软件:
dhcp(CentOS 7 之前版本) 或 dhcp-server(CentOS 8 中的包名)
dnsmasq:小型服务软件,可以提供dhcp和dns功能
1 DHCP相关文件组成
dhcp或dhcp-server 包文件组成
/usr/sbin/dhcpd dhcp服务主程序
/etc/dhcp/dhcpd.conf dhcp服务配置文件
/usr/share/doc/dhcp-server/dhcpd.conf.example #dhcp服务配置范例文件
/usr/lib/systemd/system/dhcpd.service #dhcp服务service文件
/var/lib/dhcpd/dhcpd.leases 地址分配记录
dhcp-client客户端包
/usr/sbin/dhclient #客户端程序
/var/lib/dhclient #自动获取的IP信息
windows 工具
ipconfig /release #释放DHCP获取的IP,重新申请IP
ipconfig/renew #刷新租约,续约
2 DHCP服务器配置文件
注意:
DHCP服务器本身采用静态IP
必须配置和DHCP网卡的静态IP所在网段的subent段,否则DHCP服务无法启动
/etc/dhcp/dhcpd.conf格式
全局配置
subnet {
...
}
host {
}
3 DHCP实现
#centos实现DHCP
[root@centos7 ~]# yum install dhcp
[root@centos7 ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf #修改DHCP配置文件
[root@centos7 ~]# vim /etc/dhcp/dhcpd.conf
8 # 设置dns
9 option domain-name-servers 180.76.76.76,223.5.5.5;
10 # 租期以秒为单位
11 default-lease-time 86400;
12 # 最大有效期,一般用不到
13 max-lease-time 100000;
28 # 设置地址池和网关
29 subnet 10.0.0.0 netmask 255.255.255.0 {
30 range 10.0.0.40 10.0.0.80;
31 option routers 10.0.0.2;
32 }
[root@centos7 ~]# systemctl enable --now dhcpd
[root@centos7 ~]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sat 2021-05-08 15:12:52 CST; 4min 51s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 2713 (dhcpd)
Status: "Dispatching packets..."
CGroup: /system.slice/dhcpd.service
└─2713 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid
May 08 15:12:52 centos7 dhcpd[2713]: All rights reserved.
May 08 15:12:52 centos7 dhcpd[2713]: For info, please visit https://www.isc.org/software/dhcp/
May 08 15:12:52 centos7 dhcpd[2713]: Wrote 0 class decls to leases file.
May 08 15:12:52 centos7 dhcpd[2713]: Wrote 0 deleted host decls to leases file.
May 08 15:12:52 centos7 dhcpd[2713]: Wrote 0 new dynamic host decls to leases file.
May 08 15:12:52 centos7 dhcpd[2713]: Wrote 0 leases to leases file.
May 08 15:12:52 centos7 dhcpd[2713]: Listening on LPF/eth0/00:0c:29:e6:d5:03/10.0.0.0/24
May 08 15:12:52 centos7 dhcpd[2713]: Sending on LPF/eth0/00:0c:29:e6:d5:03/10.0.0.0/24
May 08 15:12:52 centos7 dhcpd[2713]: Sending on Socket/fallback/fallback-net
May 08 15:12:52 centos7 systemd[1]: Started DHCPv4 Server Daemon.
3.1 查看地址池中分配出去已经使用的地址
[root@centos7 ~]# cat /var/lib/dhcpd/dhcpd.leases
# The format of this file is documented in the dhcpd.leases(5) manual page.
# This lease file was written by isc-dhcp-4.2.5
server-duid "\000\001\000\001((\366t\000\014)\346\325\003";
lease 10.0.0.40 {
starts 6 2021/05/08 07:18:33; #获取IP地址时间,时区为UTC
ends 0 2021/05/09 07:18:33; #IP地址过期时间,时区为UTC
cltt 6 2021/05/08 07:18:33;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:3b:41:64; #使用此IP地址的机器的mac地址
}
3.2 已有静态地址,再次获取一个动态地址
[root@centos7 ~]# 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:c5:bd:97 brd ff:ff:ff:ff:ff:ff
inet 10.0.0.207/24 brd 10.0.0.255 scope global noprefixroute eth0
valid_lft forever preferred_lft forever
[root@centos7 ~]# 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:c5:bd:97
Sending on LPF/eth0/00:0c:29:c5:bd:97
Sending on Socket/fallback
DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 3 (xid=0x76331da)
DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x76331da)
DHCPOFFER from 10.0.0.217
DHCPACK from 10.0.0.217 (xid=0x76331da)
bound to 10.0.0.41 -- renewal in 38316 seconds. #获取到的IP地址
3.3 通过mac地址进行IP地址绑定
在服务器端进行修改配置文件
[root@centos7 ~]# vim /etc/dhcp/dhcpd.conf
65 host testhost{
66 hardware ethernet 00:0c:29:3b:41:64;
67 fixed-address 10.0.0.40;
68 }
[root@centos7 ~]# systemctl restart dhcpd #重启服务
运维工程师QQ在线交流群:580175957