安装
yum install -y dhcp
配置文件
默认配置为/etc/dhcpd.conf
[[email protected] ~]# [[email protected] ~]# rpm -ql dhcp | grep conf.sample /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample [[email protected] ~]#
使用此示例配置文件覆盖/etc/dhcpd.conf
根据具体情况配置此文件
[[email protected] ~]# cat /etc/dhcpd.conf ddns-update-style interim; ignore client-updates; subnet 100.1.1.0 netmask 255.255.255.0 { # --- default gateway option routers 100.1.1.1; option subnet-mask 255.255.255.0; option nis-domain "domain.org"; option domain-name "domain.org"; option domain-name-servers 114.114.114.114; option time-offset -18000; # Eastern Standard Time # option ntp-servers 192.168.1.1; # option netbios-name-servers 192.168.1.1; # --- Selects point-to-point node (default is hybrid). Don‘t change this unless # -- you understand Netbios very well # option netbios-node-type 2; range dynamic-bootp 100.1.1.128 100.1.1.254; default-lease-time 21600; max-lease-time 43200; # we want the nameserver to appear at a fixed address host ns { next-server marvin.redhat.com; hardware ethernet 12:34:56:78:AB:CD; fixed-address 207.175.42.254; } }
关于配置文件内容跟参数选项意义参考一下:
/etc/dhcpd.conf通常包括三部分:parameters、declarations 、option。
1.DHCP配置文件中的parameters(参数):表明如何执行任务,是否要执行任务,或将哪些网络配置选项发送给客户。主要内容见表1
2. DHCP配置文件中的declarations (声明):用来描述网络布局、提供客户的IP地址等。主要内容见表2:
3. DHCP配置文件中的option(选项):用来配置DHCP可选参数,全部用option关键字作为开始,主要内容包括见表3:
注意:如果客户端使用的是视窗操作系统,不要选择"host-name"选项,即不要为其指定主机名称。
启动服务
service dhcpd start
或者
dhcpd -p 67 -cf /etc/dhcpd.conf
查看端口是否监听
netstat -an | grep 67
启动脚本,可以查看很多相关记录文件信息
#!/bin/sh # ### BEGIN INIT INFO # Provides: dhcpd # Default-Start: # Default-Stop: # Should-Start: # Required-Start: $network # Required-Stop: # Short-Description: Start and stop the DHCP server # Description: dhcpd provides the Dynamic Host Configuration Protocol (DHCP) # server. ### END INIT INFO # # The fields below are left around for legacy tools (will remove later). # # chkconfig: - 65 35 # description: dhcpd provides the Dynamic Host Configuration Protocol (DHCP) # server # processname: dhcpd # config: /etc/dhcpd.conf # config: /var/lib/dhcpd/dhcpd.leases # pidfile: /var/run/dhcpd.pid . /etc/rc.d/init.d/functions RETVAL=0 prog=dhcpd exec=/usr/sbin/dhcpd lockfile=/var/lock/subsys/dhcpd pidfile=/var/run/dhcpd.pid statedir=/var/lib/dhcpd [ -f /etc/sysconfig/dhcpd ] && . /etc/sysconfig/dhcpd # if the user specified a different config file, make sure we reference it findConfig() { for arg in $DHCPDARGS ; do if [ "$found" = 1 ]; then [ -f "$arg" ] && echo "$arg" return fi if [ "$arg" = "-cf" ]; then found=1 continue fi done echo "/etc/dhcpd.conf" } config="$(findConfig "$DHCPDARGS")" if [ ! -f $statedir/dhcpd.leases ] ; then mkdir -p $statedir touch $statedir/dhcpd.leases [ -x /sbin/restorecon ] && [ -d /selinux ] && /sbin/restorecon $statedir/dhcpd.leases >/dev/null 2>&1 fi configtest() { [ -x $exec ] || return 5 [ -f $config ] || return 6 $exec -q -t -cf $config RETVAL=$? if [ $RETVAL -eq 1 ]; then $exec -t -cf $config else echo "Syntax: OK" >&2 fi return $RETVAL } rh_status() { status -p $pidfile $exec } rh_status_q() { rh_status >/dev/null 2>&1 } start() { [ `id -u` -eq 0 ] || return 4 [ -x $exec ] || return 5 [ -f $config ] || return 6 rh_status_q && return 0 echo -n $"Starting $prog: " daemon --pidfile=$pidfile $exec $DHCPDARGS 2>/dev/null RETVAL=$? echo [ $RETVAL -eq 0 ] && touch $lockfile return $RETVAL } stop() { [ `id -u` -eq 0 ] || return 4 rh_status_q || return 0 echo -n $"Shutting down $prog: " killproc -p $pidfile $prog RETVAL=$? echo [ $RETVAL -eq 0 ] && rm -f $lockfile return $RETVAL } usage() { echo $"Usage: $0 {start|stop|restart|force-reload|condrestart|try-restart|configtest|status}" } if [ $# -gt 1 ]; then exit 2 fi case "$1" in start) start ;; stop) stop ;; restart|force-reload) stop ; start ;; condrestart|try-restart) rh_status_q || exit 0 stop ; start ;; reload) usage # unimplemented feature exit 3 ;; configtest) configtest ;; status) rh_status ;; *) usage exit 2 ;; esac exit $?
服务端查看已分配的IP地址
客户端的重新获取都会得到记录
[[email protected] ~]# [[email protected] ~]# [[email protected] ~]# cat /var/lib/dhcpd/dhcpd.leases # All times in this file are in UTC (GMT), not your local timezone. This is # not a bug, so please don‘t ask about it. There is no portable way to # store leases in the local timezone, so please don‘t request this as a # feature. If this is inconvenient or confusing to you, we sincerely # apologize. Seriously, though - don‘t ask. # The format of this file is documented in the dhcpd.leases(5) manual page. # This lease file was written by isc-dhcp-V3.0.5-RedHat lease 100.1.1.254 { starts 4 2016/05/05 03:41:36; ends 4 2016/05/05 09:41:36; binding state active; next binding state free; hardware ethernet 00:0c:29:30:b2:b3; } lease 100.1.1.254 { starts 4 2016/05/05 04:12:03; ends 4 2016/05/05 10:12:03; binding state active; next binding state free; hardware ethernet 00:0c:29:30:b2:b3; }
客户端查看相关信息
客户端上获取到IP地址后,dhclient 可以查看下相关信息,比如dhcp server等
[[email protected] ~]# [[email protected] ~]# pkill dhclient [[email protected] ~]# dhclient Internet Systems Consortium DHCP Client V3.0.5-RedHat Copyright 2004-2006 Internet Systems Consortium. All rights reserved. For info, please visit http://www.isc.org/sw/dhcp/ Listening on LPF/eth0/00:0c:29:30:b2:b3 Sending on LPF/eth0/00:0c:29:30:b2:b3 Sending on Socket/fallback DHCPREQUEST on eth0 to 255.255.255.255 port 67 (xid=0x6656d649) DHCPACK from 100.1.1.100 (xid=0x6656d649) bound to 100.1.1.254 -- renewal in 8131 seconds. [[email protected] ~]# [[email protected] ~]#
如果想查看更详细的信息,例如租期等等,可查看相关文件
[[email protected] ~]# ls /var/lib/dhclient/ dhclient-eth0.leases dhclient.leases [[email protected] ~]# [[email protected] ~]# cat /var/lib/dhclient/dhclient.leases lease { interface "eth0"; fixed-address 100.1.1.254; option subnet-mask 255.255.255.0; option time-offset -18000; option dhcp-lease-time 21600; option routers 100.1.1.1; option dhcp-message-type 5; option dhcp-server-identifier 100.1.1.100; option domain-name-servers 114.114.114.114; option nis-domain "domain.org"; option domain-name "domain.org"; renew 4 2016/5/5 20:28:53; rebind 4 2016/5/5 23:11:17; expire 4 2016/5/5 23:56:17; } lease { interface "eth0"; fixed-address 100.1.1.254; option subnet-mask 255.255.255.0; option time-offset -18000; option routers 100.1.1.1; option dhcp-lease-time 21600; option dhcp-message-type 5; option domain-name-servers 114.114.114.114; option dhcp-server-identifier 100.1.1.100; option nis-domain "domain.org"; option domain-name "domain.org"; renew 4 2016/5/5 20:12:29; rebind 4 2016/5/5 23:11:58; expire 4 2016/5/5 23:56:58; }
时间: 2024-10-11 13:37:32