DHCP server setup

Posted by gitosh on Thu, 09 Dec 2021 06:14:24 +0100

matters needing attention

  • When using two Linux hosts, you must ensure that the two devices can be connected to each other. You can configure static IP first to test whether the two hosts are connected
  • In the virtual network editor, do not use the local DHCP service to assign IP addresses to virtual machines
  • Both devices use host only mode or use the same VMnet interface




Server configuration

Configure the IP address of the server

  • Configure the IP address for the server and restart the network card to make the configuration effective
[root@Server ~]# vim /etc/sysconfig/network-scripts/ifcfg-eno16777736 
[root@Server ~]# cat /etc/sysconfig/network-scripts/ifcfg-eno16777736 
TYPE=Ethernet
BOOTPROTO=static
NAME=eno16777736
DEVICE=eno16777736
ONBOOT=yes
IPADDR=192.168.43.128
NETMASK=255.255.255.0
GATEWAY=192.168.43.1
DNS1=8.8.8.8
[root@Server ~]# systemctl restart network
  • The assigned IP address must be in the same network segment as the configured static IP address
  • ifcfg-ens33 here is my network card NAME. The network card NAME will be different in different versions of the system. Specifically, configure it according to your own network card NAME. The NAME, DEVICE and network card NAME should be consistent

Configure local YUM warehouse

  • First, you need to use RHEL's ISO image and connect to the virtual machine


  • Mount the image file
[root@Server ~]# mount /dev/cdrom /mnt/
mount: /dev/sr0 Write protected, will mount as read-only
[root@Server ~]# 
  • Configure the YUM warehouse and use the local ISO image file for installation. If the virtual machine is connected to the network, you can also use the network source to install the software package
[root@Server ~]# cd /etc/yum.repos.d/
[root@Server yum.repos.d]# rm -rf * / / delete all other source configurations in this directory
[root@Server yum.repos.d]# vim rhel.repo / / configure a new source file
[root@Server yum.repos.d]# ls
rhel.repo
[root@Server yum.repos.d]# cat rhel.repo 
[Base]
name=RHEL                //Warehouse name
baseurl=file:///mnt / / the warehouse source used. file: / / is in fixed format, / mnt indicates the directory where the local software package is located
gpgcheck=0               //Check and verify
enabled=1                //Enable this warehouse
  • If there are other. repo files in / etc/yum.repos.d/ directory, use rm -rf * to delete them all. Avoid using other source files.
  • Clear package cache
[root@Server yum.repos.d]# yum clean all 
Plug in loaded: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Cleaning up software source: Base
Cleaning up everything
[root@Server yum.repos.d]# 
  • Reload package
[root@Server yum.repos.d]# yum repolist all 
Plug in loaded: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Base                                            | 4.1 kB     00:00     
(1/2): Base/group_gz                              | 137 kB   00:00     
(2/2): Base/primary_db                            | 4.0 MB   00:00     
Source identification                         Source name                       state
Base                           RHEL                         Enable: 4,986
repolist: 4,986
[root@Server yum.repos.d]# 
  • repolist: 4986: indicates the number of software packages available. If not, check the configuration of the local source file.

Install DHCP package

  • Use the YUM tool to install the DHCP package
[root@master-bad ~]# yum -y install dhcp
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package dhcp.x86_64 12:4.2.5-58.el7 will be installed

...

Installed:
  dhcp.x86_64 12:4.2.5-58.el7                                                   

Dependency Updated:
  dhclient.x86_64 12:4.2.5-58.el7       dhcp-common.x86_64 12:4.2.5-58.el7     
  dhcp-libs.x86_64 12:4.2.5-58.el7     

Complete!
[root@master-bad ~]# 
  • Copy the default DHCP configuration file to the DHCP configuration directory
[root@master-bad ~]# cd /etc/dhcp/
[root@master-bad dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
[root@master-bad dhcp]# cat dhcpd.conf 
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
[root@master-bad dhcp]# cat /usr/share/doc/dhcp-4.2.5/dhcpd.conf.example | grep -v "#" >> dhcpd.conf
[root@master-bad dhcp]# ls
dhclient.d  dhclient-exit-hooks.d  dhcpd6.conf  dhcpd.conf  scripts
[root@master-bad dhcp]# 
  • /usr/share/doc / dhcp-4.2.5/dhcp pd.conf.example: it is the default configuration file of DHCP. According to different versions of DHCP software packages, the DHCP version under / usr/share/doc / needs to be modified
  • Grep - V "#" > > dhcpd.conf: it means to delete the comment line in the default configuration file and append it to the dhcpd.conf file again
  • Modify the DHCP configuration file and configure it as required
[root@Server dhcp]# vim dhcpd.conf
[root@Server dhcp]# cat dhcpd.conf
#
# DHCP Server Configuration file.
#   see /usr/share/doc/dhcp*/dhcpd.conf.example
#   see dhcpd.conf(5) man page
#
subnet 192.168.43.0  netmask 255.255.255.0 {     //Configure the address, network segment and subnet mask that the DHCP server can assign
  range 192.168.43.20 192.168.43.40;             //Configure the address range that the DHCP server can assign
  option domain-name-servers 192.168.43.128;     //Configure the IP address of the DNS server
  option routers 192.168.43.1;                   //Configure default gateway
  default-lease-time 600;                        //Configure the default lease term in seconds
  max-lease-time 7200;                           //Configure the maximum lease term in seconds
}

host RHEL_Client_7.4 {                           //Separate configuration for special hosts
  hardware ethernet 00:0c:29:9e:9b:29;           //The MAC address of the host that needs to be bound to the fixed IP
  fixed-address 192.168.43.15;                   //Bind fixed IP address for host
}
  • For the modification of the configuration file, try not to modify the source file. Copy it first, annotate the source content, and make a backup, so as to prevent the configuration error from being rolled back.
  • If you do not need to assign a fixed IP address to a special host, you do not need to configure host

Turn off firewall and SELinux

  • Turn off firewall
[root@Server ~]# systemctl stop firewalld.service
[root@Server ~]# systemctl status firewall
firewall.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

[root@Server ~]#
  • Active: inactive (dead): indicates that the firewall has been turned off
  • Active: active (running): indicates that the firewall has been turned on
  • Close SELinux
[root@Server ~]# vim /etc/selinux/config 
[root@Server ~]# cat !$ | grep -v "#" | grep -v "^$"
SELINUX=disabled 
SELINUXTYPE=targeted 
[root@Server ~]#
  • !$: Represents the last parameter of the previous command
  • grep -v "#" | grep -v "^ $": indicates filtering comment lines and empty lines

Start DHCP service

  • Start DHCP service
[root@Server ~]# systemctl restart dhcpd.service 
[root@Server ~]# systemctl status dhcpd.service 
dhcpd.service - DHCPv4 Server Daemon
   Loaded: loaded (/usr/lib/systemd/system/dhcpd.service; disabled)
   Active: active (running) since IV 2021-12-09 11:09:00 CST; 8s ago
     Docs: man:dhcpd(8)
           man:dhcpd.conf(5)
 Main PID: 8274 (dhcpd)
   Status: "Dispatching packets..."
   CGroup: /system.slice/dhcpd.service
           └─8274 /usr/sbin/dhcpd -f -cf /etc/dhcp/dhcpd.conf -user dhcpd -gr...

Client configuration

  • Change the IP address acquisition method of the client to dynamic acquisition through DHCP, and restart the network card
[root@Client ~]#  vim /etc/sysconfig/network-scripts/ifcfg-ens33 
[root@Client ~]# cat /etc/sysconfig/network-scripts/ifcfg-ens33 
TYPE=Ethernet
BOOTPROTO=dhcp             //Configured to obtain IP address through DHCP
NAME=ens33
DEVICE=ens33
ONBOOT=yes                 //Configure startup and self startup

# Configure static IP address
#IPADDR=192.168.43.131
#NETMASK=255.255.255.0
#GATEWAY=192.168.43.254
#DNS1=8.8.8.8
[root@Client ~]# systemctl restart network

View the IP address of the client

  • Use ifconfig or ip a to view the local IP address
[root@Client ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.43.15  netmask 255.255.255.0  broadcast 192.168.43.255
        inet6 fe80::20c:29ff:fe9e:9b29  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:9e:9b:29  txqueuelen 1000  (Ethernet)
        RX packets 2961  bytes 275996 (269.5 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1347  bytes 169005 (165.0 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1  (Local Loopback)
        RX packets 420  bytes 34416 (33.6 KiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 420  bytes 34416 (33.6 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

[root@Client ~]# 
  • ether 00:0c:29:9e:9b:29: indicates the MAC address of the local network card. When the server configures the host to bind with IP, you need to view the MAC address of the local network card
  • You can see that the IP address of this machine is 192.168.43.15, which is the IP address assigned to the DHCP server

Topics: Linux Operation & Maintenance server