DHCP is the Dynamic Host Configuration Protocol, and allows a networked machine to obtain its IP address and other information automatically. For a full description of DHCP, see the DHCP mini-HOWTO. For the full specification, see RFC2131.
There are two methods through which a Linux system can obtain an IP address using DHCP: kernel level configuration and user level configuration.
Using kernel level auto-configuration of the network interfaces has the advantage that when the kernel has finished booting it will already have a valid IP address and can immediately mount an NFS root file system. One disadvantage of this technique is that the kernel only supports BOOTP level configuration. Most DHCP servers can be configured to respond to BOOTP requests, but this must be enabled by the network administrator. Another disadvantage is that BOOTP can only configure basic network parameters, which do not include items such as DNS servers.
To use kernel level configuration, alter the boot script as described in Booting the kernel by removing the definitions of TARGETIP, GWIP and NETMASK, and change the value of AUTOCONF to bootp.
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 hard drive or a RAM or ROM disk.
The STLinux distribution supplies both a DHCP client and server, using the udhcp package, a small DHCP implementation which is tailored for use in embedded systems. This section describes how to set up udhcpc, the client program, to obtain an IP address. For more details, including how to set up a DHCP server, please see the project's home page,
In order to use udhcpc, the Linux kernel must be configured with packet socket support (CONFIG_PACKET):
Device Drivers --->
Networking support --->
Networking options ---> Packet SocketNormally udhcpc is not run directly, but through ifup and ifdown. These use the configuration file /etc/network/interfaces to determine what to do when starting and stopping interfaces. Through this file most aspects of a network interface can be configured, including IP addresses, gateways, routing tables and interaction with firewalls. The file is well commented with several examples.
To simply bring up a new interface using DHCP, add the line:
iface <interface> inet dhcp
to /etc/network/interfaces, changing <interface> as required (this will usually be eth0).
To test it run:
target# ifup eth0If the interface is to be started automatically at boot time, add the line:
auto <interface>
to /etc/network/interfaces. This will indicate that ifup -a should bring up this interface, which is usually invoked by one of the start up scripts run by init.