Centos7 搭建DHCP Server

DHCP 端口号查询
[root@localhost /]# vim /etc/services
输入/dhcp 快速查找关键字
在这里插入图片描述
DHCP协议交互过程
在这里插入图片描述
1)DHCP DISCOVER(0x01),此为Client广播寻找可用DHCP Server的过程
2)DHCP OFFER(0x02),此为Server对DHCP DISCOVER报文的响应,并向Client提供配置参数等信息
3)DHCP REQUEST(0x03),此报文是Client对server的DHCP OFFER报文的回应,或者是Client续延IP地址租期时发出的报文
4)DHCP DECLINE(0x04),当Client发现Server分配给它的IP地址无法使用,如IP地址冲突时,将发出此报文,通知Server禁止使用IP地址
5)DHCP ACK(0x05),Server对Client的DHCP REQUEST报文的确认响应报文,Client收到此报文后,才真正获得了IP地址和相关的配置信息。
6)DHCP NAK(0x06),Server对Client的DHCP REQUEST报文的拒绝响应报文,Client收到此报文后,一般会重新开始新的DHCP过程。
7)DHCP RELEASE(0x07),Client主动释放server分配给它的IP地址的报文,当Server收到此报文后,就可以回收这个IP地址,能够分配给其他的Client。
8)DHCP INFORM(0x08),Client已经获得了IP地址,发送此报文,只是为了从DHCP SERVER处获取其他的一些网络配置信息,如route ip,DNS Ip等,这种报文的应用非常少见。
如:运行IPCONFIG/RELEASE后,PC会发出释放IP的报文,DHCP Message Type是7,他的作用是主动释放server分配给它的IP地址的报文,Server收到此报文后,就可以回收这个IP地址,能够分配给其他的Client。

安装DHCP 服务操作步骤:
Dhcp 配置文件

[root@localhost xlxh]# ls /etc/dhcp/
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts

发现dhcpd.conf是一个空文件,需要参照/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example

[root@localhost doc]# cat dhc
dhclient-4.2.5/    dhcp-4.2.5/        dhcp-common-4.2.5/ 
 [root@localhost doc]# pwd
/usr/share/doc
[root@localhost doc]#
将范本拷贝到/etc/dhcp/目录下替换dhcpd.conf
[root@localhost dhcp]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@localhost dhcp]#
[root@localhost dhcp]# /etc/init.d/dhcpd restart
[root@localhost dhcp]# service dhcpd restart
Redirecting to /bin/systemctl restart dhcpd.service
Job for dhcpd.service failed because the control process exited with error code. See "systemctl status dhcpd.service" and "journalctl -xe" for details.
[root@localhost dhcp]# 
[root@localhost dhcp]# chkconfig dhcpd on
 [root@localhost dhcp]# chkconfig --list dhcpd
开机自启动
systemctl restart dhcpd.service
systemctl status  dhcpd

实战操作
公司有60台电脑,IP地址:192.168.1.2-192.168.1.250,子网掩码24,网关为192.18.1.1.
进入[root@localhost xlxh]# vim /etc/dhcp/dhcpd.conf修改配置

#A slightly different configuration for an internal subnet.
subnet 10.10.20.0 netmask 255.255.255.0 {
#DHCP Server config
  range 10.10.20.100 10.10.20.200;
  option domain-name-servers 10.10.20.254;
  option domain-name "internal.example.org";
  option routers 10.10.20.254;
  option broadcast-address 10.10.20.255;
  default-lease-time 600;
  max-lease-time 7200;

#fix IP assign to PC 指定某台电脑分配IP地址
  host xxx{
        hardware ethernet xx:xx:xx:xx:xx;
        fixed-address 10.10.20.100;
        }
}

如:PC连接DHCP Server后查看PC是否能动态获取IP地址
C:\Users\xlxh>hostname
xlxh-PC
C:\Users\xlxh>ipconfig /all

以太网适配器 本地连接:

   连接特定的 DNS 后缀 . . . . . . . :
   描述. . . . . . . . . . . . . . . : Realtek PCIe GBE Family Controller
   物理地址. . . . . . . . . . . . . : 2C:60:0C:1C:93:28
   DHCP 已启用 . . . . . . . . . . . : 是
   自动配置已启用. . . . . . . . . . : 是

猜你喜欢

转载自blog.csdn.net/weixin_42353331/article/details/86324628
今日推荐