网络配置
静态指定
- 动态获取: bootp:boot protocol MAC与IP一一静态对应
- dhcp:增强的bootp,动态
DHCP: ( Dynamic Host Configuration Protocol)
- 动态主机配置协议
- 局域网协议, UDP协议
- DHCP利用UDP协议的67:dhcp服务器端口 68:dhcp客户端端口
DHCP工作原理:基于广播形式。
主要用途:
- 用于内部网络和网络服务供应商自动分配IP地址给用户
- 用于内部网络管理员作为对所有电脑作集中管理的手段
使用场景
- 自动化安装系统
- 解决IPV4资源不足问题
DHCP的八种报文
- DHCP DISCOVER:客户端到服务器
- DHCP OFFER :服务器到客户端
- DHCP REQUEST:客户端到服务器
- DHCP ACK :服务器到客户端
- DHCP NAK:服务器到客户端,通知用户无法分配合适的IP地址
- DHCP DECLINE :客户端到服务器,指示地址已被使用
- DHCP RELEASE:客户端到服务器,放弃网络地址和取消剩余的租约时间
- DHCP INFORM:客户端到服务器, 客户端如果需要从DHCP服务器端获取更为详细的配置信息,则发送Inform报文向服务器进行请求,极少用到
DHCP服务简介
同网段多DHCP服务
- DHCP服务必须基于本地
- 先到先得的原则
跨网段
- RFC 1542 Compliant Routers 路由器自带的RFC1542
- dhcrelay: 中继
- /usr/sbin/dhcrelay dhcp服务的中继代理
路由器中的接口可以启动中继代理的功能。
相关协议:Arp /rarp
DHCP实现
Linux DHCP协议的实现程序: dhcp,或者dnsmasq软件都可以充当dhcp服务器( dhcp,dns)
Dhcp Server
/usr/sbin/dhcpd 存放dhcp发放IP等数据库信息
/etc/dhcp/dhcpd.conf --> /etc/rc.d/init.d/dhcpd
/etc/dhcp/dhcpd6.conf--> /etc/rc.d/init.d/dhcpd6
/usr/sbin/dhcrelay
/etc/rc.d/init.d/dhcrelay
dhcp server:67/udp
dhcp client: 68/udp
dhcpv6 client:546/udp
DHCP客户端client
dhclient
自动获取的IP信息: /var/lib/dhclient 系统中自身就有
dhcp的dhclient 客户端需要自动获取IP地址才会启动dhcp服务;也可以手动触发dhclient 客户端来生成自动获取IP
[[email protected] ~]# dhclient
[[email protected] ~]# ip a
dhclient -d 跟踪查看dhclinet 执行的过程
killall dhclient 可以看到信息。
查看客户端主机从哪个dhcp服务器中获取地址和有关信息在 /var/lib/dhclient目录中
[[email protected] network-scripts]# cd /var/lib/dhclient/
[[email protected] dhclient]# ls
dhclient.leases
[[email protected] dhclient]# cat dhclient.leases
DHCP服务器 服务端 server
[[email protected] ~]# yum info dhcp 查看dhcp说明
[[email protected] ~]# yum install dhcp 安装dhcp服务包
[[email protected] ~]# rpm -ql dhcp 查看包的文件列表
/usr/lib/systemd/system/dhcpd.service 服务
/etc/dhcp/dhcpd.conf 配置文件 默认为空
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example 配置范例
/usr/sbin/dhcpd 工作目录
注意:dhcp在空配置文件的情况下启动服务是起不来的 systemctl start dhcpd
拷贝范例文件来覆盖 .conf的配置文件
[[email protected] ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[[email protected]~]# vim ifcfg-eth0
BOOTPROTO=dhcp
[[email protected]~]# systemctl restart network.service
虚拟主机网卡设置:
当dhcp服务器给别人分配地址的同时,必须先给dhcp服务器本身的地址网段分配地址,然后在给另外网段的主机分配IP地址。
[[email protected] ~]# vim /etc/dhcp/dhcpd.conf
option domain-name "example.org"; 原本域名
option domain-name "zxbblog.org"; 可以任意修改
option domain-name-servers ns1.example.org, ns2.example.org; DNS服务
option domain-name-servers 114.114.114.144,1.1.1.1; 建议修改
default-lease-time 600; 默认租期时长,时长根据环境来定
max-lease-time 7200; 最大租期
修改配置文件中的一下内容;
修改配置文件之前
# 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;
修改配置文件后
cp /etc/dhcp/dhcpd.conf /data/ ##修改配置前先备份原来的配置文件
# DHCP server to understand the network topology.
subent 192.168.161.0 netmask 255.255.255.0 {
ranage 192.168.161.10 192.168.161.100; 地址池
option routers 192.168.161.254; 网关
}
先定义本网段地址:
subnet 192.168.161.0 netmask 255.255.255.0 {
}
[[email protected] dhcp]# systemctl restart dhcpd ##重启服务
[[email protected] dhcp]# systemctl restart dhcpd
[[email protected] dhcp]# systemctl start dhcpd
[[email protected] dhcp]# systemctl status dhcpd
Active: active (running)
[[email protected] dhcp]# vim dhcpd.conf
subent 192.168.161.0 netmask 255.255.255.0 {
ranage 192.168.161.10 192.168.161.100; 地址池
option routers 192.168.161.254; 网关
}
[[email protected] dhcp]# systemctl restart dhcpd
客户端配置:
[[email protected]]#cd /etc/sysconfig/network-scripts
[[email protected]]#vim ifcfg-eth0
DEVICE=eth0
IPADDR=192.168.161.128
PREFIX=24
BOOTPROTO=dhcp
#HWADDR=00:0C:29:75:B6:98
#TYPE=Ethernet
#UUID=a9cb5202-a938-492c-ad69-638e21cd6ac5
ONBOOT=yes
#NM_CONTROLLED=yes
[[email protected]]#service network restart
[[email protected]]#ip a
inet 192.168.161.10/24
查看客户端获取地址过程的详细信息:
[[email protected] ~]# cd /var/lib/dhclient/
[[email protected] ~]# ls
[[email protected] dhclient]# cat dhclient-5fb06bd0-0bb0-7ffb-45f1-d6edd65f3e03-eth0.lease
lease {
interface "eth0";
fixed-address 192.168.161.10;
option subnet-mask 255.255.255.0;
option routers 192.168.161.253;
option dhcp-lease-time 60000;
option dhcp-message-type 5;
option domain-name-servers 114.114.114.114,1.1.1.1;
option dhcp-server-identifier 192.168.161.131;
option domain-name "magedu.org";
renew 6 2018/09/22 13:53:13;
rebind 6 2018/09/22 20:35:24;
expire 6 2018/09/22 22:40:24;
}
DHCP如何做到给指定的主机分配指定IP地址
方法如下:
[[email protected] ~]# vim /etc/dhcp/dhcpd.conf
host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}
要修改的内容
host fantasia {
hardware ethernet 0f:00:07:58:45;
fixed-192.168.161.100;
}
[[email protected] ~]# service network restart
[[email protected] ~]# ip a
eth0:inet 192.168.161.100/24
****************************DHCP服务器与客户端请求通信完成**************************