Linux安装DHCP实现自动获取IP地址

本文档详细介绍了在CentOS系统上使用yum和rpm方式安装DHCP服务,包括安装过程、配置步骤及服务启动与测试。首先通过yum搜索并安装DHCP软件包,然后使用rpm方式卸载并重新安装。接着配置DHCP服务,编辑配置文件,并启动服务。最后,通过虚拟机测试DHCP服务,验证其能否成功分配IP地址。
摘要由CSDN通过智能技术生成

使用yum方式安装DHCP

1.使用provides搜索相应的软件包

[root@hzp000 ~]# yum provides dhcp
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
 * base: mirrors.163.com
 * epel: hk.mirrors.thegigabit.com
 * extras: mirrors.163.com
 * updates: mirrors.163.com
12:dhcp-4.2.5-82.el7.centos.x86_64 : Dynamic host configuration protocol software
Repo        : base

2.查看到与之对应的软件包为dhcp-4.2.5-82.el7.centos.x86_64,将其安装

[root@hzp000 ~]# yum -y install dhcp
Loaded plugins: fastestmirror
Repository base is listed more than once in the configuration
Repository updates is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository centosplus is listed more than once in the configuration
Loading mirror speeds from cached hostfile
...
...
...
Installed:
  dhcp.x86_64 12:4.2.5-82.el7.centos                                                                                                                             

Complete!

3.显示安装成功

使用rpm方式安装DHCP

1.使用rpm -qa命令查看是否已经安装该程序

[root@hzp000 ~]# rpm -qa | grep dhcp*
dhcp-libs-4.2.5-82.el7.centos.x86_64
dhcp-4.2.5-82.el7.centos.x86_64
dhcp-common-4.2.5-82.el7.centos.x86_64
dhclient-4.2.5-82.el7.centos.x86_64

2.查看到已经安装了DHCP软件包
3.将其卸载重新安装

[root@hzp000 ~]# rpm -e dhcp

4.挂载光驱

[root@hzp000 ~]# mkdir /mnt/sr0
[root@hzp000 ~]# mount /dev/sr0 /mnt/sr0/
mount: /dev/sr0 is write-protected, mounting read-only

5.查找DHCP软件包

[root@hzp000 ~]# ls /mnt/sr0/Packages/ | grep dhcp*
dhclient-4.2.5-68.el7.centos.x86_64.rpm
dhcp-4.2.5-68.el7.centos.x86_64.rpm
dhcp-common-4.2.5-68.el7.centos.x86_64.rpm
dhcp-libs-4.2.5-68.el7.centos.x86_64.rpm

查找到DHCP软件包
6.安装DHCP软件包

[root@localhost Packages]# rpm -ivh dhcp-4.2.5-68.el7.centos.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:dhcp-12:4.2.5-68.el7.centos      ################################# [100%]

安装成功

配置DHCP服务

1.使用rpm -ql命令列出dhcp所有套件的文件路径

[root@localhost Packages]# rpm -ql dhcp 
/etc/NetworkManager
/etc/NetworkManager/dispatcher.d
/etc/NetworkManager/dispatcher.d/12-dhcpd
/etc/dhcp/dhcpd.conf
/etc/dhcp/dhcpd6.conf
/etc/dhcp/scripts
/etc/dhcp/scripts/README.scripts
/etc/openldap/schema/dhcp.schema
/etc/sysconfig/dhcpd
/usr/bin/omshell
/usr/lib/systemd/system/dhcpd.service
/usr/lib/systemd/system/dhcpd6.service
/usr/lib/systemd/system/dhcrelay.service
/usr/sbin/dhcpd
/usr/sbin/dhcrelay
/usr/share/doc/dhcp-4.2.5
/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example
/usr/share/doc/dhcp-4.2.5/dhcpd6.conf.example
/usr/share/doc/dhcp-4.2.5/ldap
/usr/share/doc/dhcp-4.2.5/ldap/README.ldap
/usr/share/doc/dhcp-4.2.5/ldap/dhcp.schema
/usr/share/doc/dhcp-4.2.5/ldap/dhcpd-conf-to-ldap
/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
/usr/share/systemtap/tapset/dhcpd.stp
/var/lib/dhcpd
/var/lib/dhcpd/dhcpd.leases
/var/lib/dhcpd/dhcpd6.leases

可以查看到dhcp的主配置文件/etc/dhcp/dhcpd.conf以及配置文件模板/usr/share/doc/dhcp-4.2.5/dhcpd.conf.example,可参考模板配置文件进行配置文件编写
2.使用vim编辑器编辑配置文件

option domain-name "zhipenghe.com";
option domain-name-servers www.zhipenghe.com;

default-lease-time 600;
max-lease-time 7200;
subnet 192.168.100.0 netmask 255.255.255.0 {
  range 192.168.100.50 192.168.100.60;
}

保存退出
4.开启dhcp服务

[root@localhost ~]# systemctl start dhcpd
[root@localhost ~]# systemctl status dhcpd
● dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled; vendor preset: disabled)
   Active: active (running) since Sat 2021-05-08 08:23:19 EDT; 11s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 2168 (dhcpd)
   Status: "Dispatching packets..."
   CGroup: /system.slice/dhcpd.service
           └─2168 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid

May 08 08:23:19 localhost.localdomain dhcpd[2168]: Internet Systems Consortium DHCP Server 4.2.5
May 08 08:23:19 localhost.localdomain dhcpd[2168]: Copyright 2004-2013 Internet Systems Consortium.
May 08 08:23:19 localhost.localdomain dhcpd[2168]: All rights reserved.
May 08 08:23:19 localhost.localdomain dhcpd[2168]: For info, please visit https://www.isc.org/software/dhcp/
May 08 08:23:19 localhost.localdomain dhcpd[2168]: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
May 08 08:23:19 localhost.localdomain dhcpd[2168]: Wrote 0 leases to leases file.
May 08 08:23:19 localhost.localdomain dhcpd[2168]: Listening on LPF/ens33/00:0c:29:46:cf:73/192.168.100.0/24
May 08 08:23:19 localhost.localdomain dhcpd[2168]: Sending on   LPF/ens33/00:0c:29:46:cf:73/192.168.100.0/24
May 08 08:23:19 localhost.localdomain dhcpd[2168]: Sending on   Socket/fallback/fallback-net
May 08 08:23:19 localhost.localdomain systemd[1]: Started DHCPv4 Server Daemon.
[root@localhost ~]# 

测试服务

1.重新开启一台虚拟机
注意:新开启的虚拟机必须能与dhcp服务器向沪通信,否则无法进行自动获取
这里使用的是NAT模式的虚拟网络
2.使用vim编辑器编辑新开虚拟机的网卡配置文件

[root@hzp001 ~]# vim /etc/sysconfig/network-scripts/ifcfg-ens33

在配置文件中如下配置

TYPE="Ethernet"
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="ens33"
UUID="4316680c-2864-4eff-881f-f2e8feba3c48"
DEVICE="ens33"
ONBOOT="yes"
#IPADDR="192.168.100.11"
#PREFIX="24"
#GATEWAY="192.168.100.2"
#DNS1="192.168.100.2"
IPV6_PRIVACY="no"

3.重启网卡

[root@hzp001 ~]# systemctl restart network

4.查看网卡信息

[root@hzp001 ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.100.50  netmask 255.255.255.0  broadcast 192.168.100.255
        inet6 fe80::557:7f9e:4174:b607  prefixlen 64  scopeid 0x20<link>
        inet6 fe80::318:3a48:e92e:a12  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:86:d1:d1  txqueuelen 1000  (Ethernet)
        RX packets 722  bytes 68247 (66.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 552  bytes 82430 (80.4 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

可以查看到网卡已经自动获取了ip地址

附:请问有没有人知道下图依赖中最后两个依赖是啥,知道的请分享以下,感谢
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

屎到淋头还嚼便

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值