DHCP 服务与管理
# DHCP 服务与管理
# 掌握要点
(1)了解 DHCP 服务的安装与配置。
(2)使用 DHCP 服务完成主机 IP 的自动获取。
# 部署流程
# 1.规划节点
IP | 主机名 | 节点 |
---|---|---|
11.0.1.128 192.168.2.2(LAN1) | xnode1 | DHCP 服务器 |
11.0.1.129 (DHCP-LAN1) | client | DHCP 客户端 |
# 2.添加网卡(两个节点)
⚫打开虚拟机设置添加网卡,单击“添加”按钮,如图
⚫在弹出的对话框中,选择网络适配器,然后单击“完成”按钮
⚫选择网络适配器 2,设置模式为 LAN 区段,选择 LAN 区段 1,如图 3-15-3 所示。如果没有 LAN 区段 1,单击下方“LAN 区段(S)”按钮,然后在弹出的对话框中单击“添加” 按钮,最后单击“确定”按钮
⚫使用 ip a 命令可以查看到新添加的网卡,但并没有相关配置文件
# 3.修改网卡配置
⚫在DHCP服务端进入网卡配置文件目录,复制第一块网卡配置。
⚫修改网卡配置文件。
⚫启动网卡并查看。
[root@xnode1 ~]# cd /etc/sysconfig/network-scripts/
[root@xnode1 network-scripts]# cp -rvf ifcfg-eno16777736 ifcfg-eno33554984
[root@xnode1 network-scripts]# vi ifcfg-eno33554984
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eno33554984
DEVICE=eno33554984
ONBOOT=yes
IPADDR=192.168.2.2
PREFIX=24
GATEWAY=192.168.2.1
[root@xnode1 ~]# ifup eno33554984
[root@xnode1 ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: eno16777736: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:8d:4e:69 brd ff:ff:ff:ff:ff:ff
inet 11.0.1.128/24 brd 11.0.1.255 scope global dynamic eno16777736
valid_lft 1579sec preferred_lft 1579sec
inet6 fd15:4ba5:5a2b:1008:20c:29ff:fe8d:4e69/64 scope global noprefixroute dynamic
valid_lft 86395sec preferred_lft 14395sec
inet6 fe80::20c:29ff:fe8d:4e69/64 scope link
valid_lft forever preferred_lft forever
3: eno33554984: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP qlen 1000
link/ether 00:0c:29:8d:4e:73 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.2/24 brd 192.168.2.255 scope global eno33554984
valid_lft forever preferred_lft forever
inet6 fe80::20c:29ff:fe8d:4e73/64 scope link
valid_lft forever preferred_lft forever
# 4.安装配置DHCP
⚫安装 DHCP 服务。
[root@xnode1 ~]# yum install -y dhcp
# 5.DHCP的配置文件模板
⚫可以在以下的模板中找到DHCP的配置文件,只需要找到对应需要的去更改就可以了。
[root@xnode1 ~]# cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
# 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
# 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; #IP地址池
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; #DNS服务器
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;
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地址
}
# You can declare a class of clients and then do address allocation
# based on that. The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.
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;
}
}
⚫修改 DHCP 配置文件。
[root@xnode1 ~]# vim /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
subnet 192.168.2.0 netmask 255.255.255.0 {
range 192.168.2.100 192.168.2.254;
option domain-name-servers 192.168.2.1;
option domain-name "test.cn";
option routers 192.168.2.1;
option broadcast-address 192.168.2.255;
default-lease-time 600;
max-lease-time 7200;
}
# 6.重启服务测试
⚫启动 DHCP 服务并设置开机自启,查看服务状态。
⚫Client 节点同样进行复制和修改网卡配置文件的操作。
[root@xnode1 ~]# systemctl start dhcpd
[root@xnode1 ~]# systemctl enable dhcpd
[root@xnode1 ~]# systemctl status dhcpd
[root@client ~]# cd /etc/sysconfig/network-scripts/
[root@client network-scripts]# cp -rvf ifcfg-eno16777736 ifcfg-eno33554984
[root@client network-scripts]# cat ifcfg-eno33554984
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
NAME=eno33554984
DEVICE=eno33554984
ONBOOT=yes
[root@client ~]# systemctl restart network
[root@client ~]# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
valid_lft forever preferred_lft forever
inet6 ::1/128 scope host
valid_lft forever preferred_lft forever
2: ens32: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
link/ether 00:0c:29:7c:32:c5 brd ff:ff:ff:ff:ff:ff
inet 192.168.2.100/24 brd 192.168.2.255 scope global noprefixroute dynamic ens32
valid_lft 599sec preferred_lft 599sec
inet6 fe80::fade:7228:6397:cdb1/64 scope link noprefixroute
valid_lft forever preferred_lft forever
上次更新: 2023/11/28, 22:03:59