My linux world » srv-dhcp

srv-dhcp


Prerequiste

I assume that you have a Centos installation.

Installation

You can copy/paste this script and use it to configure automatically your server.

  1. #!/bin/bash
  2.  
  3. echo "install dhcp"
  4. dnf -y install dhcp
  5.  
  6. echo "start service dhcpd at boot"
  7. systemctl enable dhcpd.service
  8.  
  9. echo "get current network interface"
  10. currentDevice=`nmcli d | grep connected | awk '{split($1,a,"\t"); print a[1]}'`
  11.  
  12. #echo "configure /etc/sysconfig/dhcpd and /etc/sysconfig/dhcpd6"
  13. #sed -i "s/^(DHCPDARGS=\).*/\1$currentDevice/" /etc/sysconfig/dhcpd
  14. #sed -i "s/^(DHCPDARGS=\).*/\1$currentDevice/" /etc/sysconfig/dhcpd6
  15.  
  16. echo "configure /etc/dhcp/dhcpd.conf"
  17.  
  18. myprefixIP=`hostname -I | cut -d. -f1,2,3| awk '{ print $1}'`
  19.  
  20. cat > /etc/dhcp/dhcpd.conf << "EOF"
  21. ddns-update-style none;
  22. authoritative;
  23. log-facility local7;
  24. default-lease-time 600;
  25. max-lease-time 7200;
  26. option subnet-mask 255.255.255.0;
  27. option broadcast-address a.b.c.255;
  28. option routers a.b.c.1;
  29. option domain-name-servers a.b.c.1;
  30. option domain-name "centos.local";
  31. subnet a.b.c.0 netmask 255.255.255.0 {
  32. range a.b.c.10 a.b.c.254;
  33. }
  34. EOF
  35.  
  36. sed -i "s/a.b.c/$myprefixIP/g" /etc/dhcp/dhcpd.conf
  37.  
  38.  
  39. echo "add service dhcp (port 67) to firewall"
  40. firewall-cmd --permanent --add-service dhcp
  41.  
  42. echo "start service"
  43. systemctl start dhcpd.service
  44.  

Copyright © 2024 My linux world - by Marc RABAHI
Design by Marc RABAHI and encelades.