前言:
现时你们对“ubuntu2004配置dhcp”大体比较珍视,你们都想要学习一些“ubuntu2004配置dhcp”的相关知识。那么小编同时在网上网罗了一些对于“ubuntu2004配置dhcp””的相关文章,希望各位老铁们能喜欢,姐妹们一起来学习一下吧!五、DHCP作用域
教学案例一、配置一个作用域,用于为本地局域网中的计算机发放IP信息。要求:
本地网段:192.168.11.0/24
发放IP地址:192.168.11.153--252
网关:192.168.11.254
DNS1:202.106.0.20
DNS2:114.114.114.114
默认租约为两个小时
最大租约为3个小时
本DHCP服务器为本地权威DHCP,要求可以本地所有计算机获得IP都是由本DHCP发放
5.1)DHCP服务配置
[root@localhost dhcp]# cat /etc/dhcp/dhcpd.confoption domain-name-servers 202.106.0.20, 114.114.114.114;#声明DNS服务器default-lease-time 7200; #定义默认租约时间max-lease-time 10800; #定义最大租约时间authoritative; #拒绝不正确的IP地址的要求log-facility local7; #定义日志subnet 192.168.11.0 netmask 255.255.255.0 { range 192.168.11.153 192.168.11.252; option routers 192.168.11.254; option broadcast-address 192.168.11.255; default-lease-time 7200; max-lease-time 10800;}请根据4.2中的讲解理解配置文件内容。5.2)重启DHCP服务,生效配置
#重启dhcpd服务[root@localhost dhcp]# systemctl restart dhcpd#查看启动情况,同时也验证了客户端使用的是68端口,服务端使用的是67端口[root@localhost dhcp]# lsof -i :68COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEdhclient 55234 root 6u IPv4 110700 0t0 UDP *:bootpc [root@localhost dhcp]# lsof -i :67COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAMEdnsmasq 1507 nobody 3u IPv4 27149 0t0 UDP *:bootps dhcpd 56570 dhcpd 7u IPv4 129157 0t0 UDP *:bootps5.3)测试IP分发
打开一个客户端机器,IP获得方式为自动获取,测试是否获得到了自己这个DHCP服务器发放的IP地址。本例子中测试机使用了centos 8系统。来看下测试结果吧!
1) 查看一下当前eth0的IP地址、MAC地址,并保证其IP获得方式为:DHCP[root@test 桌面]# ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:0C:29:1A:F8:BD inet addr:172.16.44.132 Bcast:172.16.44.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1a:f8bd/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:86 errors:0 dropped:0 overruns:0 frame:0 TX packets:63 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:15294 (14.9 KiB) TX bytes:6769 (6.6 KiB)2)使用dhclient命令来获得IP,看一下重要输出[root@test 桌面]# dhclient -d eth0Internet Systems Consortium DHCP Client 4.1.1-P1Copyright 2004-2010 Internet Systems Consortium.All rights reserved.For info, please visit on LPF/eth0/00:0c:29:1a:f8:bdSending on LPF/eth0/00:0c:29:1a:f8:bdSending on Socket/fallbackDHCPDISCOVER on eth0 to 255.255.255.255 port 67 interval 6 (xid=0x316768c3) 发广播寻找DHCP服务器DHCPOFFER from 192.168.11.16 192.168.11.16DHCP服务器应答DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x316768c3) client向服务器请求IP地址DHCPACK from 192.168.11.16 (xid=0x316768c3) 确认租赁关系bound to 192.168.11.156 -- renewal in 2983 seconds. client分得IP:192.168.11.156注意:看到这些信息后,按CTRL+C退出。dhclient是一个DHCP协议客户端,它使用DHCP协议或者BOOTP协议或在这两个协议都不可用时使用静态地址来配置一个或多个网络接口dhclient -r 释放IP地址dhclient -d 强制dhclient作为前台进程运行。 通常情况下,DHCP客户端将在前台运行,直到配置了一个接口,此时它将恢复为在后 台运行。 3) 服务器日志查看验证获取信息[root@localhost ~]# tailf /var/log/messagesFeb 21 13:40:44 baism dhcpd: DHCPDISCOVER from 00:0c:29:1a:f8:bd via ens33Feb 21 13:40:45 baism dhcpd: DHCPOFFER on 192.168.11.156 to 00:0c:29:1a:f8:bd via ens33Feb 21 13:40:45 baism dhcpd: DHCPREQUEST for 192.168.11.156 (192.168.11.16) from 00:0c:29:1a:f8:bd via ens33Feb 21 13:40:45 baism dhcpd: DHCPACK on 192.168.11.156 to 00:0c:29:1a:f8:bd via ens334) 在client上通过ifconfig命令再次查看eth0 IP地址,验证是否为192.168.11.156[root@test 桌面]# ifconfig eth0eth0 Link encap:Ethernet HWaddr 00:0C:29:1A:F8:BD inet addr:192.168.11.156 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1a:f8bd/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:97 errors:0 dropped:0 overruns:0 frame:0 TX packets:67 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:17364 (16.9 KiB) TX bytes:7537 (7.3 KiB)5)查看网关,确定网关为192.168.11.254[root@test 桌面]# route -nKernel IP routing tableDestination Gateway Genmask Flags Metric Ref Use Iface192.168.11.0 0.0.0.0 255.255.255.0 U 0 0 0 eth1192.168.11.0 0.0.0.0 255.255.255.0 U 0 0 0 eth0169.254.0.0 0.0.0.0 255.255.0.0 U 1003 0 0 eth10.0.0.0 192.168.11.254 0.0.0.0 UG 0 0 0 eth06)查看DNS配置文件,看DNS是否为DHCP服务器发放的DNS服务器IP[root@test 桌面]# cat /etc/resolv.conf ; generated by /sbin/dhclient-scriptnameserver 202.106.0.20nameserver 114.114.114.1145.4)通过DHCP服务器租约文件查看具体租约
租约文件的路径: /var/lib/dhcpd/dhcpd.leases
[root@localhost ~]# cat /var/lib/dhcpd/dhcpd.leases# The format of this file is documented in the dhcpd.leases(5) manual page.# This lease file was written by isc-dhcp-4.3.6# authoring-byte-order entry is generated, DO NOT DELETEauthoring-byte-order little-endian;server-duid "\000\001\000\001%\257\376\022\000\014)c.\345";lease 192.168.11.153 { starts 2 2020/01/14 04:21:04; #开始时间 ends 2 2020/01/14 06:21:04; #结束时间 cltt 2 2020/01/14 04:21:04; binding state active; next binding state free; rewind binding state free; hardware ethernet 00:0c:29:6d:1c:b3; uid "\001\000\014)m\034\263";}注意:当你发现这里的时间和你的服务器时间不一致的时候,建议你修改时区解决问题,一般是差8个小时,大家明白就好。5.5)保留IP
在IP租约到期后,如果无法续订租约,client只能乖乖交出IP地址,重新获得一个其他IP使用。但是在公司有些服务器的IP地址是不能变化的,因为变了用户就无法连接到服务器了,比如公司文件服务器、打印服务器等等。那么在这种环境中我们既想使用DHCP管理公司IP,又想实现部分机器的IP永久不变,那么怎么实现呢。
DHCP的作者在写DHCP的时候也想到了这个问题,提出了保留IP的概念,就是将某些IP保留,然后服务器来获得IP的时候,根据其MAC地址做匹配,将对应的IP分给它即可。
教学案例:希望这个MAC地址为00:0C:29:1A:F8:C7的网卡能永久获得IP 192.168.11.252,实现方式如下:
a、在配置文件/etc/dhcp/dhcpd.conf末尾添加以下内容 host print { hardware ethernet 00:0C:29:1A:F8:C7; fixed-address 192.168.11.252;}host print host为指令,print是个名字,随便起,但是最好有意义,要不过一段你也记不住了。hardware ethernet 指定以太网网卡MAC地址fixed-address 指定要绑定的IPb、重启DHCP服务[root@localhost ~]# systemctl restart dhcpdc、测试,登陆测试机,释放挡墙IP,重新获得新的IP,查看IP地址是否正确分发[root@test 桌面]# ifconfig eth1eth1 Link encap:Ethernet HWaddr 00:0C:29:1A:F8:C7 inet addr:192.168.11.155 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1a:f8c7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4071 errors:0 dropped:0 overruns:0 frame:0 TX packets:187 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:433880 (423.7 KiB) TX bytes:16888 (16.4 KiB)[root@test 桌面]# dhclient -r eth1[root@test 桌面]# dhclient -d eth1Internet Systems Consortium DHCP Client 4.1.1-P1Copyright 2004-2010 Internet Systems Consortium.All rights reserved.For info, please visit on LPF/eth1/00:0c:29:1a:f8:c7Sending on LPF/eth1/00:0c:29:1a:f8:c7Sending on Socket/fallbackDHCPDISCOVER on eth1 to 255.255.255.255 port 67 interval 4 (xid=0x45c162c2)DHCPOFFER from 192.168.11.16DHCPREQUEST on eth1 to 255.255.255.255 port 67 (xid=0x45c162c2)DHCPACK from 192.168.11.16 (xid=0x45c162c2)bound to 192.168.11.252 -- renewal in 2881 seconds.^C[root@test 桌面]# ifconfig eth1eth1 Link encap:Ethernet HWaddr 00:0C:29:1A:F8:C7 inet addr:192.168.11.252 Bcast:192.168.11.255 Mask:255.255.255.0 inet6 addr: fe80::20c:29ff:fe1a:f8c7/64 Scope:Link UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:4081 errors:0 dropped:0 overruns:0 frame:0 TX packets:191 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:436337 (426.1 KiB) TX bytes:17656 (17.2 KiB)完美!总结
按需求配置DHCP服务
配置文件如何修改
DHCP使用的端口号是多少
如何测试验证
如何查看日志信息
重点:配置文件的修改,端口号的查看,验证的方式
难点:需要操作两台主机,一定要搞清楚他们的角色,否则很容易将操作混淆
标签: #ubuntu2004配置dhcp