How to use DHCP in Linux


How to use DHCP in Linux

DHCP is the Dynamic Host Configuration Protocol, which allows a networked machine to obtain its IP address and other information automatically from a DHCP server.

There are two methods through which a Linux system can obtain an IP address using DHCP:
1. Kernel level configuration.
2. User level configuration.

Kernel level auto-configuration:

To use kernel level auto configurtion select below options in the Linux kernel configuration.

-> Networking support (NET [=y])
-> Networking options
-> TCP/IP networking (INET [=y])
[*]   IP: kernel level autoconfiguration
[*]     IP: DHCP support

Advantage:
In case of your root file system in NFS and wanted to have a IP dynamicaly configirued though DHCP, kernel level DHCP will come to rescue you. Using kernel level DHCP you will have have a valid IP address by the time the Linux kernel have completed the booting and can immediately mount an NFS root file system.

Disadvantage:
1. Can only configure basic network parameters, which do not include items such as DNS servers.

User level auto-configuration

I will use DHCP client applet that comes Busybox named udhcpc

As per Busybox:

udhcpc is a DHCP client geared primarily toward embedded systems, while striving to be fully functional and RFC compliant. The udhcp client negotiates a lease with the DHCP server and runs a script when a lease is obtained or lost.

User level auto-configuration is much more flexible, but has the does require that there is a root file system available when the system has finished booting which does not depend on the network being configured. This file system is usually on a Flash, SD/MMC, etc.,

In order to use udhcpc, the Linux kernel must be configured with packet socket support (CONFIG_PACKET):

-> Networking support (NET [=y])
-> Networking options
<*> Packet socket

Once the udchpc obtained or lost lease it will run a script. This script should configure the system ip, gateway, and resolv.conf etc. The busybox provides sample scripts, which is placed in the examples/udhcp/ directory.

copy the sample scripts to /etc/udhcpc/ of your device.

edit the file /etc/udhcpc/sample.script changed the path where the scripts are located.

exec /etc/udhcpc/sample.$1

Create a dummy script named sample.fail

$ touch /etc/udhcpc/sample.fail

Now start the DHCP client.

$ udhcpc -i wlan0 -s /etc/udhcpc/sample.script
Configuring IP using DHCP
udhcpc (v1.18.3) started
Sending discover...
Sending select for 192.168.7.10...
Lease of 192.168.7.10 obtained, lease time 604800
deleting routers
route: ioctl 0x890c failed: No such process
adding dns 192.168.7.1
Initializing completed

That’s it.

Advertisements

5 thoughts on “How to use DHCP in Linux

  1. Hi Mohamed,

    I used the steps mentioned above to get an IP address using dhcpc. udhcpc able to lease the IP address but can not bind to the device. Below are the logs.
    udhcpc: started, v1.25.0
    udhcpc: sending discover
    udhcpc: sending select for 10.138.135.202
    udhcpc: lease of 10.138.135.202 obtained, lease time 90000

    The logs which you attached it tries to attach DNS but in my case it do nothing after taking lease.

    Could you give some pointers debug the issue?

  2. Hai, I follow the same above steps. But unable to obtain ip for wlan0. But it is configuring ip for eth0. Please post the configuration files for Wi-Fi

    1. It should be there in busybox. got to this link http://busybox.net/downloads/BusyBox.html and search for udhcp.

      May be it is hided because of dependency, use as below. Start the ncursers based menuconfig then press / to active search, in the input box enter udhcp and it should show the details of the package and search match.

      All the best.

  3. Hi,
    I work in same field and involved in such works. If you are searching some one who can do work for you try freelancing website. like http://www.freelancer.com.

    Otherwise contact some consulting companies, even the company I work does embedded software/hardware consulting (www.e-consystems.com). It is better to rely on companies.

    All the best.

    Best Regards,
    Mohamed Thalib h.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s