分配固定IP地址
在DHCP动态主机地址管理协议中有个术语叫做“预约”,预约指的是保证局域网中特定的设备总是获取到固定的IP地址,换句话说就是dhcpd服务程序会把某个IP地址私藏下来,只有匹配到特定主机了才会拿出来分配,让某个特定主机总能获取到固定的IP地址。详情请关注《linux就该这么学》。
操作方法
- 01
要想把IP地址与某个主机相互绑定,那么需要该主机的MAC网卡物理地址才可以,MAC地址是网卡上面的一串独立标识符,因此不用担心冲突的情况,如图所示,咱们在Linux系统或Windows系统中都可以通过查看网卡状态来查看到这个MAC值。
- 02
此时一定有同学会问如果不方便查看MAC地址怎么办呢?比如给公司老板的机器绑定IP地址,总不能随便就去看别人电脑信息吧,刘遄老师就来告诉大家一个很好的办法,首先咱们启动dhcpd服务程序,然后为老板的主机分配一个IP地址,这样在DHCP服务器本地的日志文件中就会保存有了这次的IP地址分配记录,通过看日志文件来获取到对方电脑的网卡MAC地址啦~ [root@linuxprobe ~]# tail -f /var/log/messages Mar 30 05:33:17 localhost dhcpd: Copyright 2004-2013 Internet Systems Consortium. Mar 30 05:33:17 localhost dhcpd: All rights reserved. Mar 30 05:33:17 localhost dhcpd: For info, please visit https://www.isc.org/software/dhcp/ Mar 30 05:33:17 localhost dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file Mar 30 05:33:17 localhost dhcpd: Wrote 0 leases to leases file. Mar 30 05:33:17 localhost dhcpd: Listening on LPF/eno16777728/00:0c:29:c4:a4:09/192.168.10.0/24 Mar 30 05:33:17 localhost dhcpd: Sending on LPF/eno16777728/00:0c:29:c4:a4:09/192.168.10.0/24 Mar 30 05:33:17 localhost dhcpd: Sending on Socket/fallback/fallback-net Mar 30 05:33:26 localhost dhcpd: DHCPDISCOVER from 00:0c:29:27:c6:12 via eno16777728 Mar 30 05:33:27 localhost dhcpd: DHCPOFFER on 192.168.10.50 to 00:0c:29:27:c6:12 (WIN-APSS1EANKLR) via eno16777728 Mar 30 05:33:29 localhost dhcpd: DHCPDISCOVER from 00:0c:29:27:c6:12 (WIN-APSS1EANKLR) via eno16777728 Mar 30 05:33:29 localhost dhcpd: DHCPOFFER on 192.168.10.50 to 00:0c:29:27:c6:12 (WIN-APSS1EANKLR) via eno16777728 Mar 30 05:33:29 localhost dhcpd: DHCPREQUEST for 192.168.10.50 (192.168.10.10) from 00:0c:29:27:c6:12 (WIN-APSS1EANKLR) via eno16777728 Mar 30 05:33:29 localhost dhcpd: DHCPACK on 192.168.10.50 to 00:0c:29:27:c6:12 (WIN-APSS1EANKLR) via eno16777728
- 03
以前我在线下讲课的时候讲完DHCP服务后看到底下总是会有学生挠头,起初我很不理解,因为dhcpd服务程序应该算是Linux系统中最简单不过的了,就那么点参数怎么可能写错呢?后来慢慢发现了原因——有些同学是以Windows系统为对象做的IP绑定实验,而在Windows系统中直接查看到的MAC地址是类似于00-0c-29-27-c6-12这样的,很明显MAC地址虽然值是一样的,但间隔符变成了-(减号),因此咱们在Linux系统中配置dhcpd服务程序的时候一定要保证里面的MAC地址都是以:(冒号)来间隔的哦~ [root@linuxprobe ~]# vim /etc/dhcp/dhcpd.conf ddns-update-style none; ignore client-updates; subnet 192.168.10.0 netmask 255.255.255.0 { range 192.168.10.50 192.168.10.150; option subnet-mask 255.255.255.0; option routers 192.168.10.1; option domain-name "linuxprobe.com"; option domain-name-servers 192.168.10.1; default-lease-time 21600; max-lease-time 43200; host linuxprobe { hardware ethernet 00:0c:29:27:c6:12; fixed-address 192.168.10.88; } } [root@linuxprobe ~]# systemctl restart dhcpd
- 04
确认参数填写正确后就可以保存退出配置文件,然后就可以重启dhcpd服务程序了,对了~如果这台主机是您刚刚分配过IP地址的,那么它的IP地址租约时间还没有到期,因此不会立即的更换成新绑定的IP地址,要想立即看到绑定效果还需要再重启下客户端的网络服务才行,如图所示: