1. 程式人生 > 其它 >Linux部署dhcp (照做就行)

Linux部署dhcp (照做就行)

大家好,今天來跟大家分享一下centos7如何快速安裝DHCP服務
DHCP的意思就是動態主機配置協議。作用在於為區域網內的主機自動提供地址。這樣的話。省去了手工配置的過程。避免了手工配置可能發生的錯誤。同時有效的提高工作效率,這些都是他的好處。
這裡我們使用自己的內網靜態ip
[root@localhost yum.repos.d]# ifconfig ens33
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.75.10 netmask 255.255.255.0 broadcast 192.168.75.255
inet6 fe80::5f9c:18cd:8c82:9cc8 prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:1a:26:9c txqueuelen 1000 (Ethernet)
RX packets 1400 bytes 111205 (108.5 KiB)
RX errors 0 dropped 4 overruns 0 frame 0
TX packets 364 bytes 40052 (39.1 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.把光碟連線到桌面


[root@localhost ~]# mkdir /media/cdrom
[root@localhost ~]# mount /dev/cdrom /media/cdrom/
mount: /dev/sr0 防寫,將以只讀方式掛載
[root@localhost ~]#

[root@localhost ~]# rpm -ivh /media/cdrom/Packages/dhcp-4.2.5-68.el7.centos.x86_64.rpm
準備中... ################################# [100%]
正在升級/安裝...
1:dhcp-12:4.2.5-68.el7.centos ################################# [100%]
[root@localhost ~]# //安裝DHCP服務

[root@localhost ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
# //這是dhcp的配置檔案


[root@localhost ~]# cp /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example /etc/dhcp/dhcpd.conf
cp:是否覆蓋"/etc/dhcp/dhcpd.conf"? y
[root@localhost ~]# //把dhcp的配置檔案的模板複製到dhcp.conf檔案裡 y (表示覆蓋)

[root@localhost ~]# cd /etc/dhcp/
[root@localhost dhcp]# ls
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf scripts
[root@localhost dhcp]# //檢視一下


[root@localhost ~]# cp /etc/dhcp/dhcpd.conf /opt/ //做備份

[root@localhost ~]# grep -v "^#" /etc/dhcp/dhcpd.conf > /etc/dhcp/dhcpd.conf.bak
[root@localhost ~]#

//將/etc/dhcp/dhcpd.conf檔案中的以#開頭的行給過濾掉,匯入到一個名叫/etc/dhcp/dhcpd.bak檔案當中

[root@localhost ~]# vim /etc/dhcp/dhcpd.conf.bak //編輯這個檔案
host passacaglia {
hardware ethernet 0:0:c0:5d:bd:95;
filename "vmunix.passacaglia";
server-name "toccata.fugue.com";
}

host fantasia {
hardware ethernet 08:00:07:26:c0:a5;
fixed-address fantasia.fugue.com;
}


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;
}
}
"/etc/dhcp/dhcpd.conf.bak" 71L, 1508C // 這些都是它原來的配置檔案

subnet 192.168.75.0 netmask 255.255.255.0 { //地址段,掩碼
range 192.168.75.20 192.168.75.30; //地址範圍
option domain-name-servers ns1.internal.example.org;
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; 配置這一段,根據現實情況
}

保持退出

[root@localhost ~]# cd /etc/dhcp/
[root@localhost dhcp]# ls
dhclient.d dhclient-exit-hooks.d dhcpd6.conf dhcpd.conf dhcpd.conf.bak scripts
[root@localhost dhcp]#


[root@localhost dhcp]# mv dhcpd.conf.bak dhcpd.conf
mv:是否覆蓋"dhcpd.conf"? y

[root@localhost ~]# systemctl start dhcpd //開啟範圍


[root@localhost ~]# systemctl start dhcpd
dhcpd6.service dhcpd.service
[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 六 2021-10-30 13:22:56 CST; 48s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Main PID: 5374 (dhcpd)
Status: "Dispatching packets..."
Tasks: 1
CGroup: /system.slice/dhcpd.service
└─5374 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid

10月 30 13:22:56 localhost.localdomain dhcpd[5374]: No subnet declaration for virbr0 (192.168.122.1).
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: ** Ignoring requests on virbr0. If this is not what
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: you want, please write a subnet declaration
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: in your dhcpd.conf file for the network segment
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: to which interface virbr0 is attached. **
10月 30 13:22:56 localhost.localdomain dhcpd[5374]:
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: Listening on LPF/ens33/00:0c:29:1a:26:9c/192.168.75.0/24
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: Sending on LPF/ens33/00:0c:29:1a:26:9c/192.168.75.0/24
10月 30 13:22:56 localhost.localdomain dhcpd[5374]: Sending on Socket/fallback/fallback-net
10月 30 13:22:56 localhost.localdomain systemd[1]: Started DHCPv4 Server Daemon.


在Windows上工作管理員上禁用VMwarework dhcp 以及VMwarework nat


[root@localhost ~]# systemctl stop firewalld.service //關閉防火牆


接著在客戶機上測試


乙太網介面卡 VMware Network Adapter VMnet1:

連線特定的 DNS 字尾 . . . . . . . : internal.example.org
本地連結 IPv6 地址. . . . . . . . : fe80::101e:e033:f882:a919%14
IPv4 地址 . . . . . . . . . . . . : 192.168.75.20
子網掩碼 . . . . . . . . . . . . : 255.255.255.0


實驗完成