【08-04】搭建dhcp服务,实现ip地址申请分发
搭建dhcp服务,实现ip地址申请分发
0.环境准备
思路:两台设备 一台作为dhcp服务器下发地址,一台接收,如果接收的地址是设定网段的话则为成功,并通过看分发记录,对比MAC地址来确保下发成功。
需要先在VMware中关闭DHCP功能:
编辑->虚拟网络编辑器->更改设置->仅主机模式->取消勾选:使用本地DHCP服务器将IP地址分配给虚拟机
1.安装dhcp服务
#CentOS7之前安装dhcp 8安装dhcp-server [root@Rocky8-1 ~]# yum install -y dhcp-server
2.配置DHCP服务
#查看配置文件发现什么都没有 [root@Rocky8-1 ~]# vim /etc/dhcp/dhcpd.conf # DHCP Server Configuration file. # see /usr/share/doc/dhcp-server/dhcpd.conf.example # see dhcpd.conf(5) man page #如果想要了解则导入引导文件到配置文件中 [root@Rocky8-1 ~]# cat /usr/share/doc/dhcp-server/dhcpd.conf.example >> /etc/dhcp/dhcpd.conf #但是我没用配置 我这里使用的是最简单的配置 [root@Rocky8-1 ~]# vim /etc/dhcp/dhcpd.conf option domain-name-servers 114.114.114.114, 8.8.8.8; default-lease-time 86400; #默认租约时间 一天86400 max-lease-time 106400; log-facility local7; subnet 10.0.0.0 netmask 255.255.255.0 { range 10.0.0.200 10.0.0.220; option routers 10.0.0.2; }
3.启动DHCP服务
#因为是实验所以没开开机启动 [root@Rocky8-1 ~]# systemctl restart dhcpd [root@Rocky8-1 ~]# systemctl status dhcpd ● dhcpd.service - DHCPv4 Server Daemon Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled) Active: active (running) since Wed 2022-03-23 23:53:55 CST; 2s ago Docs: man:dhcpd(8) man:dhcpd.conf(5) Main PID: 2523 (dhcpd) Status: "Dispatching packets..." Tasks: 1 (limit: 2565) Memory: 4.8M CGroup: /system.slice/dhcpd.service └─2523 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Copyright 2004-2017 Internet Systems Consortium. 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: All rights reserved. 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: For info, please visit https://www.isc.org/software/dhcp/ 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Source compiled to use binary-leases 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Wrote 0 leases to leases file. 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Listening on LPF/eth0/00:0c:29:58:fe:9a/10.0.0.0/24 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Sending on LPF/eth0/00:0c:29:58:fe:9a/10.0.0.0/24 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Sending on Socket/fallback/fallback-net 3月 23 23:53:55 Rocky8-1 dhcpd[2523]: Server starting service. 3月 23 23:53:55 Rocky8-1 systemd[1]: Started DHCPv4 Server Daemon.
4.验证结果
#查看DHCP的分发信息 [root@Rocky8-1 ~]# vim /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.3.6 # authoring-byte-order entry is generated, DO NOT DELETE authoring-byte-order little-endian; server-duid "\000\001\000\001)\315\374b\000\014)X\376\232"; lease 10.0.0.200 { starts 3 2022/03/23 16:03:04; ends 4 2022/03/24 16:03:04; cltt 3 2022/03/23 16:03:04; binding state active; next binding state free; rewind binding state free; hardware ethernet 00:0c:29:6f:d5:df; uid "\001\000\014)o\325\337"; client-hostname "Rocky8-2"; } ===================================================================================================== # 查看接收端IP地址和MAC地址进行对比 [root@Rocky8-2 ~]# 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 3: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 00:0c:29:6f:d5:df brd ff:ff:ff:ff:ff:ff inet 10.0.0.200/8 brd 10.255.255.255 scope global dynamic noprefixroute eth0 valid_lft 1741sec preferred_lft 1741sec inet6 fe80::20c:29ff:fe6f:d5df/64 scope link noprefixroute valid_lft forever preferred_lft forever
实验成功
5.配置文件详解
[root@Rocky8-1 ~]# vim /etc/dhcp/dhcpd.conf # Sample configuration file for ISC dhcpd # ISC dhcpd的示例配置文件 # option definitions common to all supported networks... option domain-name "example.org"; option domain-name-servers ns1.example.org, ns2.example.org; #默认DNS #默认租约时间 一天86400 default-lease-time 600; #最大租约时间 106400 max-lease-time 7200; #是否开启全局动态dns更新 # Use this to enble / disable dynamic dns updates globally. #ddns-update-style none; # 如果这是权威dhcp服务器则关闭注释 # 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; #此处不提供服务器,声明后有助于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; #dhcp范围 option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org; #网关 } #此处语序BOOTP客户端获取动态地址,不推荐 # 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; next-server:10.0.0.8 #下载服务器使用TFTP服务地址 filename "pexlinux.0"; #读取的文件内容 server-name "toccata.example.com"; } #根据Mac地址分发地址,如果没有硬件地址则根据DHCP分发地址 # 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.example.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; } }
浙公网安备 33010602011771号