DHCP ----- Dynamic Host Configuration Protocol 动态主机配置协议
应用层协议
作用:为客户端分配IP信息
原理:
1、客户端发送DHCP Discovery探索DHCP服务器
2、DHCP服务器发送DHCP Offer (IP/NETMASK/GATEWAY/DNS)
3、客户端发送DHCP Request
4、DHCP服务器发送DHCP ACK
5、客户端发送Gratuation ARP用于检测IP地址是否冲突
软件:dhcp
配置文件:/etc/dhcp/dhcpd.conf
服务:dhcpd
端口:67/udp(DHCP服务端端口) , 68/udp(DHCP客户端端口)
示例:配置DHCP服务器
环境描述:
隔离网络: Lan1 网段:192.168.87.0/24
DHCP服务器: 192.168.87.101, dhcpserver.linux.com
客户端:
Linux
windows XP
1) 配置主机名,IP地址
[[email protected] ~]# hostnamectl set-hostname server.linux.com
[[email protected] ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
# Generated by dracut initrd
DEVICE=eth0
NAME=eth0
ONBOOT=yes
BOOTPROTO=none
TYPE=Ethernet
IPADDR=192.168.87.101
NETMASK=255.255.255.0
[[email protected] ~]# systemctl restart network
[[email protected] ~]# systemctl restart NetworkManager
[[email protected] ~]# ip addr show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 52:54:00:71:3b:1c brd ff:ff:ff:ff:ff:ff
inet 192.168.87.101/24 brd 192.168.87.255 scope global eth0
valid_lft forever preferred_lft forever
inet6 fe80::5054:ff:fe71:3b1c/64 scope link
valid_lft forever preferred_lft forever
2) 配置yum源,安装DHCP软件
[[email protected] ~]# mkdir /etc/yum.repos.d/default
[roo[email protected] ~]# mv /etc/yum.repos.d/CentOS-* /etc/yum.repos.d/default
[[email protected] ~]# vim /etc/yum.repos.d/local.repo
[local]
name=localsrc
baseurl=file:///mnt
enabled=1
gpgcheck=0
[[email protected] ~]# mount /dev/cdrom /mnt/
[[email protected] ~]# yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: local
Cleaning up everything
[[email protected] ~]# yum makecache 生成元数据
[[email protected] ~]#
[[email protected] ~]# yum install -y dhcp
3) 编辑配置文件
[[email protected] ~]# vim /etc/dhcp/dhcpd.conf
subnet 192.168.87.0 netmask 255.255.255.0 {
range 192.168.87.10 192.168.87.100;
option routers 192.168.87.1;
option domain-name-servers 114.114.114.114, 8.8.8.8;
}
[[email protected] ~]# systemctl start dhcpd
[[email protected] ~]# systemctl enable dhcpd >>>设置服务开机自动启动
Created symlink from /etc/systemd/system/multi-user.target.wants/dhcpd.service to /usr/lib/systemd/system/dhcpd.service.
[[email protected] ~]# ss -anup | grep :67
UNCONN 0 0 *:67 *:* users:(("dhcpd",pid=1822,fd=7))
UNCONN 0 0 *%virbr0:67 *:* users:(("dnsmasq",pid=1506,fd=3))
4) 测试
DHCP排错:
日志:/var/log/messages
# tail -f /var/log/messages