本文共 7920 字,大约阅读时间需要 26 分钟。
1.安装dhcp软件
注意:安装时需要有配置好的yum仓库,关于yum仓库的配置,祥见Linux下系统的软件安装以及yum源配置(yum仓库搭建、yum命令、rpm命令)
[root@servser ~]# yum install dhcp -y
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.2.5-27.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
dhcp x86_64 12:4.2.5-27.el7 rhel7.0 506 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 506 k
Installed size: 1.4 M
Downloading packages:
dhcp-4.2.5-27.el7.x86_64.rpm | 506 kB 00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 12:dhcp-4.2.5-27.el7.x86_64 1/1
Verifying : 12:dhcp-4.2.5-27.el7.x86_64 1/1
Installed:
dhcp.x86_64 12:4.2.5-27.el7
Complete!
2.配置动态网络
注意:配置防火墙关闭或者将dhcp服务加入防火墙白名单,详见LInux下防火墙的基本管理(查看、火墙伪装、添加防火墙白名单)
[root@servser ~]# systemctl start dhcpd ##安装完成后打开dhcpd服务
[root@servser ~]# systemctl status dhcpd.service ##可以看到,服务打不开
dhcpd.service - DHCPv4 Server Daemon
Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled)
Active: failed (Result: exit-code) since Mon 2019-02-18 11:52:33 EST; 13s ago
Docs: man:dhcpd(8)
man:dhcpd.conf(5)
Process: 31524 ExecStart=/usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -group dhcpd --no-pid (code=exited, status=1/FAILURE)
Main PID: 31524 (code=exited, status=1/FAILURE)
Feb 18 11:52:33 servser.westos.com dhcpd[31524]: you want, please write a su...n
Feb 18 11:52:33 servser.westos.com dhcpd[31524]: in your dhcpd.conf file for...t
Feb 18 11:52:33 servser.westos.com dhcpd[31524]: to which interface eth0 is ...*
Feb 18 11:52:33 servser.westos.com dhcpd[31524]: Feb 18 11:52:33 servser.westos.com dhcpd[31524]: Feb 18 11:52:33 servser.westos.com dhcpd[31524]: Not configured to listen on...!
Feb 18 11:52:33 servser.westos.com dhcpd[31524]: Feb 18 11:52:33 servser.westos.com dhcpd[31524]: This version of ISC DHCP is...e
Feb 18 11:52:33 servser.westos.com systemd[1]: dhcpd.service: main process e...E
Feb 18 11:52:33 servser.westos.com systemd[1]: Unit dhcpd.service entered fa....
Hint: Some lines were ellipsized, use -l to show in full.
[root@servser ~]# vim /etc/dhcp/dhcpd.conf ##打开配置文件,发现配置文件为空,需要查看模板
[root@servser ~]# cat /etc/dhcp/dhcpd.conf
#
# DHCP Server Configuration file.
# see /usr/share/doc/dhcp*/dhcpd.conf.example
# see dhcpd.conf(5) man page
#
[root@servser ~]# cp /usr/share/doc/dhcp*/dhcpd.conf.example /etc/dhcp/dhcpd.conf ##复制模板过来
cp: overwrite ‘/etc/dhcp/dhcpd.conf’? y
[root@servser ~]# vim /etc/dhcp/dhcpd.conf ##编辑动态网络配置文件
##写入:
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
# option definitions common to all supported networks...
option domain-name "westos.com";
option domain-name-servers 172.25.68.100;
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.
# This is a very basic subnet declaration.
subnet 172.25.68.0 netmask 255.255.255.0 {
range 172.25.68.101 172.25.68.200;
option routers 172.25.68.100;
}
##其中:
option domain-name "westos.com";
option domain-name-servers 172.25.68.100;
DNS,配置获取到网络的DNS服务域名和域名服务器的IP
default-lease-time 600;
默认租约时间
max-lease-time 7200;
最大租约时间
subnet 172.25.68.0
netmask 255.255.255.0
子网掩码
range 172.25.68.101 172.25.68.200;
可获取的动态IP范围
option routers 172.25.68.100;
获取到网络的网关
3.测试动态网络源:
[root@dhcp_test ~]# ifconfig ##查看当前已有的IP
eth0: flags=4163mtu 1500
inet 172.25.68.101 netmask 255.255.255.0 broadcast 172.25.68.255
inet6 fe80::5054:ff:fe00:440b prefixlen 64 scopeid 0x20
ether 52:54:00:00:44:0b txqueuelen 1000 (Ethernet)
RX packets 370 bytes 35553 (34.7 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 338 bytes 36819 (35.9 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163mtu 1500
ether 52:54:00:24:53:64 txqueuelen 1000 (Ethernet)
RX packets 156 bytes 10332 (10.0 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 0 bytes 0 (0.0 B)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10loop txqueuelen 0 (Local Loopback)
RX packets 275 bytes 22000 (21.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 275 bytes 22000 (21.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
[root@dhcp_test ~]# cd /etc/sysconfig/network-scripts/ ##切换目录
[root@dhcp_test network-scripts]# ls
ifcfg-eth0-1 ifdown-ppp ifup-eth ifup-sit
ifcfg-lo ifdown-routes ifup-ippp ifup-Team
ifdown ifdown-sit ifup-ipv6 ifup-TeamPort
ifdown-bnep ifdown-Team ifup-isdn ifup-tunnel
ifdown-eth ifdown-TeamPort ifup-plip ifup-wireless
ifdown-ippp ifdown-tunnel ifup-plusb init.ipv6-global
ifdown-ipv6 ifup ifup-post network-functions
ifdown-isdn ifup-aliases ifup-ppp network-functions-ipv6
ifdown-post ifup-bnep ifup-routes
[root@dhcp_test network-scripts]# cp ifcfg-eth0-1 ifcfg-eth1 ##复制配置文件
[root@dhcp_test network-scripts]# vim ifcfg-eth1 ##编辑配置文件
##写入
TYPE=Ethernet
BOOTPROTO=dhcp
NAME=eth1
ONBOOT=yes
DEVICE=eth1
[root@dhcp_test network-scripts]# systemctl restart network重启网络服务
[root@dhcp_test network-scripts]# ifconfig
eth0: flags=4163mtu 1500
inet 172.25.68.101 netmask 255.255.255.0 broadcast 172.25.68.255
inet6 fe80::5054:ff:fe00:440b prefixlen 64 scopeid 0x20
ether 52:54:00:00:44:0b txqueuelen 1000 (Ethernet)
RX packets 1469 bytes 130431 (127.3 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 1048 bytes 121340 (118.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth1: flags=4163mtu 1500
inet 172.25.68.108 netmask 255.255.255.0 broadcast 172.25.68.255 ##可以看到eth1成功获得到了动态IP
inet6 fe80::5054:ff:fe24:5364 prefixlen 64 scopeid 0x20
ether 52:54:00:24:53:64 txqueuelen 1000 (Ethernet)
RX packets 360 bytes 20100 (19.6 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 22 bytes 3891 (3.7 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10loop txqueuelen 0 (Local Loopback)
RX packets 275 bytes 22000 (21.4 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 275 bytes 22000 (21.4 KiB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
##查看日志也可以看到动态IP获取的来源
Feb 18 12:50:43 localhost NetworkManager: DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0x410b580d)
Feb 18 12:50:43 localhost dhclient[4482]: DHCPACK from 172.25.68.100 (xid=0x410b580d)
Feb 18 12:50:43 localhost NetworkManager: DHCPACK from 172.25.68.100 (xid=0x410b580d)
Feb 18 12:50:43 localhost dhclient[4482]: bound to 172.25.68.108 -- renewal in 234 seconds.
Feb 18 12:50:43 localhost NetworkManager: bound to 172.25.68.108 -- renewal in 234 seconds.
Feb 18 12:50:43 localhost NetworkManager[664]: (eth1): DHCPv4 state changed preinit -> reboot
Feb 18 12:50:43 localhost NetworkManager[664]: address 172.25.68.108
Feb 18 12:50:43 localhost NetworkManager[664]: plen 24 (255.255.255.0)
Feb 18 12:50:43 localhost NetworkManager[664]: gateway 172.25.68.100
Feb 18 12:50:43 localhost NetworkManager[664]: server identifier 172.25.68.100
Feb 18 12:50:43 localhost NetworkManager[664]: lease time 600
Feb 18 12:50:43 localhost NetworkManager[664]: nameserver '172.25.68.100'
Feb 18 12:50:43 localhost NetworkManager[664]: domain name 'westos.com'
转载地址:http://snyzo.baihongyu.com/