嵌入式linux中的dhcp伺服器

來源:互聯網
上載者:User

嵌入式系統下,用開發板建立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都已經給出例子了,

      [zhh@localhost 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地址

    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

    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版本。

    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

      

    4、將產生的udhcpc複製到開發板/sbin下,在控制台下運行udhcpc即可,很快就可以從DHCP SERVER獲得IP地址。但是有個問題,開發板可以獲得IP地址,在路由器裡也可以看到分配地址了,問題是一使用PING命令,不管是PC 機PING 開發板,還是開發板PING PC 機,就會造成開發板死機。試了好多方法,檢查ROUTE,檢查ARP,修改Default.script
    ,始終無法解決。

     

    花了一整天時間,總算完全解決了。

    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

    聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.