Centos DHCP service to build and test

Preface:

Record Centos learning!

DHCP server role

  • Large number of clients to automatically assign an address to provide centralized management
  • Reduce the management and maintenance costs and improve the efficiency of network configuration

DHCP principle

  • Client looking for a server
  • Server provides address information
  • Accept and Broadcasting
  • Server Confirmation
  • The client re-visit
  • Server Confirmation

Data packets sent by the client will include its own MAC address and host name to broadcast the way sent out

DHCP configuration

  • Port udp67 udp68
  • Service dhcpd dhrelay
  • Profile: /etc/dhcp/dhcpd.conf
  • Relay file: / etc / sysconfig / dhcrealy

Installing the DHCP Service

Because it is just built a virtual machine, so the first thing is to change the source (I use the Centos7)

Here I replaced Ali source

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

After running

yum clean all
yum makecache

Generate cache
download

yum –y install dhcp
或者
yum  install dhcp

If this problem occurs, the download can not put the plug to switch off
Here Insert Picture Description

修改插件的配置文件
$vim /etc/yum/pluginconf.d/fastestmirror.conf
enabled = 0//由1改为0,禁用该插件

Here Insert Picture Description

修改yum的配置文件
$vim /etc/yum.conf
plugins= 0//改为0,不使用插件

Here Insert Picture Description
Then download, download Eau success
Here Insert Picture Description

Configuring DHCP

Check the main configuration file

cat /etc/dhcp/dhcpd.conf

Here Insert Picture Description
Copy the template files to the / etc / dhcp directory

cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf 

Detailed profiles

# 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;(DNS服务器的主机名或IP)

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;(日志类型,日志类型是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 {
}
subnet 网络 nemtmask 子网掩码 {
选项或参数
}
# 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;(网关)
}

# 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;
}
#其实上面那么多注释以及代码都是举得如何配置的例子,你想要配置一个较好的DHCP,应该包含哪些内容
#下面的就是一个比较常见的声明,编辑的话应该按照这样进行编辑
# 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";(DNS域名,可以忽略)
  option routers 10.5.5.1;(网关)
  option broadcast-address 10.5.5.31;(广播地址)
  default-lease-time 600;(租约期限)
  max-lease-time 7200;(最大租约期限)
}
。。。。。。    
下面就是对特定主机进行设置,分配固定的IP,暂时用不到            

So do not look at the configuration file so long, in fact, when you edit it so short, you can only keep a scope

Edit Profile

vi /etc/dhcp/dhcpd.conf 

As shown below:
Here Insert Picture Description
start dhcp service after configured

systemctl start dhcpd

Look dhcp status, see udp port 67 is up there

netstat -anptu | grep dhcp

Here Insert Picture Description
So far the service has been set up DHCP, configuration is complete, then you add the client to verify.

Here again the record about commonly used commands .

#查看本机网关
route -n
#查看路由
ip route
#查看DNS
cat /etc/resolv.conf
#查看主机名
hostname
#查看MAC
ifconfig -a

Connection Test (host and client in the same network segment)

第一次实验:
客户机:Centos7(无界面)
服务机:Centos7(有界面)

First make sure both machines in the same network segment, let the client and server in the same mode, or can not obtain ip address, virtual machine two network connections select only host mode
Here Insert Picture Description
wow, this experiment on your own machine really is a bit distressed computer, but there is no way to learn

In order to enable the client to assign IP, LAN need to edit the configuration file

$vi /etc/sysconfig/network-scripts/ifcfg-ens33

Then modify the configuration file

ONBOOT=yes是指系统启动时激活此设备
BOOTPROTO=dhcp是指网卡的获取IP方式为dhcp
想要改成静态获取的,可以把这个值设为static

Here Insert Picture Description
After modifying the save and exit, and then restart the network card

$service network restart

Check allocation dhcp server side, found

cat /var/lib/dhcpd/dhcpd.leases

Here Insert Picture Description
Assigned out 192.168.73.20, to the client look at
Here Insert Picture Description
the test is successful

to sum up

Build process encountered many problems, not to say how difficult configuration, but before the NIC problems, has been unable to lead the restart, the last way is to reinstall the Centos, hundred percent solve the problem, this time to learn here , this study only set up and assigned the same network segment DHCP, next to sum up in a different network segment, how DHCP should be configured.

Published 71 original articles · won praise 80 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_43431158/article/details/104516824