RHEL5搭建DHCP
此实验利用VMware12搭建的环境,网卡都是-自定义VMnet3
网络环境:
一台server(DHCP),一台client 其中一台当做打印机使用,固定IP(根据MAC地址)
需求描述:
1:DHCP服务器的主机名为dhcpsvr.mvp.com IP地址:192.168.1.10/24
2:给网络内各主机自动分配IP,地址范围192.168.1.20-192.168.1.60
3:局域网内各主机用的默认网关地址为192.168.1.254
4:局域网使用的DNS服务器地址分别为192.168.1.6,DNS服务器的主机名为ns2.mvp.com
5:网络打印机设备,主机名为print01,要求自动分配固定IP192.168.1.50
要求1:设置dhcp网卡信息
cd /etc/sysconfig/network-scripts/ 切换到网卡目录下
配置默认网卡 vim/etc/sysconfig/network-scripts/ifcfg-eth0
编辑网卡配置信息
默认的网卡信息
更改后
保存
[root@localhost ~]# cat/etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=static
HWADDR=08:00:27:36:C4:81
ONBOOT=yes
IPADDR=192.168.1.10
NETMASK=255.255.255.0
GATEWAY=192.168.1.254
DNS1=192.168.1.6
修改网络主机 /etc/sysconfig/network
验证
vim /etc/hosts 编辑完wq 保存并退出
验证
[root@localhost ~]# cat /etc/hosts
# Do not remove the following line, orvarious programs
# that require network functionality willfail.
127.0.0.1 localhost.localdomain localhost
192.168.1.10 dhcpsvr.mvp.com
192.168.1.6 ns2.mvp.com
重启网卡
检查dhcp安装信息
要将服务器配置成为DHCP服务器,还需要安装以下程序包:dhcp-3.0.5-18.el5.i386.rpm,dhcp-devel-3.0.5-18.el5.i386.rpm,dhcpv6-1.0.10-16.el5.i386.rpm。
(注意,不同的操作系统版本,DHCP安装包的版本会不一样)
先安装
[root@localhost Server]#rpm -ivh dhcp-3.0.5-23.el5.i386.rpm
warning:dhcp-3.0.5-23.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
error: failed to stat/media/RHEL_5.5 i386 DVD: 没有那个文件或目录
Preparing... ###########################################[100%]
1:dhcp ########################################### [100%]
其次
[root@localhost Server]#rpm -ivh dhcp-devel-3.0.5-23.el5.i386.rpm
warning:dhcp-devel-3.0.5-23.el5.i386.rpm: Header V3 DSA signature: NOKEY, key ID37017186
error: failed to stat/media/RHEL_5.5 i386 DVD: 没有那个文件或目录
Preparing... ########################################### [100%]
1:dhcp-devel ########################################### [100%]
验证
[root@localhost Server]#rpm -qa |grep dhcp
dhcpv6-client-1.0.10-18.el5
dhcp-devel-3.0.5-23.el5
dhcp-3.0.5-23.el5
将/usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample复制到/etc/dhcpd.conf,也可以新建一个dhcpd.conf。
[root@localhost ~]# cp/usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
cp:是否覆盖“/etc/dhcpd.conf”? y
配置dhcp
[root@localhost ~]# vim/etc/dhcpd.conf
默认·的
更改后查询结果
[root@localhost ~]# cat/etc/dhcpd.conf
ddns-update-styleinterim;
ignore client-updates;
subnet 192.168.1.0netmask 255.255.255.0 { 默认网段
# --- default gateway
option routers 192.168.1.254; 默认网关
option subnet-mask 255.255.255.0; 默认掩码
option nis-domain "ns2.mvp.com"; dns域名
option domain-name "ns2.mvp.com"; 域名
option domain-name-servers 192.168.1.6; dns服务器地址
option time-offset -18000; # Eastern Standard Time
# option ntp-servers 192.168.1.6; dhcp服务器地址
# option netbios-name-servers 192.168.1.6;
# --- Selectspoint-to-point node (default is hybrid). Don't change this unless
# -- you understandNetbios very well
# option netbios-node-type 2;
range dynamic-bootp 192.168.1.20192.168.1.60; 地址池
default-lease-time 21600;
max-lease-time 43200;
# we want the nameserver to appear at afixed address
host ns {
next-server dhcpsrv.mvp.com;
hardware ethernet08:00:27:D4:2A:F5; 预留mac地址 (给打印机使用)
fixed-address 192.168.1.50; 预留ip地址
}
}
[root@localhost ~]#开启dhcp服务
关闭防火墙
去客户机验证
禁用再启用,cmd命令行输入ipconfig /renew 更新ip地址 ipconfig/release 释放ip地址
ipconfig /all 查看网卡详细信息包括mac地址,主机名等等。
获取到1.50没 这就给打印机预留了
了一个ip地址 为固定ip.
实验总结:dhcp实现的范围广,灵活性强。
补充
客户端租用IP地址的信息都会存放到租约数据库,
可以使用命令:cat /var/lib/dhcpd/dhcpd.leases查看租约数据库信息。注意,刚安装好的租约数据库是空的,需要DHCP服务器运行,有客户端使用后才有数据记录。
[root@localhost ~]# cat/var/lib/dhcpd/dhcpd.leases
# All times in this fileare in UTC (GMT), not your local timezone. This is
# not a bug, so pleasedon't ask about it. There is noportable way to
# store leases in thelocal timezone, so please don't request this as a
# feature. If this is inconvenient or confusing to you,we sincerely
# apologize. Seriously, though - don't ask.
# The format of this fileis documented in the dhcpd.leases(5) manual page.
# This lease file waswritten by isc-dhcp-V3.0.5-RedHat
lease 192.168.1.60 {
starts 2 2018/02/06 20:56:55;
ends 2 2018/02/06 20:57:03;
tstp 2 2018/02/06 20:57:03;
binding state free;
hardware ethernet 08:00:27:d4:2a:f5;
uid "\001\010\000'\324*\365";
}
名词解释:
配置参数意义
修改配置文件/etc/dhcp/dhcpd.conf
如下:
//支持的dhcp动态更新方式(non:不支持;interim:互动更新模式;ad-hoc:特殊更新模式)
ddns-update-style interim;
#ignore client-updates;
#subnet<子网络号> netmask <子网掩码>:
subnet 192.168.1.0 netmask 255.255.255.0 {
#指定地址池,将在192.168.1.20~192.168.1.254范围之内的IP地址动态分配给客户机
range 192.168.1.20 192.168.1.254;
option routers192.168.1.1; #客户端默认网关为192.168.1.1
option subnet-mask255.255.255.0; #指定客户端子网掩码
option domain-name"example.com"; #指定DNS后缀
option domain-name-servers*.*.*.*; #DNS服务器地址
option broadcast-address192.168.1.255; #指定广播地址
default-lease-time 86400; #指定默认租约时间,单位是秒 默认1天
max-lease-time 172800; #指定最长租赁时间,单位是秒
#为网络适配器的物理地址为00:0C:29:23:F5:A2的主机保留指定IP地址
host pc1 {
hardware ethernet 00:0C:29:D4:E7:58;
fixed-address 192.168.1.50;
}
}
设置默认网卡IP为相同网段
修改:/etc/sysconfig/network-scripts/ifcfg-eth0”文件如下:
DEVICE="eth0"
NM_CONTROLLED="yes"
ONBOOT=yes
TYPE=Ethernet
BOOTPROTO="static"
IPADDR=192.168.1.1
PREFIX=24
GATEWAY=x.x.x.x
DNS1=x.x.x.x
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
设置参数:
default-lease-time 时间:租约时间
max-lease-time 时间:最长租约时间
option domain-name “域名”:设置DHCP客户端resolve.conf内解析用的域名
option domain-name-servers IP1, IP2:设置DHCP客户端resolve.confneo的IP
option routers:设置路由信息
声明IP地址池和相关配置:
subnet NETWORK_IP netmask NETMASK_IP {
range IP1 IP2;设置IP范围
host 主机名 { //静态IP设置
hardware ethernert 硬件地址;
fixed-address IP;
};
}