鸟哥学习笔记---DHCP

 

 1.到服务器的日志文件中查找该用户是否曾经租用过某个IP,若有且该IP目前无人使用,则提供此IP给客户端。

 2.若配置文件针对该IP地址提供特定的固定IP时,提供该固定IP给客户端。

 3.若不符合上面的两个条件,则随机选取当前没有被使用的IP参数给客户端,并记录下来。

 

获取MAC地址:

[root@Centosszm ~]# ifconfig | grep HW

eth0      Link encap:Ethernet  HWaddr 00:0C:29:24:C2:A5

[root@Centosszm ~]# arp -n
Address                  HWtype  HWaddress           Flags Mask            Iface
192.168.179.1            ether   00:50:56:c0:00:08   C                     eth0
192.168.179.2            ether   00:50:56:eb:ab:2c   C                     eth0
 0.5个租约时会发出续约,0.85个租约也一样会发出续约。服务器端67端口监听,客户端:68端口请求。

 

 

 

 

[root@Centosszm ~]# rpm -ql dhcp | grep conf

/etc/dhcp/dhcpd.conf

/etc/dhcp/dhcpd6.conf

/etc/sysconfig/dhcpd

/etc/sysconfig/dhcpd6

/etc/sysconfig/dhcrelay

/usr/share/doc/dhcp-4.1.1/dhcpd-conf-to-ldap

/usr/share/doc/dhcp-4.1.1/dhcpd.conf.sample

/usr/share/doc/dhcp-4.1.1/dhcpd6.conf.sample

/usr/share/man/man5/dhcpd.conf.5.gz

 

 

[root@Centosszm ~]# vi /etc/dhcp/dhcpd.conf

 CentOS 5.x以前,配置文件都被存储于/etc/dhcpd.conf,新版的才放置于此处。

[root@Centosszm ~]# vi /var/lib/dhcpd/dhcpd.leases
 
[root@Centosszm ~]# /usr/sbin/dhcpd
 
[root@Centosszm ~]# cat /etc/dhcp/dhcpd.conf
# dhcpd.conf
#
# Sample configuration file for ISC dhcpd
#
 
# option definitions common to all supported networks...
option domain-name "example.org"; #域名,加域名后缀用
option domain-name-servers ns1.example.org, ns2.example.org; #DNSIP
 
default-lease-time 600; #默认租约时间
max-lease-time 7200;   #最大租约时间
#客户端通过dhcpd服务器来更新DNS相关的信息
ignore client-updates
 
# Use this to enble / disable dynamic dns updates globally.
#ddns-update-style none; #更新主机名与IP的对应关系
 
# If this DHCP server is the official DHCP server for the local
# network, the authoritative directive should be uncommented.
#authoritative;
 
# Use this to send dhcp log messages to a different log file (you also
# have to hack syslog.conf to complete the redirection).
log-facility local7;
 
# No service will be given on this subnet, but declaring it helps the
# DHCP server to understand the network topology.
 
subnet 10.152.187.0 netmask 255.255.255.0 {
}
 
# This is a very basic subnet declaration.
 
subnet 10.254.239.0 netmask 255.255.255.224 {
  range 10.254.239.10 10.254.239.20;
  option routers rtr-239-0-1.example.org, rtr-239-0-2.example.org;
}
 
# This declaration allows BOOTP clients to get dynamic addresses,
# which we don't really recommend.
 
subnet 10.254.239.32 netmask 255.255.255.224 {
  range dynamic-bootp 10.254.239.40 10.254.239.60;
  option broadcast-address 10.254.239.31;
  option routers rtr-239-32-1.example.org;
}
 
# A slightly different configuration for an internal subnet.
subnet 10.5.5.0 netmask 255.255.255.224 {
  range 10.5.5.26 10.5.5.30;
  option domain-name-servers ns1.internal.example.org;
  option domain-name "internal.example.org";
  option routers 10.5.5.1;
  option broadcast-address 10.5.5.31;
  default-lease-time 600;
  max-lease-time 7200;
}
 
# Hosts which require special configuration options can be listed in
# host statements.   If no address is specified, the address will be
# allocated dynamically (if possible), but the host-specific informati                                                                                        on
# will still come from the host declaration.
 
host passacaglia {
  hardware ethernet 0:0:c0:5d:bd:95;
  filename "vmunix.passacaglia";
  server-name "toccata.fugue.com";
}
 
# Fixed IP addresses can also be specified for hosts.   These addresse                                                                                        s
# should not also be listed as being available for dynamic assignment.
# Hosts for which fixed IP addresses have been specified can boot usin                                                                                        g
# BOOTP or DHCP.   Hosts for which no fixed address is specified can o                                                                                        nly
# be booted with DHCP, unless there is an address range on the subnet
# to which a BOOTP client is connected which has the dynamic-bootp fla                                                                                        g
# set.
host fantasia {
  hardware ethernet 08:00:07:26:c0:a5;
  fixed-address fantasia.fugue.com;
}
 
# You can declare a class of clients and then do address allocation
# based on that.   The example below shows a case where all clients
# in a certain class get addresses on the 10.17.224/24 subnet, and all
# other clients get addresses on the 10.0.29/24 subnet.
 
class "foo" {
  match if substring (option vendor-class-identifier, 0, 4) = "SUNW";
}
 
shared-network 224-29 {
  subnet 10.17.224.0 netmask 255.255.255.0 {
    option routers rtr-224.example.org;
  }
  subnet 10.0.29.0 netmask 255.255.255.0 {
    option routers rtr-29.example.org;
  }
  pool {
    allow members of "foo";
    range 10.17.224.10 10.17.224.250;
  }
  pool {
    deny members of "foo";
    range 10.0.29.10 10.0.29.230;
  }
}
[root@Centosszm ~]# vim /etc/dhcp/dhcpd.conf
option domain-name "test.com";
option domain-name-servers 192.168.179.2;
 
subnet 192.168.179.0 netmsk 255.255.255.0 {
        range 192.168.179.4 192.168.179.200;
        option routers 192.168.179.2;
}
 
[root@Centosszm ~]# cat /etc/sysconfig/dhcpd
# Command line options here
DHCPDARGS="eth0"
 上一条命令在CentOS 5.x以后的版本上已经不需要了,因为新版本的DHCP会主动分析服务器与实际的dhcpd.conf设置,如果两者无法吻合,就会有错误提示,人性化多了。

 

 

[root@Centosszm ~]# service dhcpd start

Starting dhcpd:                                            [  OK  ]

 如果启动不了,可以查看下面的内容: [root@Centosszm ~]# tailf /var/log/messages

 

Mar 29 14:29:58 Centosszm dhcpd: Internet Systems Consortium DHCP Server 4.1.1-P1
Mar 29 14:29:58 Centosszm dhcpd: Copyright 2004-2010 Internet Systems Consortium.
Mar 29 14:29:58 Centosszm dhcpd: Copyright 2004-2010 Internet Systems Consortium.
Mar 29 14:29:58 Centosszm dhcpd: All rights reserved.
Mar 29 14:29:58 Centosszm dhcpd: For info, please visit https://www.isc.org/software/dhcp/
Mar 29 14:29:58 Centosszm dhcpd: Not searching LDAP since ldap-server, ldap-port and ldap-base-dn were not specified in the config file
Mar 29 14:29:58 Centosszm dhcpd: Internet Systems Consortium DHCP Server 4.1.1-P1
Mar 29 14:29:58 Centosszm dhcpd: Copyright 2004-2010 Internet Systems Consortium.
Mar 29 14:29:58 Centosszm dhcpd: All rights reserved.
Mar 29 14:29:58 Centosszm dhcpd: For info, please visit https://www.isc.org/software/dhcp/
Mar 29 14:29:58 Centosszm dhcpd: Wrote 0 class decls to leases file.
Mar 29 14:29:58 Centosszm dhcpd: Wrote 0 deleted host decls to leases file.
Mar 29 14:29:58 Centosszm dhcpd: Wrote 0 new dynamic host decls to leases file.
Mar 29 14:29:58 Centosszm dhcpd: Wrote 0 leases to leases file.
Mar 29 14:29:58 Centosszm dhcpd: Listening on LPF/eth0/00:0c:29:24:c2:a5/192.168.179.0/24
Mar 29 14:29:58 Centosszm dhcpd: Sending on   LPF/eth0/00:0c:29:24:c2:a5/192.168.179.0/24
Mar 29 14:29:58 Centosszm dhcpd: Sending on   Socket/fallback/fallback-net
 /etc/hosts会影响内部计算机在连接阶段的等待时间。
 
客户端机szm:

 [root@szm ~]# cat /etc/resolv.conf

; generated by /sbin/dhclient-script

search test.com

nameserver 192.168.179.2

 

[root@szm ~]# route -n

Kernel IP routing table

Destination     Gateway         Genmask         Flags Metric Ref    Use Iface

192.168.179.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1

192.168.111.0   0.0.0.0         255.255.255.0   U     0      0        0 eth1

169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth1

0.0.0.0         192.168.179.2   0.0.0.0         UG    0      0        0 eth1

 

[root@szm ~]# netstat -tulnp | grep dhc

udp        0      0 0.0.0.0:68                  0.0.0.0:*        3213/dhclient

 

[root@szm ~]# cat /var/lib/dhclient/dhclient-eth1.leases

lease {

  interface "eth1";

  fixed-address 192.168.179.143;

  option subnet-mask 255.255.255.0;

  option routers 192.168.179.2;

  option dhcp-lease-time 600;

  option dhcp-message-type 5;

  option domain-name-servers 192.168.179.2;

  option dhcp-server-identifier 192.168.179.7;

  option domain-name "test.com";

  renew 5 2013/03/29 07:16:18;

  rebind 5 2013/03/29 07:21:07;

  expire 5 2013/03/29 07:22:22;

}

    上面这个地方会记录客户端曾经获得的IP地址,客户端会向DHCP申请这个IP地址,如果想要不同的IP地址,那就把想要的Ip地址取代上面的IP地址就可以了

     如果一个局域网内有两台DHCP,若第一次取得DHCP1服务器的IP后,以后重新启动网络,都只会取得DHCP1的网络参数,这是为什么呢?[root@szm ~]# cat /var/lib/dhclient/dhclient-eth1.leases,删除后就是先到先得的方式了。

 

服务器端记录IP分配情况:

[root@Centosszm ~]# cat /var/lib/dhcpd/dhcpd.leases

# The format of this file is documented in the dhcpd.leases(5) manual page.

# This lease file was written by isc-dhcp-4.1.1-P1

 

lease 192.168.179.143 {

  starts 5 2013/03/29 07:01:09;

  ends 5 2013/03/29 07:11:09;

  tstp 5 2013/03/29 07:11:09;

  cltt 5 2013/03/29 07:01:09;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:e2:84:0d;

}

server-duid "\000\001\000\001\030\347\361f\000\014)$\302\245";

 

lease 192.168.179.143 {

  starts 5 2013/03/29 07:05:21;

  ends 5 2013/03/29 07:15:21;

  cltt 5 2013/03/29 07:05:21;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:e2:84:0d;

}

lease 192.168.179.143 {

  starts 5 2013/03/29 07:09:08;

  ends 5 2013/03/29 07:19:08;

  cltt 5 2013/03/29 07:09:08;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:e2:84:0d;

}

lease 192.168.179.143 {

  starts 5 2013/03/29 07:12:22;

  ends 5 2013/03/29 07:22:22;

  cltt 5 2013/03/29 07:12:22;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:e2:84:0d;

}

lease 192.168.179.88 {

  starts 5 2013/03/29 07:16:17;

  ends 5 2013/03/29 07:26:17;

  cltt 5 2013/03/29 07:16:17;

  binding state active;

  next binding state free;

  hardware ethernet 00:0c:29:e2:84:0d;

}

 

让大量PC都具有固定IP的脚本:arp -n

Widnows获得:netsh interface ip set address xxx

 

    已经知道了客户端的MAC地址了,如果客户端的主机支持一些电源标准,并且改客户端主机所使用的网卡与主板支持网络唤醒功能,我们就可以通过网络来让客户端计算机开机了。

[root@Centosszm ~]# ether-wake -i 11:22:33:44:55:66

 

 

 

 

 

你可能感兴趣的:(linux)