Linux中利用DHCP服务为特定设备分配固定IP
实质:实现设备与IP的绑定。
服务模式:服务器/客户端
PC1为服务器,PC2为客户端。PC1和PC2均为RHEL7系统
1、查看服务器PC1系统版本,ip信息
[root@PC1 ~]# cat /etc/redhat-release ## 查看系统版本
Red Hat Enterprise Linux Server release 7.0 (Maipo)
[root@PC1 ~]# ifconfig | head -n 3 ## 查看IP
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.10 netmask 255.255.255.255 broadcast 192.168.10.10
inet6 fe80::20c:29ff:fe66:37f7 prefixlen 64 scopeid 0x20<link>
2、安装DHCP服务 (提前配置好yum仓库)
[root@PC1 ~]# yum install dhcp
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription
Installed size: 1.4 M
Is this ok [y/d/N]: y
………………
Installed:
dhcp.x86_64 12:4.2.5-27.el7
Complete!
3、DHCP服务配置文件所在位置
[root@PC1 dhcp]# pwd
/etc/dhcp
[root@PC1 dhcp]# ls
dhclient.d dhcpd6.conf dhcpd.conf
4、在服务器PC1中修改DHCP服务配置文件
[root@PC1 dhcp]# vim /etc/dhcp/dhcpd.conf ## ip地址范围为50-150
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;
}
各参数意义:
5、在服务器PC1中重启DHCP服务,并设动为开机自动启动
[root@PC1 dhcp]# systemctl restart dhcpd
[root@PC1 dhcp]# systemctl enable dhcpd
ln -s '/usr/lib/systemd/system/dhcpd.service' '/etc/systemd/system/multi-user.target.wants/dhcpd.service'
6、 在客户机PC2中验证自动分配IP效果
[root@PC2 Desktop]# ifconfig | head -n 3 ## 查看当前ip,未分配
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
ether 00:0c:29:25:bb:3e txqueuelen 1000 (Ethernet)
RX packets 445 bytes 27249 (26.6 KiB)
[root@PC2 Desktop]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777728 ## 修改网卡配置文件,设为开机启动
HWADDR=00:0C:29:25:BB:3E
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eno16777728
UUID=a0c2aa6b-48e1-4822-a67c-151155cc14bd
ONBOOT=yes
[root@PC2 Desktop]# systemctl restart network ## 重启网卡
[root@PC2 Desktop]# ifconfig | head -n 3 ## 查看此时ip
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.50 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
7、查看客户机PC2网卡MAC地址、PC2主机名称(绑定ip时需要使用MAC地址,MAC地址具有唯一性)
[root@PC2 Desktop]# ifconfig | head -n 6 ## 查看PC2的MAC地址
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.50 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
ether 00:0c:29:25:bb:3e txqueuelen 1000 (Ethernet)
RX packets 498 bytes 31249 (30.5 KiB)
RX errors 0 dropped 0 overruns 0 frame 0
[root@PC2 Desktop]# hostname ## 查看主机名称
PC2
8、在服务器PC1中修改配置文件,将特定IP和客户机PC2绑定
[root@PC1 dhcp]# 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 PC2 {
hardware ethernet 00:0c:29:25:bb:3e;
fixed-address 192.168.10.100;
}
}
9、在服务器PC1中重启DHCP服务
[root@PC1 dhcp]# systemctl restart dhcpd
10、在客户机PC2中进行验证
[root@PC2 Desktop]# ifconfig | head -n 3 ## 查看此时ip,仍然是50
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.50 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
[root@PC2 Desktop]# systemctl restart network ## 重启网卡服务
[root@PC2 Desktop]# ifconfig | head -n 3 ## 查看此时ip,可见已经实现了绑定
eno16777728: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.10.100 netmask 255.255.255.0 broadcast 192.168.10.255
inet6 fe80::20c:29ff:fe25:bb3e prefixlen 64 scopeid 0x20<link>
总结:
- 实现特定IP与特定设备的绑定仅需要修改服务器端配置文件,然后在客户端重启网卡
- 服务器端修改配置文件需要的关键信息为客户端网卡MAC地址、主机名称