dhcp server:
DHCP:Dynamic Host Configuration Protocol arp --> address resolving Protocol IP --> MAC rarp --> reverse arp MAC --> IP bootp --> boot protocol dhcp: lease time, bootp 工作流程:广播 (1) Client:dhcp discover (2) Server: dhcp offer(IP/Netmask, GW, DNS Server, NTP Server, Wins Server, ...) (3) Client:dhcp request (4) Server:dhcp ack lease time:租约期限; 续租:50%, 75%, 87.5%, 93.75% dhcp discover,... 续租: dhcp request dhcp ack|nak
实现:
dhcp (isc.org) - The dhcp package provides the ISC DHCP service and relay agent. dnsmasq (dns, dhcp) - Dnsmasq is lightweight, easy to configure DNS forwarder and DHCP server.(轻量级的dhcp用于嵌入式系统) It is designed to provide DNS and, optionally, DHCP, to a small network.
dhcp:
程序环境:
dhcp server: /usr/sbin/dhcpd IPV4网络: /etc/dhcp/dhcpd.conf /usr/lib/systemd/system/dhcpd.service IPV6网络: /etc/dhcp/dhcpd6.conf /usr/lib/systemd/system/dhcpd6.service dhcrelay: 实现dhcp中继功能 /usr/sbin/dhcrelay /usr/lib/systemd/system/dhcrelay.service 监听的地址和端口: Server: 67/udp Client: 68/udp
配置
配置文件:/etc/dhcp/dhcpd.conf 配置指令: option选项:配置给客户端的相信息 指令:定义dhcp server的工作特性 /usr/share/doc/dhcp-x.x.x/dhcp.conf.example复制到/etc/dchp/目录下修改配置文件即可 配置有级别之分: 全局配置 子网配置 子网配置覆盖全局配置 主机配置
常用的配置:
default-lease-time 600;默认租约期限 max-lease-time 7200; 最大租约期限 option domain-name "search_domain.tld"; 指定搜索域,例如客户端ping www没有ping到,会自动加上域名。再ping option domain-name-servers DNS_SERVER1, DNS_SERVER2, DNS_SERVER3; 指定dns地址的 option routers GW1, GW2, ...; 配置给客户端的指定网关 option broadcast-address BROADCAST_ADDRESS;指定广播地址 #option一般是配置给请求者的,客户端的。 subnet NETWORK netmask MASK { #定义一个子网,作用域 range START_IP END_IP; #定义地址范围 } 定义网络作用域,即一个子网,主要用于指明地址池; host passacaglia { 定义主机作用域,主机的配置绑定mac和ip hardware ethernet 0:0:c0:5d:bd:95; fixed-address IP_ADDR; }
实验:
[root@localhost ~]# cat /etc/dhcp/dhcpd.conf option domain-name "magedu.com"; option domain-name-servers 10.1.0.1; default-lease-time 3600; max-lease-time 7200; log-facility local7; subnet 10.1.0.0 netmask 255.255.0.0 { range 10.1.100.20 10.1.100.29; option routers 10.1.0.1; }
地址绑定配置
host fantasia { #不使用主机名的话,输入什么都行 hardware ethernet fixed-address fantasia.mageducom; ##也可以直接输入ip }
也可以直接绑定ip,dhcp与dns结合可以实现动态域名解析-ddns
dhclient -d 自动激活网卡获取地址并在前台显示dhcp报文信息
[root@yangyouwei ~]# dhclient -d Internet Systems Consortium DHCP Client 4.1.1-P1 Copyright 2004-2010 Internet Systems Consortium. All rights reserved. For info, please visit https://www.isc.org/software/dhcp/ Listening on LPF/pan0/c2:e1:24:cc:07:be Sending on LPF/pan0/c2:e1:24:cc:07:be Listening on LPF/eth1/00:0c:29:83:fa:81 Sending on LPF/eth1/00:0c:29:83:fa:81 Listening on LPF/eth0/00:0c:29:83:fa:77 Sending on LPF/eth0/00:0c:29:83:fa:77 Sending on Socket/fallback DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0x5e0d2a8e) DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x607ae9f8) DHCPACK from 10.1.0.1 (xid=0x5e0d2a8e) bound to 10.1.100.20 -- renewal in 1735 seconds. DHCPDISCOVER on pan0 to 255.255.255.255 port 67 interval 6 (xid=0x2d47c28f) DHCPACK from 192.168.1.1 (xid=0x607ae9f8) bound to 192.168.1.117 -- renewal in 3070 seconds. DHCPDISCOVER on pan0 to 255.255.255.255 port 67 interval 13 (xid=0x2d47c28f) DHCPDISCOVER on pan0 to 255.255.255.255 port 67 interval 7 (xid=0x2d47c28f) DHCPDISCOVER on pan0 to 255.255.255.255 port 67 interval 15 (xid=0x2d47c28f) DHCPDISCOVER on pan0 to 255.255.255.255 port 67 interval 11 (xid=0x2d47c28f) DHCPDISCOVER on pan0 to 255.255.255.255 port 67 interval 9 (xid=0x2d47c28f) No DHCPOFFERS received. No working leases in persistent database - sleeping.
以上信息是双网卡的,并且已经能获取过地址了。所以信息不是dhcp开始获取地址的状态信息。
查看route -n 路由表中是否有dhcp中设置的网关。
原创文章,作者:yyw,如若转载,请注明出处:http://www.178linux.com/56566