1.2. DHCP服务搭建

Date:

2018-09

1.2.1. 环境准备

注意

dhcp在实际生活中很常见的一种服务。但是一般都是通过家用路由器(AP),或者企业内的三层交换机提供。 所以一般的网络都不用配置dhcp服务器。但是如果是超大型网络。出于网络规划。会设计一个dhcp。 但是这时候因为这种大型网络拓补结构都是多个网段或者多种网络类型。所以一般都需要DHCP中继。这会消耗一部分网络带宽。所以慎重应用。 常见工作应用: 配置PXE自动化安装系统的时候会用到DHCP。

1.2.1.1. dhcp服务器环境

系统环境:

 1[root@dhcp_server01 ~]# uname -n
 2dhcp_server01
 3[root@dhcp_server01 ~]# uname -r
 42.6.32-504.el6.x86_64
 5[root@dhcp_server01 ~]# uname -i
 6x86_64
 7[root@dhcp_server01 ~]# uname -o
 8GNU/Linux
 9[root@dhcp_server01 ~]# cat /etc/sysconfig/network
10NETWORKING=yes
11HOSTNAME=dhcp_server01

网络环境:

注意

DHCP服务器需要配置静态IP,而且需要配置网卡开机自启动

 1[root@dhcp_server01 ~]# ifconfig
 2eth0      Link encap:Ethernet  HWaddr 00:0C:29:11:56:AC
 3        inet addr:192.168.161.137  Bcast:192.168.161.255  Mask:255.255.255.0
 4        inet6 addr: fe80::20c:29ff:fe11:56ac/64 Scope:Link
 5        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6        RX packets:215868 errors:0 dropped:0 overruns:0 frame:0
 7        TX packets:29843 errors:0 dropped:0 overruns:0 carrier:0
 8        collisions:0 txqueuelen:1000
 9        RX bytes:272259368 (259.6 MiB)  TX bytes:2479551 (2.3 MiB)
10
11eth1      Link encap:Ethernet  HWaddr 00:0C:29:11:56:B6
12        inet addr:192.168.6.10  Bcast:192.168.6.255  Mask:255.255.255.0
13        inet6 addr: fe80::20c:29ff:fe11:56b6/64 Scope:Link
14        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
15        RX packets:536902 errors:0 dropped:0 overruns:0 frame:0
16        TX packets:3467335 errors:0 dropped:0 overruns:0 carrier:0
17        collisions:0 txqueuelen:1000
18        RX bytes:34209491 (32.6 MiB)  TX bytes:5161363668 (4.8 GiB)
19
20lo        Link encap:Local Loopback
21        inet addr:127.0.0.1  Mask:255.0.0.0
22        inet6 addr: ::1/128 Scope:Host
23        UP LOOPBACK RUNNING  MTU:65536  Metric:1
24        RX packets:14 errors:0 dropped:0 overruns:0 frame:0
25        TX packets:14 errors:0 dropped:0 overruns:0 carrier:0
26        collisions:0 txqueuelen:0
27        RX bytes:1161 (1.1 KiB)  TX bytes:1161 (1.1 KiB)

其中DHCP服务器的eth0是通过dhcp获取的IP,可以方便管理使用。工作中一般就一个网卡即可,eth1是静态IP,这个网卡用来做DHCP的服务器端口。

工作中就只用这个静态IP的配置即可实现dhcp服务器部署。

 1[root@dhcp_server01 ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth1
 2DEVICE=eth1
 3#HWADDR=00:0C:29:11:56:B6
 4TYPE=Ethernet
 5#UUID=15ee2fe0-2e6e-4f6f-83a3-ef7fbe466508
 6ONBOOT=yes
 7NM_CONTROLLED=yes
 8BOOTPROTO=static
 9IPADDR=192.168.6.10
10NETMASK=255.255.255.0
11#GATEWAY=192.168.6.1

1.2.1.2. dhcp客户端环境

系统环境和dhcp服务器一致,此处dhcp客户端程序系统都默认自动集成安装,所以不用单独安装。

DHCP客户端的网卡也是使用两个网卡,方便实验使用。工作中只有一个网卡,网卡配置通过DHCP获取IP,则网卡启动后会自动通过 DHCP协议获取IP地址。

客户端的网络配置信息:

 1[root@dhcpclient ~]# uname -n
 2dhcpclient
 3[root@dhcpclient ~]# cat /etc/sysconfig/network
 4NETWORKING=yes
 5HOSTNAME=dhcpclient
 6[root@dhcpclient ~]# ifconfig
 7eth0      Link encap:Ethernet  HWaddr 00:0C:29:F0:8E:33
 8        inet6 addr: fe80::20c:29ff:fef0:8e33/64 Scope:Link
 9        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
10        RX packets:17 errors:0 dropped:0 overruns:0 frame:0
11        TX packets:12 errors:0 dropped:0 overruns:0 carrier:0
12        collisions:0 txqueuelen:1000
13        RX bytes:1866 (1.8 KiB)  TX bytes:2520 (2.4 KiB)
14
15eth1      Link encap:Ethernet  HWaddr 00:0C:29:F0:8E:3D
16        inet addr:192.168.161.134  Bcast:192.168.161.255  Mask:255.255.255.0
17        inet6 addr: fe80::20c:29ff:fef0:8e3d/64 Scope:Link
18        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
19        RX packets:691 errors:0 dropped:0 overruns:0 frame:0
20        TX packets:298 errors:0 dropped:0 overruns:0 carrier:0
21        collisions:0 txqueuelen:1000
22        RX bytes:67924 (66.3 KiB)  TX bytes:53465 (52.2 KiB)
23
24lo        Link encap:Local Loopback
25        inet addr:127.0.0.1  Mask:255.0.0.0
26        inet6 addr: ::1/128 Scope:Host
27        UP LOOPBACK RUNNING  MTU:65536  Metric:1
28        RX packets:0 errors:0 dropped:0 overruns:0 frame:0
29        TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
30        collisions:0 txqueuelen:0
31        RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
32
33[root@dhcpclient ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
34DEVICE=eth0
35TYPE=Ethernet
36ONBOOT=yes
37NM_CONTROLLED=yes
38BOOTPROTO=dhcp
39#HWADDR=00:0C:29:C2:EF:FB
40DEFROUTE=yes
41PEERDNS=yes
42PEERROUTES=yes
43IPV4_FAILURE_FATAL=yes
44IPV6INIT=no
45NAME="System eth0"

1.2.1.3. dhcp安装系统及软件信息

软件:

dhcp-common-4.1.1-61.P1.el6.centos.x86_64 dhcp-4.1.1-61.P1.el6.centos.x86_64

备注

dhcp-common默认系统已经安装。所以只用安装dhcp软件包即可。

1.2.1.4. 关闭vmware的dhcp功能

默认情况下vmware会提供dhcp功能,为了不影响我们后续的自己搭建dhcp功能,建议先关闭vmware的dhcp功能。

关闭vmwaredhcp功能具体步骤: 【菜单栏】->【编辑】->【虚拟网络编辑器】->【更改设置】,如下图:

设置虚拟机的网络方式具体步骤: 【虚拟机右键】->【虚拟机设置】->【网络适配器】,如下图:

备注

我这个主机是最小化安装的,环境都没有配置,下面有写环境配置相关的, 自行跳过。

1.2.1.5. 网络时间同步

注意

如果时间没有和网络同步,yum安装会报错。

参考:

14: Peer cert cannot be verified or peer cert invalid

1[root@zzjlogin ~]# date
2Thu Sep  6 21:07:25 CST 2018
3[root@zzjlogin ~]# ntpdate pool.ntp.org
428 Sep 00:53:38 ntpdate[1577]: step time server 5.103.139.163 offset 1827966.915121 sec

1.2.1.6. 关闭selinux

注意

如果不关闭selinux也没有配置selinux。则安装以后zabbix会启动失败。会发现zabbix网页可以访问,但是提示zabbix服务没有启动。

永久关闭:

下面配置会让selinux的关闭重启系统后还是关闭状态。但是配置不会立即生效。

注意

通过 source /etc/selinux/config 也不能让修改的文件立即生效。所以需要下面的临时关闭的方式结合使用。

1[root@zzjlogin ~]# sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
2[root@zzjlogin ~]# grep SELINUX /etc/selinux/config
3# SELINUX= can take one of these three values:
4SELINUX=disabled
5# SELINUXTYPE= can take one of these two values:
6SELINUXTYPE=targeted
临时关闭:

下面配置是立即生效,但是系统重启后会失效。

1[root@zzjlogin ~]# getenforce
2Enforcing
3[root@zzjlogin ~]# setenforce 0
4[root@zzjlogin ~]# getenforce
5Permissive

1.2.1.7. 关闭防火墙

注意

防火墙一般都是关闭。如果不不关闭,也可以通过配置规则允许所有使用的端口被访问。

1[root@zzjlogin ~]# /etc/init.d/iptables stop
2iptables: Setting chains to policy ACCEPT: filter          [  OK  ]
3iptables: Flushing firewall rules:                         [  OK  ]
4iptables: Unloading modules:                               [  OK  ]

关闭防火墙开机自启动

1[root@zzjlogin ~]# chkconfig iptables off

1.2.1.8. 系统准备命令集合

1ntpdate pool.ntp.org
2sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
3setenforce 0
4/etc/init.d/iptables stop
5chkconfig iptables off

1.2.1.9. 配置yum源

注意

yum方式安装DHCP,默认的yum源是国外的安装程序比较慢,一般会换成国内的 163 ali sohu 等yum源。

小技巧

本实例安装是用的本地光驱挂在的系统镜像的中的软件包目录作为yum源。

主要步骤:
  • 备份系统自带的源配置文件

  • 新建cdrom的源配置文件,并配置

  • 挂载本地镜像

  • 清空yum缓存

  • 生成新的yum缓存

  • 可以正常使用这个新的yum源安装软件

 1[root@dhcp_server01 ~]# cd /etc/yum.repos.d/
 2[root@dhcp_server01 yum.repos.d]# ls
 3CentOS-Base.repo       CentOS-fasttrack.repo  CentOS-Vault.repo
 4CentOS-Debuginfo.repo  CentOS-Media.repo
 5[root@dhcp_server01 yum.repos.d]# mkdir bak
 6[root@dhcp_server01 yum.repos.d]# mv *.repo bak/
 7[root@dhcp_server01 yum.repos.d]# ls
 8bak
 9[root@dhcp_server01 yum.repos.d]# cat >>cdrom.repo<<EOF
10> [base]
11> name=base
12> baseurl=file:///mnt
13> gpgcheck=0
14> enabled=1
15> EOF
16[root@dhcp_server01 yum.repos.d]# cat cdrom.repo
17[base]
18name=base
19baseurl=file:///mnt
20gpgcheck=0
21enabled=1

挂载本地镜像:

1[root@dhcp_server01 yum.repos.d]# mount /dev/cdrom /mnt
2mount: block device /dev/sr0 is write-protected, mounting read-only
3[root@dhcp_server01 yum.repos.d]# cd
4[root@dhcp_server01 ~]# ls /mnt/
5CentOS_BuildTag  EULA  images    Packages                  repodata              RPM-GPG-KEY-CentOS-Debug-6     RPM-GPG-KEY-CentOS-Testing-6
6EFI              GPL   isolinux  RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-6  RPM-GPG-KEY-CentOS-Security-6  TRANS.TBL

清空缓存,生成yum缓存:

1[root@dhcp_server01 yum.repos.d]# yum clean all
2Loaded plugins: fastestmirror, security
3Cleaning repos: base
4Cleaning up Everything
5Cleaning up list of fastest mirrors
6[root@dhcp_server01 yum.repos.d]# yum makecache

1.2.1.10. vim安装

根据自己需要选择是否安装vim。

备注

默认的vi编辑器用起来有时候觉得不方便。所以一般都更新为vim。

1[root@dhcp_server01 yum.repos.d]# yum install vim -y

1.2.2. dhcp的安装

 1[root@dhcp_server01 ~]# rpm -qa dhcp*
 2dhcp-common-4.1.1-43.P1.el6.centos.x86_64
 3[root@dhcp_server01 ~]# yum install dhcp -y
 4Loaded plugins: fastestmirror, security
 5Setting up Install Process
 6Loading mirror speeds from cached hostfile
 7 * c6-media: 
 8Resolving Dependencies
 9--> Running transaction check
10---> Package dhcp.x86_64 12:4.1.1-43.P1.el6.centos will be installed
11--> Finished Dependency Resolution
12
13Dependencies Resolved
14
15=============================================================================================================================================================================
16 Package                           Arch                                Version                                                   Repository                             Size
17=============================================================================================================================================================================
18Installing:
19 dhcp                              x86_64                              12:4.1.1-43.P1.el6.centos                                 c6-media                              819 k
20
21Transaction Summary
22=============================================================================================================================================================================
23Install       1 Package(s)
24
25Total download size: 819 k
26Installed size: 1.9 M
27Downloading Packages:
28Running rpm_check_debug
29Running Transaction Test
30Transaction Test Succeeded
31Running Transaction
32Warning: RPMDB altered outside of yum.
33  Installing : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                                                                                                                     1/1 
34  Verifying  : 12:dhcp-4.1.1-43.P1.el6.centos.x86_64                                                                                                                     1/1 
35
36Installed:
37  dhcp.x86_64 12:4.1.1-43.P1.el6.centos                                                                                                                                      
38
39Complete!
40[root@dhcp_server01 ~]# rpm -qa dhcp*      
41dhcp-4.1.1-43.P1.el6.centos.x86_64
42dhcp-common-4.1.1-43.P1.el6.centos.x86_64

1.2.3. dhcp目录结构说明

查看dhcp安装所关联的目录:

 1[root@dhcp_server01 ~]# rpm -ql dhcp
 2/etc/dhcp
 3/etc/dhcp/dhcpd.conf
 4/etc/dhcp/dhcpd6.conf
 5/etc/openldap/schema/dhcp.schema
 6/etc/portreserve/dhcpd
 7/etc/rc.d/init.d/dhcpd
 8/etc/rc.d/init.d/dhcpd6
 9/etc/rc.d/init.d/dhcrelay
10/etc/rc.d/init.d/dhcrelay6
11/etc/sysconfig/dhcpd
12/etc/sysconfig/dhcpd6
13/etc/sysconfig/dhcrelay
14/etc/sysconfig/dhcrelay6
15/usr/bin/omshell
16/usr/sbin/dhcpd
17/usr/sbin/dhcrelay
18/usr/share/doc/dhcp-4.1.1
19/usr/share/doc/dhcp-4.1.1/3.0b1-lease-convert
20/usr/share/doc/dhcp-4.1.1/IANA-arp-parameters
21/usr/share/doc/dhcp-4.1.1/README.ldap
22/usr/share/doc/dhcp-4.1.1/api+protocol
23/usr/share/doc/dhcp-4.1.1/dhclient-tz-exithook.sh
24/usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap
25/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
26/usr/share/doc/dhcp-4.1.1/dhcpd6.conf.sample
27/usr/share/doc/dhcp-4.1.1/draft-ietf-dhc-ldap-schema-01.txt
28/usr/share/doc/dhcp-4.1.1/ms2isc
29/usr/share/doc/dhcp-4.1.1/ms2isc/Registry.perlmodule
30/usr/share/doc/dhcp-4.1.1/ms2isc/ms2isc.pl
31/usr/share/doc/dhcp-4.1.1/ms2isc/readme.txt
32/usr/share/doc/dhcp-4.1.1/sethostname.sh
33/usr/share/doc/dhcp-4.1.1/solaris.init
34/usr/share/man/man1/omshell.1.gz
35/usr/share/man/man5/dhcpd.conf.5.gz
36/usr/share/man/man5/dhcpd.leases.5.gz
37/usr/share/man/man8/dhcpd.8.gz
38/usr/share/man/man8/dhcrelay.8.gz
39/var/lib/dhcpd
40/var/lib/dhcpd/dhcpd.leases
41/var/lib/dhcpd/dhcpd6.leases
dhcp配置目录
  • /etc/dhcp

目录文件

作用

/etc/dhcp/dhcpd.conf

dhcp配置文件(ipv4)

/etc/dhcp/dhcpd6.conf

dhcp的ipv6环境的配置文件

/etc/openldap/schema/dhcp.schema ——————————————————————- ——————————————- /etc/portreserve/dhcpd =================================================================== ===========================================

dhcp启动脚本目录 /etc/rc.d/init.d/

目录文件

作用

/etc/rc.d/init.d/dhcpd

dhcp启动脚本(ipv4)

/etc/rc.d/init.d/dhcpd6

dhcp启动脚本(ipv6)

/etc/rc.d/init.d/dhcrelay

dhcp中继

/etc/rc.d/init.d/dhcrelay6

目录文件

作用

/etc/sysconfig/dhcpd

/etc/sysconfig/dhcpd6

/etc/sysconfig/dhcrelay

/etc/sysconfig/dhcrelay6

dhcp服务器软件包提供的命令
  • /usr/bin/

  • /usr/sbin/

目录文件

作用

/usr/bin/omshell

/usr/sbin/dhcpd

/usr/sbin/dhcrelay

dhcp文档目录(配置模版等文件)
  • /usr/share/doc/dhcp-4.1.1

目录文件

作用

/usr/share/doc/dhcp-4.1.1/3.0b1-lease-convert

/usr/share/doc/dhcp-4.1.1/IANA-arp-parameters

/usr/share/doc/dhcp-4.1.1/README.ldap

/usr/share/doc/dhcp-4.1.1/api+protocol

/usr/share/doc/dhcp-4.1.1/dhclient-tz-exithook.sh

/usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap

/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample

dhcp配置模版

/usr/share/doc/dhcp-4.1.1/dhcpd6.conf.sample

dhcp在IPv6环境的配置模版

/usr/share/doc/dhcp-4.1.1/draft-ietf-dhc-ldap-schema-01.txt

/usr/share/doc/dhcp-4.1.1/ms2isc

/usr/share/doc/dhcp-4.1.1/ms2isc/Registry.perlmodule

/usr/share/doc/dhcp-4.1.1/ms2isc/ms2isc.pl

/usr/share/doc/dhcp-4.1.1/ms2isc/readme.txt

/usr/share/doc/dhcp-4.1.1/sethostname.sh

/usr/share/doc/dhcp-4.1.1/solaris.init

dhcp对应命令的所有帮助手册(man帮助)

目录文件

作用

/usr/share/man/man1/omshell.1.gz

/usr/share/man/man5/dhcpd.conf.5.gz

/usr/share/man/man5/dhcpd.leases.5.gz

/usr/share/man/man8/dhcpd.8.gz

/usr/share/man/man8/dhcrelay.8.gz

dhcp运行状态记录

/var/lib/dhcpd

目录文件

作用

/var/lib/dhcpd/dhcpd.leases

dhcp分配的ip记录信息文件

/var/lib/dhcpd/dhcpd6.leases

1.2.4. dhcp默认配置

默认配置如下:

1[root@dhcp_server01 ~]# cd /etc/dhcp/
2[root@dhcp_server01 dhcp]# ls
3dhcpd6.conf  dhcpd.conf
4[root@dhcp_server01 dhcp]# cat dhcpd.conf
5#
6# DHCP Server Configuration file.
7#   see /usr/share/doc/dhcp*/dhcpd.conf.sample
8#   see 'man 5 dhcpd.conf'
9#

配置样例:

  1[root@dhcp_server01 dhcp]# cat /usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample
  2# dhcpd.conf
  3#
  4# Sample configuration file for ISC dhcpd
  5#
  6
  7# option definitions common to all supported networks...
  8option domain-name "example.org";
  9option domain-name-servers ns1.example.org, ns2.example.org;
 10
 11default-lease-time 600;
 12max-lease-time 7200;
 13
 14# Use this to enble / disable dynamic dns updates globally.
 15#ddns-update-style none;
 16
 17# If this DHCP server is the official DHCP server for the local
 18# network, the authoritative directive should be uncommented.
 19#authoritative;
 20
 21# Use this to send dhcp log messages to a different log file (you also
 22# have to hack syslog.conf to complete the redirection).
 23log-facility local7;
 24
 25# No service will be given on this subnet, but declaring it helps the
 26# DHCP server to understand the network topology.
 27
 28subnet 10.152.187.0 netmask 255.255.255.0 {
 29}
 30
 31# This is a very basic subnet declaration.
 32
 33subnet 10.254.239.0 netmask 255.255.255.224 {
 34range 10.254.239.10 10.254.239.20;
 35option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
 36}
 37
 38# This declaration allows BOOTP clients to get dynamic addresses,
 39# which we don't really recommend.
 40
 41subnet 10.254.239.32 netmask 255.255.255.224 {
 42range dynamic-bootp 10.254.239.40 10.254.239.60;
 43option broadcast-address 10.254.239.31;
 44option routers rtr-239-32-1.example.org;
 45}
 46
 47# A slightly different configuration for an internal subnet.
 48subnet 10.5.5.0 netmask 255.255.255.224 {
 49range 10.5.5.26 10.5.5.30;
 50option domain-name-servers ns1.internal.example.org;
 51option domain-name "internal.example.org";
 52option routers 10.5.5.1;
 53option broadcast-address 10.5.5.31;
 54default-lease-time 600;
 55max-lease-time 7200;
 56}
 57
 58# Hosts which require special configuration options can be listed in
 59# host statements.   If no address is specified, the address will be
 60# allocated dynamically (if possible), but the host-specific information
 61# will still come from the host declaration.
 62
 63host passacaglia {
 64hardware ethernet 0:0:c0:5d:bd:95;
 65filename "vmunix.passacaglia";
 66server-name "toccata.fugue.com";
 67}
 68
 69# Fixed IP addresses can also be specified for hosts.   These addresses
 70# should not also be listed as being available for dynamic assignment.
 71# Hosts for which fixed IP addresses have been specified can boot using
 72# BOOTP or DHCP.   Hosts for which no fixed address is specified can only
 73# be booted with DHCP, unless there is an address range on the subnet
 74# to which a BOOTP client is connected which has the dynamic-bootp flag
 75# set.
 76host fantasia {
 77hardware ethernet 08:00:07:26:c0:a5;
 78fixed-address fantasia.fugue.com;
 79}
 80
 81# You can declare a class of clients and then do address allocation
 82# based on that.   The example below shows a case where all clients
 83# in a certain class get addresses on the 10.17.224/24 subnet, and all
 84# other clients get addresses on the 10.0.29/24 subnet.
 85
 86class "foo" {
 87match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
 88}
 89
 90shared-network 224-29 {
 91subnet 10.17.224.0 netmask 255.255.255.0 {
 92    option routers rtr-224.example.org;
 93}
 94subnet 10.0.29.0 netmask 255.255.255.0 {
 95    option routers rtr-29.example.org;
 96}
 97pool {
 98    allow members of "foo";
 99    range 10.17.224.10 10.17.224.250;
100}
101pool {
102    deny members of "foo";
103    range 10.0.29.10 10.0.29.230;
104}
105}

1.2.5. dhcp简单配置

参考上面样例配置简单配置一个地址池:
  • 网关地址:192.168.6.1

  • 地址池范围:192.168.6.20-200

  • dns地址:192.168.6.1/8.8.8.8

  • IP地址默认租期:1小时

  • IP地址最长租期:1天(24小时)

注意

地址范围不要把服务器地址包含进去。

在配置文件 /etc/dhcp/dhcpd.conf 文件中追加下面内容:

1subnet 192.168.6.0 netmask 255.255.255.0 {
2    range 192.168.6.20 192.168.6.200;
3    option domain-name-servers 192.168.6.1,8.8.8.8;
4    option routers 192.168.6.1;
5    default-lease-time 3600;
6    max-lease-time 86400;
7}

1.2.6. dhcp启动和开机自启动

1.2.6.1. dhcp启动

1[root@dhcp_server01 ~]# /etc/init.d/dhcpd start
2Shutting down dhcpd:                                       [  OK  ]
3Starting dhcpd:                                            [  OK  ]

备注

dhcp的日志默认时存放在 /var/log/message 如果dhcpd启动失败,可以从/var/log/message文件的后30行获取帮助信息。

1.2.6.2. dhcp开机自启动

方法1:

1[root@dhcp_server01 ~]# chkconfig dhcpd on

方法2:

1[root@dhcp_server01 ~]# echo '#dhcp server start by zzjlogin on 20180910'>>/etc/rc.local
2[root@dhcp_server01 ~]# echo '/etc/init.d/dhcpd start >/dev/null 2&1' >>/etc/rc.local
3[root@dhcp_server01 ~]# tail -2 /etc/rc.local
4#dhcp server start by zzjlogin on 20180910
5/etc/init.d/dhcpd start >/dev/null 2&1

1.2.6.3. 检查dhcp服务

1[root@dhcp_server01 ~]# ss -lntup|grep 67|column -t
2udp  UNCONN  0  0  *:67  *:*  users:(("dhcpd",30599,7))

1.2.6.4. dhcpd的详细参数

关于dhcpd的详细配置,我们可以使用”man dhcpd.conf”命令快速获取帮助,我这里简单介绍下常用参数

1.2.7. dhcp的测试

在另外一个虚拟机里面测试

注意

这个客户端。dhclient是在dhcp客户端包含的调试命令。客户端需要也配置同一个hostonly网络。然后配置dhcp获取IP。具体信息参考下面配置和显示。

 1[root@dhcpclient ~]# dhclient -d
 2Internet Systems Consortium DHCP Client 4.1.1-P1
 3Copyright 2004-2010 Internet Systems Consortium.
 4All rights reserved.
 5For info, please visit https://www.isc.org/software/dhcp/
 6
 7Listening on LPF/eth1/00:0c:29:f0:8e:3d
 8Sending on   LPF/eth1/00:0c:29:f0:8e:3d
 9Listening on LPF/eth0/00:0c:29:f0:8e:33
10Sending on   LPF/eth0/00:0c:29:f0:8e:33
11Sending on   Socket/fallback
12DHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 5 (xid=0x4471d59e)
13DHCPOFFER from 192.168.6.10
14DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x4471d59e)
15DHCPACK from 192.168.6.10 (xid=0x4471d59e)
16bound to 192.168.6.20 -- renewal in 1462 seconds.
17DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 7 (xid=0xc9fff86)
18DHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 12 (xid=0xc9fff86)
19DHCPOFFER from 192.168.161.254
20DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0xc9fff86)
21DHCPACK from 192.168.161.254 (xid=0xc9fff86)
22bound to 192.168.161.135 -- renewal in 717 seconds

此时查看eth0网卡信息:

 1[root@dhcpclient ~]# ifconfig eth0
 2eth0      Link encap:Ethernet  HWaddr 00:0C:29:F0:8E:33
 3        inet addr:192.168.6.20  Bcast:192.168.6.255  Mask:255.255.255.0
 4        inet6 addr: fe80::20c:29ff:fef0:8e33/64 Scope:Link
 5        UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
 6        RX packets:85 errors:0 dropped:0 overruns:0 frame:0
 7        TX packets:16 errors:0 dropped:0 overruns:0 carrier:0
 8        collisions:0 txqueuelen:1000
 9        RX bytes:11061 (10.8 KiB)  TX bytes:3288 (3.2 KiB)
10
11[root@dhcpclient ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
12DEVICE=eth0
13TYPE=Ethernet
14ONBOOT=yes
15NM_CONTROLLED=yes
16BOOTPROTO=dhcp
17#HWADDR=00:0C:29:C2:EF:FB
18DEFROUTE=yes
19PEERDNS=yes
20PEERROUTES=yes
21IPV4_FAILURE_FATAL=yes
22IPV6INIT=no
23NAME="System eth0"

从上面的测试中,我们可以看出来eth0这个hostonly网卡的ip绑定了192.168.46.100这个ip,是我们dhcp服务器range的第一个ip。