DHCP簡介
DHCP(Dynamic Host Configuration Protocol,動態主機配置協議)是一種基於客戶/服務器模式的服務協議。工做原理其實很簡單,就是在安裝有DHCP服務器的兩種網絡中,客戶端啓動時自動與DHCP服務器通訊,要求服務器提供自動分配ip地址的服務,而安裝了DHCP服務軟件的服務器響應這個要求。並向客戶端發送出合法的IP地址。
DHCP工做流程
DHCP ip地址租約
1. 限定租期
2. 永久租用
DHCP 租約的更新
1. 開機在關機後ip地址更新
2. 租期到50%後發送dhcp請求更新地址租約
3. 若是更新未成功到租期的87.5%是在發送dhcp請求更新租約
4. 若是以上兩次都未成功租約到期後從新更新租約
DHCP安裝前的準備工做
1. 經過命令確認系統中是否已經安裝DHCP服務
[root@localhost ~]# rpm -qa |grep dhcp
dhcp-3.0.5-21.el5
dhcpv6-client-1.0.10-17.el5
2. 配置dhcrelay守護進程
若是dhcp服務器須要用於遠程網絡,那麼必須把dhcrelay守護進程配置在局域網之間的路由器/網關計算機上。
3. 把dhcp服務器的ip地址設置成爲靜態ip地址
案例一(dhcp單區域)
1.掛在光盤
[root@localhost ~]#mkdir /mnt/cdrom
建立一個用戶掛載的光盤目錄
[root@localhost ~]#mount /dev/cdrom /mnt/cdrom 掛在光盤
2. 安裝dhcp服務
[root@localhost ~]#cd /mnt/cdrom
[root@localhost ~]#prm –ivh dhcp-3.0.5-13.e15.i386.rpm 安裝dhcp服務的命令
3.配置dhcp服務器的配置文件
[root@localhost ~]# vim /etc/dhcpd.conf
進入配置目錄
1 #
2 ddns-update-style interim; #配置使用過分性DHCP-DNS互動模式。
3 ignore client-updates;
#忽略客戶端更新
4
5 subnet 192.168.1.0 netmask 255.255.255.0 { 網段聲明
6
7 # --- default gateway
8
option routers 192.168.1.254; 定義網關
9
option subnet-mask 255.255.255.0; 定義子網掩碼
10
11
option nis-domain "domain.org"; 設置nis域名
12
option domain-name "domain.org"; 設置域名
13
option domain-name-servers 222.88.88.88; (這裏是電信的dns)
14
15
option time-offset -18000; # 設置時間偏移
16 #
option ntp-servers 192.168.1.1; 設置ntp服務器
17 #
option netbios-name-servers 192.168.1.1;
18 # --- Selects point-to-point node (default is hybrid). Don't change this unless
19 # -- you understand Netbios very well
20 # option netbios-node-type 2; 設置nis服務的節點(不建議修改)
21
22
range dynamic-bootp 192.168.0.128 192.168.0.254; 設置地址池
23
default-lease-time 21600; 設置默認租期
24
max-lease-time 43200; 設置最大租期
25
26
# we want the nameserver to appear at a fixed address
27
host ns {
28
next-server marvin.redhat.com;
29
hardware ethernet 12:34:56:78:AB:CD;
30
fixed-address 207.175.42.254;
31
}
32 }
3
3 # DHCP Server Configuration file.
34 #
see /usr/share/doc/dhcp*/dhcpd.conf.sample
35 #
4.利用一個客戶機測試的結果
案例二(dhcp超級做用域)
咱們知道一個C類的地址能爲254臺PC提供ip地址上網用,若是一個公司的電腦數量多於254臺這是用一個C類的地址就不夠用了。對於這種狀況有兩種解決方案一種是用一個B類的地址,第二種是創建一個由C類地址主城的超級做用域。
1. 第一步
跟案例一同樣
2. 第二部 跟案例一同樣
3.配置dhcp服務器的配置文件
[root@localhost ~]# vim /etc/dhcpd.conf
進入配置目錄
5 shared-network zzdx {
配置的超級做用域zzdx是超級做用域的名稱
6 subnet 192.168.1.0 netmask 255.255.255.0 {
7
8 # --- default gateway
9
option routers 192.168.1.254;
10
option subnet-mask 255.255.255.0;
11
12
option nis-domain "domain.org";
13
option domain-name "abc.com";
14
option domain-name-servers 222.88.88.88;
15
16
option time-offset -18000; # Eastern Standard Time
17 #
option ntp-servers 192.168.1.1;
18 #
option netbios-name-servers 192.168.1.1;
19 # --- Selects point-to-point node (default is hybrid). Don't change this unless
20 # -- you understand Netbios very well
21 #
option netbios-node-type 2;
22
23
range dynamic-bootp 192.168.1.2 192.168.1.253;
24
default-lease-time 21600;
25
max-lease-time 43200;
26
27
# we want the nameserver to appear at a fixed address
28
host ns {
29
next-server marvin.redhat.com;
30
hardware ethernet 12:34:56:78:AB:CD;
31
fixed-address 207.175.42.254;
32
}
33 }
34 subnet 192.168.2.0 netmask 255.255.255.0 {
配置第二個ip池
35
option routers 192.168.2.254;
36
option subnet-mask 255.255.255.0;
37
option domain-name-servers 222.88.88.88;
38
range dynamic-bootp 192.168.2.2 192.168.2.253;
39
default-lease-time 21600;
40
max-lease-time 43200;
4 測試因爲超級做用域的ip取值是先從一個做用域內取值知道一個做用域內的ip地址用完後纔開始從第二個ip地址池內取地址。測試時能夠把超級做用域中的兩個地址池都改成一個ip地址。
案例三(dhcp中繼)
案例拓補圖
第一步 跟案例一同樣
第二步 跟案例二同樣
第三步 .配置dhcp服務器的配置文件
[root@localhost ~]# vim /etc/dhcpd.conf
進入配置目錄
subnet 192.168.3.0 netmask 255.255.255.0 {
vlan30
的地址池
# --- default gateway
option routers 192.168.3.254;
option subnet-mask 255.255.255.0;
option domain-name "jisu.com";
option domain-name-servers 222.88.88.88;
option time-offset -18000; # Eastern Standard Time
default-lease-time 21600;
max-lease-time 43200;
host ns {
next-server marvin.redhat.com;
hardware ethernet 12:34:56:78:AB:CD;
fixed-address 207.175.42.254;
}
}
subnet 192.168.2.0 netmask 255.255.255.0 {
vlan20
的地址池
# --- default gateway
option routers 192.168.2.254;
option subnet-mask 255.255.255.0;
option domain-name "caiwu.com";
option domain-name-servers 222.88.88.88;
option time-offset -18000; # Eastern Standard Time
default-lease-time 21600;
max-lease-time 43200;
host www {
next-server marvin.redhat.com;
hardware ethernet 12:34:56:78:AB:CD;
fixed-address 207.175.42.254;
}
}
subnet 192.168.1.0 netmask 255.255.255.0 {
服務器集羣的地址池
}
配置dhcp中繼服務器
網卡配置
eth0
Link encap:Ethernet HWaddr 00:0C:29:86:4E:97
inet addr:192.168.1.254 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe86:4e97/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:288 errors:0 dropped:0 overruns:0 frame:0
TX packets:180 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:67880 (66.2 KiB) TX bytes:38315 (37.4 KiB)
Interrupt:67 Base address:0x2000
eth1
Link encap:Ethernet HWaddr 00:0C:29:86:4E:A1
inet addr:192.168.2.254 Bcast:192.168.2.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe86:4ea1/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:232 errors:0 dropped:0 overruns:0 frame:0
TX packets:145 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:48752 (47.6 KiB) TX bytes:31455 (30.7 KiB)
Interrupt:67 Base address:0x2080
eth2
Link encap:Ethernet HWaddr 00:0C:29:86:4E:AB
inet addr:192.168.3.254 Bcast:192.168.3.255 Mask:255.255.255.0
inet6 addr: fe80::20c:29ff:fe86:4eab/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:128 errors:0 dropped:0 overruns:0 frame:0
TX packets:144 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:73164 (71.4 KiB) TX bytes:31871 (31.1 KiB)
Interrupt:75 Base address:0x2400
中繼服務器的路由表
root@localhost ~]# route -n
Kernel IP routing table
Destination
Gateway Genmask Flags Metric Ref Use Iface
192.168.3.0
0.0.0.0 255.255.255.0 U 0 0 0 eth2
192.168.2.0
0.0.0.0 255.255.255.0 U 0 0 0 eth1
192.168.1.0
0.0.0.0 255.255.255.0 U 0 0 0 eth0
169.254.0.0
0.0.0.0 255.255.0.0 U 0 0 0 eth2
打開路由功能
[root@localhost ~]# vim /etc/sysctl.conf
7 net.ipv4.ip_forward = 1
8
9 # Controls source route verification
10 net.ipv4.conf.de
[root@localhost ~]# sysctl –p
配置完後打卡路由功能的命令
net.ipv4.ip_forward = 1
net.ipv4.conf.default.rp_filter = 1
net.ipv4.conf.default.accept_source_route = 0
kernel.sysrq = 0
kernel.core_uses_pid = 1
net.ipv4.tcp_syncookies = 1
kernel.msgmnb = 65536
kernel.msgmax = 65536
kernel.shmmax = 4294967295
kernel.shmall = 268435456
配置dhcp中繼服務
[root@localhost ~]# vim /etc/sysconfig/dhcrelay
dhcp中繼的配置文件
# Command line options here
INTERFACES="eth0 eth1 eth2"
DHCPSERVERS="192.168.1.100"
開啓dhcp中繼服務
[root@localhost ~]# service dhcrelay start
Starting dhcrelay:
[ OK ]