[root@hding ~]# yum install dhcp
DHCP配置文件有一定的格式
[root@hding ~]# cp /usr/share/doc/dhcp-3.0.5/dhcpd.conf.sample /etc/dhcpd.conf
编辑dhcpd.conf主配置文件
[root@hding ~]# vi /etc/dhcpd.conf 1 ddns-update-style interim; 必需有 2 ignore client-updates; 3 4 subnet 192.168.0.0 netmask 255.255.255.0 { 192.168.0局部网段 5 6 # --- default gateway 7 option routers 192.168.0.111; 本机作为网关 8 option subnet-mask 255.255.255.0; 分配掩码 9 10 option nis-domain "hding.net"; NIS域 11 option domain-name "hding.net"; 域名 12 option domain-name-servers 192.168.0.111; 域名服务器IP 13 14 option time-offset -18000; # Eastern Standard Time 15 # option ntp-servers 192.168.0.1; 16 # option netbios-name-servers 192.168.1.1; 17 # --- Selects point-to-point node (default is hybrid). Don't change this unl ess 18 # -- you understand Netbios very well 19 # option netbios-node-type 2; 20 21 range dynamic-bootp 192.168.0.128 192.168.0.253; 分配地址池 22 default-lease-time 21600; 租约时间6小时 23 max-lease-time 43200; 最大租约时间12小时 24 25 # we want the nameserver to appear at a fixed address 26 host ns { 给固定MAC固定IP,通常是服务器之类固定IP 27 next-server hding.net; 28 hardware ethernet 12:34:56:78:AB:CD; 29 fixed-address 192.168.0.254; 30 } 31 } [root@hding ~]# service dhcpd restart Starting dhcpd: [ OK ]
client端测试: dhclient
server: [root@hding ~]# tail -F /var/log/messages Jan 14 08:05:58 hding dhcpd: DHCPDISCOVER from 00:50:56:25:94:05 via eth0 Jan 14 08:05:59 hding dhcpd: DHCPOFFER on 192.168.0.253 to 00:50:56:25:94:05 via eth0 Jan 14 08:05:59 hding dhcpd: DHCPREQUEST for 192.168.0.253 (192.168.0.111) from 00:50:56:25:94:05 via eth0 Jan 14 08:05:59 hding dhcpd: DHCPACK on 192.168.0.253 to 00:50:56:25:94:05 via eth0 [root@hding ~]# cat /var/lib/dhcpd/dhcpd.leases lease 192.168.0.253 { starts 3 2015/01/14 16:05:59; ends 3 2015/01/14 22:05:59; binding state active; next binding state free; hardware ethernet 00:50:56:25:94:05; }更换client网卡为:MAC:(12:34:56:78:AB:CD),给固定MAC固定IP
server: [root@hding ~]# tail -F /var/log/messages Jan 14 08:36:58 hding dhcpd: DHCPDISCOVER from 12:34:56:78:ab:cd via eth0 Jan 14 08:36:58 hding dhcpd: DHCPOFFER on 192.168.0.254 to 12:34:56:78:ab:cd via eth0 Jan 14 08:36:58 hding dhcpd: DHCPREQUEST for 192.168.0.254 (192.168.0.111) from 12:34:56:78:ab:cd via eth0 Jan 14 08:36:58 hding dhcpd: DHCPACK on 192.168.0.254 to 12:34:56:78:ab:cd via eth0dhcprelay:
前面介绍的是给同一网段内的PC分配IP地址,由于广播包是允许跨网段的,而dhcp的第一个discovery包是广播包就过不了,那么每个网段 都要有一台DHCP服务器?dhcprelay就是为了解决跨网段问题而存在的,它接收到本网段client的discovery包,然后由它单播转给另 一个网段的DHCP server,而DHCP server回的包也通过dhcp relay再回给client,这样client虽然发了广播的discovery,但经过dhcp relay转一下变成了单播,跨网段成功
A作为server,分配 192.168.0.0 255.255.255.0
192.168.4.0 255.255.255.0
C作为router,转发路由器
D作为与B同网段的dhcp relay,B发的广播包由D接收转发给A,A回的包由D再转给B
A: [root@hding ~]# vi /etc/dhcpd.conf 1 ddns-update-style interim; 2 ignore client-updates; 3 4 subnet 192.168.0.0 netmask 255.255.255.0 { 5 6 # --- default gateway 7 option routers 192.168.0.111; 8 option subnet-mask 255.255.255.0; 9 10 option nis-domain "hding.net"; 11 option domain-name "hding.net"; 12 option domain-name-servers 192.168.0.111; 13 14 option time-offset -18000; # Eastern Standard Time 15 # option ntp-servers 192.168.0.1; 16 # option netbios-name-servers 192.168.1.1; 18 # -- you understand Netbios very well 19 # option netbios-node-type 2; 20 21 range dynamic-bootp 192.168.0.128 192.168.0.253; 22 default-lease-time 21600; 23 max-lease-time 43200; 24 25 # we want the nameserver to appear at a fixed address 26 host ns { 27 next-server hding.net; 28 hardware ethernet 12:34:56:78:AB:CD; 29 fixed-address 192.168.0.254; 30 } 31 } 32 33 subnet 192.168.4.0 netmask 255.255.255.0 { 34 option routers 192.168.4.112; 35 option subnet-mask 255.255.255.0; 36 range dynamic-bootp 192.168.4.12 192.168.4.25; 37 default-lease-time 21600; 38 max-lease-time 43200; 39 } route add -net 192.168.4.0 netmask 255.255.255.0 gw 192.168.0.112
D: [root@ding ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth1 1 # Advanced Micro Devices [AMD] 79c970 [PCnet32 LANCE] 2 DEVICE=eth1 3 BOOTPROTO=none 4 ONBOOT=yes 5 IPADDR=192.168.4.1 6 NETMASK=255.255.255.0 7 GATEWAY=192.168.4.112 8 HWADDR=00:0c:29:52:73:72 [root@ding ~]# vi /etc/sysconfig/dhcrelay 1 # Command line options here 2 DHCRELAYARGS="" 3 INTERFACES="eth1" 4 DHCPSERVERS="192.168.0.111"
C: [root@dh network-scripts]# ifconfig eth0 Link encap:Ethernet HWaddr 12:34:56:78:AB:CD inet addr:192.168.0.112 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:591 errors:0 dropped:0 overruns:0 frame:0 TX packets:478 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:53371 (52.1 Kb) TX bytes:63627 (62.1 Kb) Interrupt:5 Base address:0x2000 eth1 Link encap:Ethernet HWaddr 00:0C:29:1D:C2:01 inet addr:192.168.4.112 Bcast:192.168.4.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:0 errors:0 dropped:0 overruns:0 frame:0 TX packets:4 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:100 RX bytes:0 (0.0 b) TX bytes:168 (168.0 b) Interrupt:5 Base address:0x2080 [root@dh network-scripts]# echo "1" > /proc/sys/net/ipv4/ip_forwardB: dhclient获取IP地址
A log: Jan 14 09:24:50 hding dhcpd: DHCPREQUEST for 192.168.0.252 from 00:0c:29:d0:f2:d2 (Julia) via 192.168.4.1: ignored (not authoritative). Jan 14 09:24:58 hding last message repeated 2 times Jan 14 09:25:06 hding dhcpd: DHCPDISCOVER from 00:0c:29:d0:f2:d2 via 192.168.4.1 Jan 14 09:25:07 hding dhcpd: DHCPOFFER on 192.168.4.25 to 00:0c:29:d0:f2:d2 (Julia) via 192.168.4.1 Jan 14 09:25:07 hding dhcpd: DHCPREQUEST for 192.168.4.25 (192.168.0.111) from 00:0c:29:d0:f2:d2 (Julia) via 192.168.4.1 Jan 14 09:25:07 hding dhcpd: DHCPACK on 192.168.4.25 to 00:0c:29:d0:f2:d2 (Julia) via 192.168.4.1B获取到192.168.4.1的地址,与A的192.168.0.0网段不同的地址,D在dchprelay成功