Install the dhcp service script with one click
Experimental environment: set the native IP to 192.168.100.100, and the script code is as follows:
The replicable code is as follows:
#! / bin/bashyum install dhcp- ynetwork= "/ etc/sysconfig/network-scripts/ifcfg-ens33" dhcp= "/ etc/dhcp/dhcpd.conf" [!-e $network.bak] & & cp- p $network $network.baksed-I-e "4s/dhcp/static/"-e'$aIPADDR=192.168.100.100\ nNETMASK=255.255.255.0\ nGATEWAY=192.168.100.1' $networksystemctl restart networkcp-p / usr/share/doc/dhcp-4.2.5/dhcpd.conf.example $dhcp [!-e $dhcp.bak] & & cp-p $dhcp $dhcp.baksed-I-e "29asubnet 192.168.100.0 netmask 255.255.255.0 {\ nrange 192.168.100.10 192.168.100.50 \ n option routers 192.168.100.1;\ n} "$dhcpsystemctl start dhcpd detailed explanation
First perform variable assignment, assign value to network and dhcp variable names
1. ``[!-e $network.bak] & & cp-p $network $network.bak
Determine whether the network file has been backed up, and perform a backup operation if there is no backup
2. Sed-I-e "4s/dhcp/static/"-e'$aIPADDR=192.168.100.100\ nNETMASK=255.255.255.0\ nGATEWAY=192.168.100.1' $network
Systemctl restart network
Change the IP address type in the network file to static, and add IPADDR=192.168.100.100 at the last line, followed by a new line with "\ n"; add NETMASK=255.255.255.0, and then use another line; add GATEWAY=192.168.100.1
3 、 systemctl restart network
Restart network services
4. Cp- p / usr/share/doc/dhcp-4.2.5/dhcpd.conf.example $dhcp
Copy the dhcp sample configuration file to the dhcp configuration file to achieve full coverage.
5. [!-e $dhcp.bak] & & cp-p $dhcp $dhcp.bak
Determine whether the dhcp configuration file has been backed up, and perform a backup operation if there is no backup
6. Sed-I-e "29asubnet 192.168.100.0 netmask 255.255.255.0 {\ nrange 192.168.100.10 192.168.100.50
Add network segments, subnet masks, gateways and the range of ip addresses that can be given to the dhcp configuration file (can be changed as needed)
7 、 ssystemctl start dhcpd
Enable the dhcpd service