DHCP
DHCP——dynamic host configuration protocol
动态主机配置协议(给其他客户端分配网络地址)
可自动分配入网参数
IP 地址 / 子网掩码 / 广播地址
默认网关地址
DN 服务器地址
获地址租约四次会话
C——>S DHCP DISCOVER
S——>C DHCP OFFER
C——>S DHCP REQUEST
S<——C DHCP ACK 或 DHCP NACK
客户端重启或租约使用时间达到百分之 50
发送 DHCP REQUSTT 包,若续航成功可继续战胜当前的 IP 地址,并重新计算租约时间 ,若续租失败也扔然使用当前的 IP 地址
租约使用时间达到百分之 87.5
发送 DHCP DISCOVERY 包,尝试获取新的 IP 地址租约,若世功则改用新地址,否则使用现有地址到租约过期后释放
租期 ———— 租约时间
允许客户机租用 IP 地址的时间期限,单位为秒
作用域
分配给客户机的 IP 地址所在的网段
地址池
用来动态分配的 IP 地址的范围
实验
服务器
[root@localhost ~]# yum install dhcp
主配置文件 /etc/dhcp/dhcpd.conf
样例 /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
执行程序 /usr/sbin/dhcpd
服务脚本 /etc/init.d/dhcpd
执行参数配置 /etc/sysconfig/dhcpd
[root@localhost ~]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
default-lease-time 600; 默认租期
max-lease-time 7200; 最大租期
option domain-name-servers IP,IP; 定义 DNS 服务器
subnet 192.168.100.0 netmask 255.255.255.0 { 定义网络作用域
range 192.168.100.150 192.168.100.200; range 定义地址池
option routers 192.168.100.100; routers 定义网关
option domain-name-servers 192.168.100.100;
}
注意 DHCP 服务器至少要定义一个与本机网卡同一网段的 subnet
客户端 ———— 地址为 150
[root@localhost ~]# tail /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE='eth0'
TYPE=Ethernet
ONBOOT=yes
BOOTPROTO=dhcp 改为 DHCP 为动态获取地址
[root@localhost ~]# route -n
Kernel IP routing table
Destination Gateway Genmask Flags Metric Ref Use Iface
192.168.100.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0 0.0.0.0 255.255.0.0 U 1002 0 0 eth0
0.0.0.0 192.168.100.100 0.0.0.0 UG 0 0 0 eth0
查看动态获取 IP 详细过程信息
[root@localhost ~]# dhclient –d
查看已获取的 IP 租约
[root@localhost ~]# cat /var/lib/dhclient/dhclient-eth0.leases
lease {
interface "eth0";
fixed-address 192.168.100.150;
option subnet-mask 255.255.255.0;
option dhcp-lease-time 600;
option routers 192.168.100.100;
option dhcp-message-type 5;
option dhcp-server-identifier 192.168.100.100;
option domain-name-servers 192.168.100.100;
option domain-name "example.org";
renew 6 2016/08/20 03:56:40;
rebind 6 2016/08/20 04:01:06;
expire 6 2016/08/20 04:02:21;
}
lease {
interface "eth0";
fixed-address 192.168.100.150;
option subnet-mask 255.255.255.0;
option routers 192.168.100.100;
option dhcp-lease-time 600;
option dhcp-message-type 5;
option domain-name-servers 192.168.100.100;
option dhcp-server-identifier 192.168.100.100;
option domain-name "example.org";
renew 6 2016/08/20 03:56:57;
rebind 6 2016/08/20 04:01:48;
expire 6 2016/08/20 04:03:03;
}
服务器查看
[root@localhost ~]# 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.1.1-P1
server-duid " 00 01 00 01 37J217o 00 14)q 04p";
lease 192.168.100.150 {
starts 6 2016/08/20 03:48:15;
ends 6 2016/08/20 03:58:15;
cltt 6 2016/08/20 03:48:15;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:34:74:19;
}
lease 192.168.100.150 {
starts 6 2016/08/20 03:52:21;
ends 6 2016/08/20 04:02:21;
cltt 6 2016/08/20 03:52:21;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:34:74:19;
}
lease 192.168.100.150 {
starts 6 2016/08/20 03:53:02;
ends 6 2016/08/20 04:03:02;
cltt 6 2016/08/20 03:53:02;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:34:74:19;
}
lease 192.168.100.150 {
starts 6 2016/08/20 03:56:56;
ends 6 2016/08/20 04:06:56;
cltt 6 2016/08/20 03:56:56;
binding state active;
next binding state free;
hardware ethernet 00:0c:29:34:74:19;
}
我们可以根据网卡名,选择 X 网卡启动 DHCP 服务
[root@localhost ~]# cat /etc/sysconfig/dhcpd
# Command line options here
DHCPDARGS= X 网卡名