1.dhcp:dynamic host configuration protocol(动态主机配置协议)
作用:利用一台主机自动分配所有的网络参数给每台客户机。
2.使用的c/s(client/server)结构:server:dhcp server udp:67
client:dhcp client udp:68(端口和协议)
3.c/s结构
4.DHCP四步租约
(1)客户机请求ip(dhcp discover)
首先客户机要设置为dhcp启动,在/etc/sysconfig/network-scripts/ifcfg-eht0这个文件中的bootproto选项设置成dhcp。客户机会发送出查找dhcp服务器的udp数据报给国有物理网段内的计算机。数据包的目标是(255.255.255.255)。
(2)服务器响应(dhcp offer)(ip/netmask)
服务器接受到了请求,会针对用户的mac地址和本身设置进行ip分配。
(3)客户机选择ip (dhcp request)
经过offer,客户机会在服务器提供的网络参数进行选择ip
(4)服务器确认 (dhcp ack)
服务器确认客户选择的ip,并且开始计算租约时间,一般客户机脱机ip地址收回,和客户机的租约到期ip收回。
5.dhcp的主要文件
(1)/etc/dhcp/dhcpd.conf:这是配置文件,我将配置文件分几个模块给大家讲解下。
# DHCP Server Configuration file. //这是dhcp的配置文件
# see /usr/share/doc/dhcp*/dhcpd.conf.sample //查看这个文件
# see 'man 5 dhcpd.conf' //man
上面是我安装dhcp,首次进入到配置文件中的显示,会发现里面没有任何配置只有这几行的代码,不要慌,这不是你的错,你要将第二行的文件复制过来就可以了。
[root@localhost /]# cp /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample /etc/dhcp/dhcpd.conf
这样就可以看见配置了!!!
下面是配置文件的内容
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
全局设置
-----------------------------------------------------------------------------------------------------
# option definitions common to all supported networks...
option domain-name "example.org"; //域名在/etc/resolv.conf设置一个search google.com 当你查找主机名的时候dns会主动帮你加上这个域名的后缀。
option domain-name-servers ns1.example.org, ns2.example.org; //dns的ip
default-lease-time 600; //默认的租约时间
max-lease-time 7200; //最大的租约时间
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none; //动态dns,更新主机名和ip的对应的关系
# 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; 日志文件
-----------------------------------------------------------------------------------------------------------------------------
子网分配设置
--------------------------------------------------------------------------------------------------------------------------
# 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; //分配网络的范围
option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}
# 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; //这几的ip地址
filename "vmunix.passacaglia"; //
server-name "toccata.fugue.com";
}
# 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; //mac地址
fixed-address fantasia.fugue.com; //只是固定的ip地址,也就是上面主机特定的ip
}
# You can declare a class of clients and then do address allocation
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org";
option domain-name-servers ns1.example.org, ns2.example.org;
default-lease-time 600;
max-lease-time 7200;
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none;
# 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;
# No service will be given on this subnet, but declaring it helps the
----------------------------------------------------------------------------------------------
配置文件总结
定义dhcpd自身的工作属性:
log-facility: 日志facilify
大部分是全局设置
全局地址分配属性:option打头
option router
子网配置:
通常每个作用域通过一个subnet定义
subnet NETWORK_ADDR netmask NETMASK {
range
option routers
}
主机配置:
通常为某特定MAC地址固定的分配一个地址
host 'HOST ID' {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address IP;
}
---------------------------------------------------------------------------------------
6.中继dhcp
工作原理