嵌入式系統下,用開發闆建立dhcp伺服器
1.重新編譯核心,添加對dhcp的支援
2.busybox是否帶有dhcp的應用,如無,則需要重新編譯busybox,或者直接下一個獨立的也可。
3.dhcp伺服器在etc目錄下的配置檔案,udhcpd.conf 需要配置這個檔案,包括配置設定的ip,dns等。
我的檔案系統中直接具有dhcp的軟體,分别為udhcpd和udhcpc,前者是伺服器端的,後者是用作用戶端的,這樣就可以直接配置udhcpd.conf了。參考busybox中的examples,配置好udhcpd.conf,然後執行
udhcpd -fS /etc/udhcpd.conf
就可以啟動dhcp伺服器了,當然,中間也碰到了一個小問題,這時udhcpd在執行的時候提示沒有一個叫做udhcpd.leases的檔案,這個隻要在相應的檔案夾下直接建立以下即可。
一切順利的話,就可以看到開發闆為pc配置設定ip,gw和dns了!
///
**************dhcp
開發闆上使用的udhcpc udhcpd兩個工具都可以從busybox裡面直接編譯得來
PC上使用的dhcp工具可以使用 sudo apt-get install udhcpc
sudo apt-get install udhcpd
用PC做udhcpd伺服器,開發闆從PC擷取ip(反過來是一樣的)
PC上: sudo vi /etc/udhcpd.conf
udhcpd.conf在busybox裡面有一個原型: busybox-1.14.2/examples/udhcp/udhcpd.conf
修改三個地方:start/end/option dns
start 192.168.4.200 #default: 192.168.0.20
end 192.168.4.254 #default: 192.168.0.254
//配置設定ip的區間
#Examles
opt dns 192.168.1.2 192.168.1.10
option subnet 255.255.255.0
opt router 192.168.1.2
opt wins 192.168.1.10
option dns 192.168.4.127 # appened to above DNS servers for a total of 3
option domain local
option lease 864000
//option dns 192.168.4.127做伺服器的機器的ip位址
用網線把PC和開發闆連接配接起來。
好了之後 sudo udhcpd
開發闆上運作 udhcpc 這樣就可以從PC擷取ip
/
由于要使用網絡通訊,是以不可避免的要用到dhcp。理想的網絡通訊方式是下面3種都要支援:
1,接入已有網絡。這便要求可以作為dhcp用戶端。
2,作為DHCP伺服器,動态配置設定IP。
3,指定固定IP
第3種情況沒有什麼好說的,簡單說下前2種情況。
使用步驟:
(1)在核心的網絡項裡面把DHCP配置上;
(2)在busybox裡面把
-
udhcp server(udhcpd)
-
udhcp client(udhcpc)都選上。
udhcpd就是終端裝置作為DHCP伺服器
udhcpc就是終端裝置作為DHCP用戶端
busybox裡面對dhcp都已經給出例子了,
[[email protected] busybox-1.14.1]$ ls ./examples/udhcp/
sample.bound sample.deconfig sample.nak sample.renew sample.script simple.script udhcpd.conf
比如使用udhcpc時
就可以直接把simple.script拿來使用,改不改名字都可以,busybox裡面預設的目錄檔案是/usr/share/udhcpc/default.script
可以檢視下幫助
# udhcpc --help
BusyBox v1.14.1 (2010-01-22 10:35:16 CST) multi-call binary
Usage: udhcpc [-Cfbnqtvo] [-c CID] [-V VCLS] [-H HOSTNAME] [-i INTERFACE]
[-p pidfile] [-r IP] [-s script] [-O dhcp-option]... [-P N]
-V CLASSID Vendor class identifier
-i INTERFACE Interface to use (default: eth0)
-H,-h HOSTNAME Client hostname
-c CLIENTID Client identifier
-C Suppress default client identifier
-p file Create pidfile
-r IP IP address to request
-s file Run file at DHCP events (default /usr/share/udhcpc/default.script)
-t N Send up to N request packets
-T N Try to get a lease for N seconds (default 3)
-A N Wait N seconds (default 20) after failure
-O OPT Request DHCP option OPT (cumulative)
-o Do not request any options (unless -O is also given)
-f Run in foreground
-b Background if lease is not immediately obtained
-S Log to syslog too
-n Exit with failure if lease is not immediately obtained
-q Quit after obtaining lease
-R Release IP on quit
-P N Use port N instead of default 68
-a Use arping to validate offered address
#
這樣就很好了解了。
下面就說說使用udhcpd,同樣可以直接把busybox自帶的udhcpd.conf作為參考,怎麼設定該檔案就不詳述了,去參考相關文檔即可。看看udhcpd怎麼使用
# udhcpd --help
BusyBox v1.14.1 (2010-01-22 10:35:16 CST) multi-call binary
Usage: udhcpd [-fS] [-P N] [configfile]
DHCP server
-f Run in foreground
-S Log to syslog too
-P N Use port N instead of default 67
幫助一目了然。
我使用的時候直接
#udhcpd /etc/udhcpd.conf
即可。
還有一個租約檔案要簡要說說,使用udhcpd時會要求建立一個租約檔案,按照提示建立一個就行了,它起什麼作用呢?就是為了記錄用戶端所獲得的IP,如果沒有租約檔案,則會重新給用戶端配置設定一個新IP,如果有,則使用原先配置設定的IP。
//
嵌入式Linux系統下使用DHCP,為開發闆自動配置設定IP位址
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukDMwAzXq9CX4p2LclGavwVbvNmL1RWahJmLn1Wavw1LcpDc0RHaiojIsJye.gif)
1、首先确認Linux核心編譯生成時選擇了DHCP
Networking --->
Networking support
Networking options --->
Packet socket //添加.配置CONFIG_PACKET
[ * ] IP: DHCP support //添加
[ * ] Network packet filtering (replaces ipchains) ---> //添加,後面子選項可不選,配置CONFIG_NETFILTER
說明:若沒選 Packet socket, [ * ] Network packet filtering (replaces ipchains) --->選項,在執行udhcpc指令時出現如下錯誤:
~ # udhcpc
udhcpc (v0.9.9-pre) started
udhcpc[208]: udhcpc (v0.9.9-pre) started
FATAL: couldn't listen on socket, Address family not supported by protocol
udhcpc[208]: FATAL: couldn't listen on socket, Address family not supported by protocol
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukDMwAzXq9CX4p2LclGavwVbvNmL1RWahJmLn1Wavw1LcpDc0RHaiojIsJye.gif)
2.編譯Busybox,使make menuconfig,添加以下選項:
Networking Utilities --->
udhcp Server/Client --->
[ ] udhcp Server (udhcpd) //在此不作服務端,故不選。生成udhcpd指令
udhcp Client (udhcpc) //生成udhcpc指令
[ ] Lease display utility (dumpleases)
[ ] Log udhcp messages to syslog (instead of stdout)
[ ] Compile udhcp with noisy debugging messages
若busybox沒編譯相應選項,也可從網上下載下傳相應檔案,用arm-linux交叉編譯得到udhcpd,udhcpc指令copy到usr/sbin下就可以了。
說明:有種方法說是從網上下的udhcp_0.9.8cvs20050303.orig.tar.gz檔案,解壓後修改Makefile檔案,
在19行添加CROSS_COMPILE=arm-linux-
注釋12行的COMBINED_BINARY=1,
否則不生成udhcpc指令。
注:使用0.9.8版本的udhcp貌似也不行,重新編譯Busybox1.9.2版本。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukDMwAzXq9CX4p2LclGavwVbvNmL1RWahJmLn1Wavw1LcpDc0RHaiojIsJye.gif)
3、使用Busybox下的simple.script,/usr/share/udhcpc/下,并重命名為default.script,修改default.script中RESOLV_CONF="/etc/resolv.conf"為RESOLV_CONF="/tmp/resolv.conf"
說明:從網上下了幾個default.script貌似無法使用,提示無法找到default.script檔案,使用上面的方法較好。由于使用Cramfs檔案系統,etc目錄不可寫,運作udhcp.c時會提示無法建立resolv.conf
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukDMwAzXq9CX4p2LclGavwVbvNmL1RWahJmLn1Wavw1LcpDc0RHaiojIsJye.gif)
4、将生成的udhcpc複制到開發闆/sbin下,在控制台下運作udhcpc即可,很快就可以從DHCP SERVER獲得IP位址。但是有個問題,開發闆可以獲得IP位址,在路由器裡也可以看到配置設定位址了,問題是一使用PING指令,不管是PC 機PING 開發闆,還是開發闆PING PC 機,就會造成開發闆當機。試了好多方法,檢查ROUTE,檢查ARP,修改Default.script ,始終無法解決。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIml2ZukDMwAzXq9CX4p2LclGavwVbvNmL1RWahJmLn1Wavw1LcpDc0RHaiojIsJye.gif)
花了一整天時間,總算完全解決了。
1、先是懷疑busybox的問題,OK,開發闆上原來移植的是1.9.2 版本,上網找找了發現最新有1.14.0 版本的,詳細移植見另一篇文章
2、Busybox1.9.2 版本貌似是有問題,udhcpc 之後,通過ifconfig 檢視IP 資訊可以看到廣播位址Bcast有問題,始終是255.255.255.255,正确應該是192.168.1.255(網段為192.168.1),使用Busybox1.14.0就沒有這個問題了,但問題是PING 仍然無法使用。
3、偶然情況下發現udhcpc 之後需要關閉網卡,之後再重開網卡使用指令
ifconfig eth0 down
ifconfig eht0 up
之後就能正常工作了,還有問題是無法PING本機,設定回環位址就可以了
ifconfig lo 127.0.0.1