Command line dhcp client setup on Ubuntu

While setting up my development server on my company’s network that had recently changed to use a Dynamic Host Configuration Protocol (DHCP) server, I ran into a few linux commands that I used to get my dynamic IP address and I thought I should share these.


This tutorial walks you through the command line dhcp client setup. In this tutorial I will be setting up my client using Ubuntu Linux.


I will assume that you, as I did when I started on my journey to setting up my server, will not have an IP address assigned and that your network interfaces are not defined for your local area network.


Firstly you will need to set up a temporary network configuration. This will be overridden when your server acquires a dynamic IP address from you DHCP server.


The following command sets up an IP address for you network interface eth0, which will be your first network interface card. It also specifies the subnet mask


sudo ifconfig eth0 netmask


Please remember to modify the IP address and subnet mask to match you network specifications.
Verify the your IP address configuration by running the ifconfig command. Running the command should show something that looks like the output below.


ifconfig eth0
eth0      Link encap:Ethernet  HWaddr 00:15:c5:4a:16:5a
          inet addr:  Bcast:  Mask:
          inet6 addr: fe80::215:c6ff:fe4a:165a/64 Scope:Link
          RX packets:466475604 errors:0 dropped:0 overruns:0 frame:0
          TX packets:403172654 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2574778386 (2.5 GB)  TX bytes:1618367329 (1.6 GB)


To configure a default gateway, you can use the route command in the following manner. Modify the default gateway address to match your network requirements.


sudo route add default gw eth0


To verify your default gateway configuration, you can use the route command in the following manner.


route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface   U     1      0        0 eth0         UG    0      0        0


Now, to configure your server to use DHCP for dynamic address assignment, add the dhcp method to the inet address family statement for the appropriate interface in the file /etc/network/interfaces.


auto eth0
iface eth0 inet dhcp


Finally run the ifup command to manually enable the interface and initiate the DHCP process via your DHCP client.


sudo ifup eth0


If done correctly, your DHCP client should be fully configured and running ifconfig again should show your newly acquired dynamic IP address.