DHCP服务搭建
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了DHCP服务搭建相关的知识,希望对你有一定的参考价值。
DHCP实验
实验规划及环境准备
1、DHCP服务器应该配置成一个静态IP(最好使用仅主机模式做实验)
2、VMware内置DHCP服务器,应禁止仅主机模式VMnet1的DHCP服务
3、关闭selinux和防火墙
实验器材准备
2台虚拟机(CentOS7、CentOS7C)。7作为客户机(IP为192.168.137.200),7C作为客户机
.
1、为CentOS7主机添加一块仅主机网卡,并配置成静态IP=192.168.137.200(这个主机还有一块桥接网卡)
bash# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="none"
NAME="ens33"
UUID="af78583d-e2d9-42ba-83f1-42659c869aed"
DEVICE="ens33"
ONBOOT="yes"
IPADDR="192.168.137.200"
NETMASK="255.255.255.0"
.
2、将CentOS7C主机添加一块仅主机网卡,并配置成通过dhcp服务获得IP地址
bash# vim /etc/sysconfig/network-scripts/ifcfg-ens33
TYPE="Ethernet"
BOOTPROTO="dhcp"
NAME="ens33"
DEVICE="ens33"
ONBOOT="yes"
.
3、重启CentOS7C之后可以发现,主机CentOS7C的网卡上是没有IP地址的
bash# ifconfig
.
4、开始在CentOS7主机上配置DHCP服务
①、在CentOS7上安装dhcp包,并尝试启动dhcp服务
bash# yum install -y dhcp
bash# systemctl start dhcpd //发现启动不了,原因是主配置文件无内容
②、查看主配置文件之后,发现是空的,文件中提示有配置模板文件/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example,复制该文件至/etc/dhcp/dhcpd.conf,添加子网和地址池
bash# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
bash# vim /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "example.org"; //从DHCP服务器获取的域后缀,/etc/resolv.conf中的search,用于补全后缀
option domain-name-servers 114.114.114.114,1.1.1.1 //指明DNS服务器。放在此处所有子网有效
default-lease-time 600; //默认租期600s。租期未到,即使机器关机,IP地址也会被占用,租期过长,主机过多,新主机可能拿不到地址
max-lease-time 7200; //最长租期,客户端可以向服务器申请租期时间,这个是客户端可以申请的最大租期时间
log-facility local7;
subnet 192.168.137.0 netmask 255.255.255.0 { //subnet子网声明,说明将来要为哪个子网分配地址
range 192.168.137.100 192.168.137.199; //地址池为192.168.137.100到192.168.137.199
option routers 192.168.137.1; //为客户端分配网关
option domain-name-servers 114.114.114.114,1.1.1.1 //指明DNS服务器,子网中定义的优先级更高
next-server 192.168.137.199 //指定tftp服务器,DHCP客户机可以向此主机获取引导文件
filename "pxelinux.0"; //tftp服务器上引导文件的名字,next-server和filename用于实现自动安装
}
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;
}
subnet 10.254.239.32 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;
}
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;
}
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;
}
host passacaglia {
hardware ethernet 0:0:c0:5d:bd:95;
filename "vmunix.passacaglia";
server-name "toccata.fugue.com";
}
# 以下操作可以做MAC绑定固定IP
host fantasia {
#hardware ethernet 00:0c:29:f4:0a:fd; //客户机网卡的MAC地址,可以做静态地址绑定
#fixed-address 192.168.30.100; //为此MAC地址分配的固定IP,也可以在此处配置网关DNS等信息
#option routers 192.168.137.1 //为客户机指定网关
#option domain-name-servers 223.5.5.5; //为客户机指定阿里的DNS服务器
}
class "foo" {
match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}
shared-network 224-29 {
subnet 10.17.224.0 netmask 255.255.255.0 {
option routers rtr-224.example.org;
}
subnet 10.0.29.0 netmask 255.255.255.0 {
option routers rtr-29.example.org;
}
pool {
allow members of "foo";
range 10.17.224.10 10.17.224.250;
}
pool {
deny members of "foo";
range 10.0.29.10 10.0.29.230;
}
}
③、CentOS7启动dhcpd服务
bash# systemctl start dhcpd.service
.
5、查看CentOS7C是否获得IP地址
bash# ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.137.129 netmask 255.255.255.0 broadcast 192.168.137.255
inet6 fe80::777d:b963:b4c7:8408 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:f4:0a:fd txqueuelen 1000 (Ethernet)
RX packets 726 bytes 191927 (187.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 374 bytes 67729 (66.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
.
6、查看CentOS7中的租约信息文件/var/lib/dhcpd/dhcpd.leases
bash# vim /var/lib/dhcpd/dhcpd.leases
lease 192.168.137.129 {
starts 5 2018/10/05 14:55:30; //租约开始时间,UTC时间,比北京时间早8个小时
ends 5 2018/10/05 15:05:30; //租约结束时间,和开始时间正好差600s
cltt 5 2018/10/05 14:55:30;
binding state active;
next binding state free;
rewind binding state free;
hardware ethernet 00:0c:29:f4:0a:fd;
client-hostname "VCentOS75";
}
.
7、查看客户端租约文件
bash# dhclient -d
bash# vim /var/lib/dhclient/dhclient.leases
lease {
interface "eth0";
fixed-address 192.168.137.129; //确实获得了地址池中的地址
option subnet-mask 255.255.255.0;
option routers 192.168.137.1;
option dhcp-lease-time 600;
option dhcp-message-type 5;
option domain-name-servers 114.114.114.114,1.1.1.1;
option dhcp-server-identifier 192.168.137.200; //从DHCP服务器192.168.137.200上获取的IP地址
option domain-name "example.org";
renew 5 2018/10/05 15:14:47;
rebind 5 2018/10/05 15:19:45;
expire 5 2018/10/05 15:21:00;
}
以上是关于DHCP服务搭建的主要内容,如果未能解决你的问题,请参考以下文章
如何在linux下搭建dhcp服务器?最关键的配置文件有哪几个?分别有啥用?一般在linux下搭