Windows XP Windows 7 Windows 2003 Windows Vista Windows教程綜合 Linux 系統教程
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟件教程
 Windows教程網 >> Linux系統教程 >> Linux教程 >> linux下的DHCP配置基礎教程

linux下的DHCP配置基礎教程

日期:2017/2/7 14:28:36      編輯:Linux教程
 

今天來學習配置Linux下的DHCP服務!

DHCP服務器軟件,http://www.isc.org 開發機構
ftp://ftp.isc.org/isc/dhcp 中可以下載到最新版本 目前為 dhcp-4.2.1-P1.tar.gz 安裝http://ftp.isc.org/isc/dhcp/dhcp-4.2.1-P1.tar.gz
下面進行安裝
[root@tony soft]# tar xvzf dhcp-4.2.1-P1.tar.gz
[root@tony soft]# cd dhcp-4.2.1-P1.tar.gz
[root@tony dhcp-4.2.1]# ./configure
[root@tony dhcp-4.2.1]# make
[root@tony dhcp-4.2.1]# make install
[root@tony dhcp-4.2.1]# vi /etc/dhcpd.conf //手動創建配置文件,很關鍵,具體參考# man dhcpd.conf
ddns-update-style none; //動態DNS的更新方式,必須添加,否則服務器無法啟動
subnet 192.168.0.0 netmask 255.255.255.0 { //定義網段的服務范圍,與服務器所在IP段在同一范圍
option routers 192.168.0.1; //默認網關
option subnet-mask 255.255.255.0; //子網掩碼
option domain-name “crazycen.com”; //域名
option domain-name-servers 192.168.0.100; //DNS服務器
range 192.168.0.2 192.168.0.99; //地址池范圍,注意已經使用的固定地址要排除在外
default-lease-time 21600; //默認釋放時間 1/4天
max-lease-time 43200; //最長釋放時間
host mail {
hardware ethernet 00:03:FF:B6:9E:AB; //這部分定義了靜態地址,mac與IP綁定,該IP地址不要放在地址池中,每個靜態地址要建一個host
fixed-address 192.168.0.200;
}
}
啟動服務器時報
WARNING: Host declarations are global. They are not limited to the scope you declared them in.
Can’t open lease database /usr/local/var/db/dhcpd.leases: No such file or directory —
check for failed database rewrite attempt!
[root@tony dhcp-4.2.1]# mkdir /usr/local/var
[root@tony dhcp-4.2.1]# mkdir /usr/local/var/db
[root@tony dhcp-4.2.1]# touch /usr/local/var/db/dhcpd.leases //建立記錄地址分配信息的文件,記錄了IP地址是否已經分配等信息,沒有該文件,服務器無法啟動
[root@tony dhcp-4.2.1]# dhcpd
dhcpd: Error opening ‘/proc/net/if_inet6’ to list IPv6 interfaces; No such file or directory
[root@tony dhcp-4.2.1]# make uninstall //猜測最新版本的bug,安裝5月份發布的dhcp-4.2.1-P1.tar.gz 步驟同上,直接成功啟動

[root@tony dhcp-4.2.1]# dhcpd
Internet Systems Consortium DHCP Server 4.2.1
Copyright 2004-2011Internet Systems Consortium.
All rights reserved.
For info, please visit http://www.isc.org/sw/dhcp/
WARNING: Host declarations are global. They are not limited to the scope you declared them in.
Wrote 0 deleted host decls to leases file.
Wrote 0 new dynamic host decls to leases file.
Wrote 0 leases to leases file.
Listening on LPF/eth0/00:03:ff:b6:9e:ab/192.168.0.0/24
Sending on LPF/eth0/00:03:ff:b6:9e:ab/192.168.0.0/24
Sending on Socket/fallback/fallback-net

[root@happyboy db]# pwd
/usr/local/var/db
[root@happyboy db]# ll //而且該庫文件也是自動生存的,並且都有內容
總用量 8
-rw-r–r– 1 root root 127 5月 25 8:15 dhcpd.leases
-rw-r–r– 1 root root 127 5月 257:23 dhcpd.leases~
[root@tony db]# more 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.0.0a2
[[email protected]]# ps -aux |grep dhcpd
root 7969 0.0 0.8 4648 2400 ? S 18:15 0:00 dhcpd
root 7973 0.0 0.2 4928 668 pts/2 S 18:18 0:00 grep dhcpd

windows客戶端測試
C:\Documents and Settings\liuzhangcai>ipconfig /renew

Windows IP Configuration

Ethernet adapter 本地連接:

Connection-specific DNS Suffix . : happyboy.net.cn
IP Address. . . . . . . . . . . . : 192.168.0.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1

C:\Documents and Settings\liuzhangcai>ipconfig/all

Windows IP Configuration

Host Name . . . . . . . . . . . . : MyServer
Primary Dns Suffix . . . . . . . :
Node Type . . . . . . . . . . . . : Unknown
IP Routing Enabled. . . . . . . . : Yes
WINS Proxy Enabled. . . . . . . . : No
DNS Suffix Search List. . . . . . : happyboy.net.cn

Ethernet adapter 本地連接:

Connection-specific DNS Suffix . : happyboy.net.cn
Description . . . . . . . . . . . : Intel(R) PRO/1000 PL Network Connection
Physical Address. . . . . . . . . : 00-15-F2-BD-9E-AB
DHCP Enabled. . . . . . . . . . . : Yes
Autoconfiguration Enabled . . . . : Yes
IP Address. . . . . . . . . . . . : 192.168.0.2
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 192.168.0.1
DHCP Server . . . . . . . . . . . : 192.168.0.200
DNS Servers . . . . . . . . . . . : 192.168.0.200

[root@tony root]# more /usr/local/var/db/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.2.1
lease 192.168.0.2 {
starts 1 2011/05/25 10:22:19;
ends 1 2011/05/25 16:22:19;
cltt 1 2011/05/25 10:22:19;
binding state active;
next binding state free;
hardware ethernet 00:15:f2:bd:9e:ab;
uid “\001\000\025\362\275\236\253”;
client-hostname “MyServer”;
}

也可以通過其他方式確認DHCP服務啟動
[root@tonyroot]# grep bootp /etc/services
bootps 67/tcp # BOOTP server
bootps 67/udp
bootpc 68/tcp # BOOTP client
bootpc 68/udp
[root@tony root]# ps -aux |grep dhcpd
root 7969 0.0 0.8 4656 2228 ? S 18:15 0:00 dhcpd
root 8132 0.0 0.2 4916 672 pts/3 S 18:33 0:00 grep dhcpd
[root@tony root]# netstat -unl |grep 67
udp 0 0 0.0.0.0:67 0.0.0.0:*
linux客戶機配置
[root@tony root]# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
ONBOOT=yes
BOOTPROTO=dhcp //設置為dhcp狀態即可
或者使用
[root@tony root]# netconfig //設置為dhcp 自動獲取,然後重新啟動network服務
[root@tony root]# service network restart
服務器端調試:
[root@happyboy root]# tail -f /var/log/messages //實時查看客戶端請求分配信息
[root@tony root]# more /usr/local/var/db/dhcpd.leases //查看庫文件

客戶端:
linxu/unix:dhclient eth0 //刷新客戶端的dhcp信息,同windows的ipconfig/renew
windows: ipconifg /renew

Copyright © Windows教程網 All Rights Reserved