基本的东西都在了,需要用的拿出来组合一下就OK了
#! /bin/bash
yum -y install dhcp
\cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
read -p "请输入网段" wd
read -r "请输入子网掩码" zwym
sed -i -r "/^subnet/s/((([0-9]{0,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]{0,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))/$wd/" /etc/dhcpd.conf
sed -i -r "/^subnet/s/((([0-9]{0,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]{0,2}|1[0-9]{2}|2[0-4][0-9]|25[0-5]))/$zwym/2" /etc/dhcpd.conf
sed -i '2 a option domain-name-servers 8.8.8.8;' /etc/dhcpd.conf
sed -i '2 a max-lease-time 7200;' /etc/dhcpd.conf
sed -i '2 a default-lease-time 600;' /etc/dhcpd.conf
sed -i '/default gateway/,+24d' /etc/dhcpd.conf
sed -ri '/^subnet/a \\toption routers 192.168.1.254;' /etc/dhcpd.conf
sed -ri '/^subnet/a \\toption subnet-mask 255.255.255.0;' /etc/dhcpd.conf
sed -ri '/^subnet/a \\trange 192.168.1.100 192.168.1.200;' /etc/dhcpd.conf
sed -ri '/^subnet/a \\trange 192.168.1.50 192.168.1.60;' /etc/dhcpd.conf
service dhcpd start
优化
sed -i 's/DHCPDARGS=/DHCPDARGS="eth0"/' /etc/sysconfig/dhcpd
开启dhcp 中继
sed -i '/net.ipv4.ip_forward/s/0/1/' /etc/sysctl.conf
sysctl -p
echo "DHCRELAYARGS=\"eth0\"" >/etc/sysconfig/dhcrelay
echo "DHCPSERVERS=\"192.168.1.254\"" >>/etc/sysconfig/dhcrelay
service dhcrelay start
或者
sed -i '/DHCRELAYARGS=/s/""/"eth0"/' /etc/sysconfig/dhcrelay
sed -i '/DHCPSERVERS=/s/""/"192.168.1.254"/' /etc/sysconfig/dhcrelay
sed -i '/DHCRELAYARGS=/s/""/"eth0"/'/etc/sysconfig/dhcrelay
客户端 检查
dhclient -d eth0
服务器查看dhcp 分配信息
/var/lib/dhcpd/dhcpd.leases