DHCP

时间:2023-03-09 03:44:38
DHCP

安装

yum install -y dhcp

配置文件

默认配置为/etc/dhcpd.conf

[root@samba ~]#
[root@samba ~]# rpm -ql dhcp | grep conf.sample
/usr/share/doc/dhcp-/dhcpd.conf.sample
[root@samba ~]#

使用此示例配置文件覆盖/etc/dhcpd.conf

根据具体情况配置此文件

[root@samba ~]# 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 ; # 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 ;

    range dynamic-bootp 100.1.1.128 100.1.1.254;
    default-lease-;
    max-lease-;

    # we want the nameserver to appear at a fixed address
    host ns {
        next-server marvin.redhat.com;
        hardware ethernet ::::AB:CD;
        fixed-address 207.175.42.254;
    }
}

关于配置文件内容跟参数选项意义参考一下:

/etc/dhcpd.conf通常包括三部分:parameters、declarations 、option。

  1.DHCP配置文件中的parameters(参数):表明如何执行任务,是否要执行任务,或将哪些网络配置选项发送给客户。主要内容见表1

DHCP

2. DHCP配置文件中的declarations (声明):用来描述网络布局、提供客户的IP地址等。主要内容见表2:

DHCP

3. DHCP配置文件中的option(选项):用来配置DHCP可选参数,全部用option关键字作为开始,主要内容包括见表3:


DHCP

注意:如果客户端使用的是视窗操作系统,不要选择"host-name"选项,即不要为其指定主机名称。

启动服务

service dhcpd start

或者

dhcpd -p  -cf /etc/dhcpd.conf

查看端口是否监听

netstat -an | 

启动脚本,可以查看很多相关记录文件信息

#!/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: -
# 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=

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
         ]; then
            [ -f "$arg" ] && echo "$arg"
            return
        fi
        if [ "$arg" = "-cf" ]; then
            found=
            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/>&
fi

configtest() {
    [ -x $exec ] || return
    [ -f $config ] || return
    $exec -q -t -cf $config
    RETVAL=$?
     ]; then
        $exec -t -cf $config
    else

    fi
    return $RETVAL
}

rh_status() {
    status -p $pidfile $exec
}

rh_status_q() {
    rh_status >/dev/>&
}

start() {
    [ ` ] || return
    [ -x $exec ] || return
    [ -f $config ] || return 

    rh_status_q && return 

    echo -n $"Starting $prog: "
    daemon --pidfile=$pidfile $exec $DHCPDARGS >/dev/null
    RETVAL=$?

    echo
    [ $RETVAL -eq  ] && touch $lockfile
    return $RETVAL
}

stop() {
    [ ` ] || return 

    rh_status_q || return 

    echo -n $"Shutting down $prog: "
    killproc -p $pidfile $prog
    RETVAL=$?

    echo
    [ $RETVAL -eq  ] && rm -f $lockfile
    return $RETVAL
}

usage() {
    echo $"Usage: $0 {start|stop|restart|force-reload|condrestart|try-restart|configtest|status}"
}

 ]; then
    exit
fi

case "$1" in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart|force-reload)
        stop ; start
        ;;
    condrestart|try-restart)
        rh_status_q || exit
        stop ; start
        ;;
    reload)
        usage
        # unimplemented feature
        exit
        ;;
    configtest)
        configtest
        ;;
    status)
        rh_status
        ;;
    *)
        usage
        exit
        ;;
esac

exit $?

服务端查看已分配的IP地址

客户端的重新获取都会得到记录

[root@samba ~]#
[root@samba ~]#
[root@samba ~]# 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 ) manual page.
# This lease file was written by isc-dhcp-V3.0.5-RedHat

lease 100.1.1.254 {
  starts  // ::;
  ends  // ::;
  binding state active;
  next binding state free;
  hardware ethernet :0c:::b2:b3;
}
lease 100.1.1.254 {
  starts  // ::;
  ends  // ::;
  binding state active;
  next binding state free;
  hardware ethernet :0c:::b2:b3;
}

客户端查看相关信息

客户端上获取到IP地址后,dhclient 可以查看下相关信息,比如dhcp server等

[root@localhost ~]#
[root@localhost ~]# pkill dhclient
[root@localhost ~]# dhclient
Internet Systems Consortium DHCP Client V3.0.5-RedHat
Copyright - Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/

Listening on LPF/eth0/:0c:::b2:b3
Sending on   LPF/eth0/:0c:::b2:b3
Sending on   Socket/fallback
DHCPREQUEST on eth0 to  (xid=0x6656d649)
DHCPACK from 100.1.1.100 (xid=0x6656d649)
bound to  seconds.
[root@localhost ~]#
[root@localhost ~]#

如果想查看更详细的信息,例如租期等等,可查看相关文件

[root@localhost ~]# ls /var/lib/dhclient/
dhclient-eth0.leases  dhclient.leases
[root@localhost ~]#
[root@localhost ~]# cat /var/lib/dhclient/dhclient.leases
lease {
  interface "eth0";
  fixed-address 100.1.1.254;
  option subnet-mask 255.255.255.0;
  option ;
  option dhcp-lease-;
  option routers 100.1.1.1;
  option dhcp-message-type ;
  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  // ::;
  rebind  // ::;
  expire  // ::;
}
lease {
  interface "eth0";
  fixed-address 100.1.1.254;
  option subnet-mask 255.255.255.0;
  option ;
  option routers 100.1.1.1;
  option dhcp-lease-;
  option dhcp-message-type ;
  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  // ::;
  rebind  // ::;
  expire  // ::;
}