Tuesday, July 2, 2013

How to Change an Ubuntu Server from DHCP-Assigned IP Address to Static IP Address

I noticed, while perusing a variety of blog posts related to Ubuntu server configuration, that none of them completely covered the necessary steps in changing the server's IP address from one assigned by DHCP to one configured statically. This blog post will explain how to do it, along with several follow-up steps that seem to be missing from other posts.

For this post, I used Ubuntu Server version 12.04.

It's common practice for servers to be configured with static IP addresses, so if your Ubuntu server is currently configured to receive its IP address from a DHCP server, you probably want to change it to use a static (manually configured) IP address.

There are four steps:
  • Use a text editor to modify /etc/network/interfaces
  • Restart networking
  • Verify the change to the IP address and the DNS client
  • Disable the DHCP client, if desired

Companion Video

I created a video demonstrating the process, in case you prefer to watch a video instead of reading the steps.

Configuration Steps

Modify /etc/network/interfaces

Use the following command to open /etc/network/interfaces for editing:
sudo vi /etc/network/interfaces
It's certainly not required for you to use the vi text editor, but it's my preference. Any text editor will work.
Under primary network interface, change dhcp to static and add the desired address, mask, gateway, DNS server(s), and DNS default search domain. In the example, the server's IP address is with a 24-bit mask, the gateway is at, the IP addresses for the DNS servers are the OpenDNS servers, and the default search domain is soundtraining.net. Presumably, you'll use different addresses that appropriate for your environment.
When you're finished, the file should look similar to this:
How to configure a static IP address on Ubuntu server

Restart Networking

Use the following command to restart networking:
sudo service network-interface restart INTERFACE=eth0

Verify the Changed Configuration

Use the ifconfig command to verify the changed configuration.
Also, view the contents of /etc/resolv.conf to verify that it picked up the DNS configuration from the configuration changes. It should look similar to this:
Notice that the DHCP-assigned DNS server is still listed. That's because the DHCP client is still running.

Disable the DHCP Client

It's not required to disable the DHCP client and, in fact, you may want to have the DHCP server supply additional DNS servers. If, however, you want to, you can disable the DHCP client (dhclient), if desired, with the dhclient -r command. It's usually a good idea to restart networking after making changes like this:
sudo service network-interface restart INTERFACE=eth0

For More Information

Pick up a copy of either of my Linux books. If you're working with Red Hat-based operating systems such as RHEL, Fedora, or CentOS, The Accidental Administrator: Linux Server Step-by-Step Configuration Guide is designed to take you from initial installation through various types of server configurations. My other Linux book is Tweeting Linux: 140 Linux Configuration Commands Explained in 140 Characters or Less, a Linux command reference for people who want concise explanations of various Linux commands. Tweeting Linux is applicable to Red Hat, Ubuntu, and Debian-based systems.


Unknown said...

Just a little textual change.
'Under primary network interface, change static to dhcp and add the '
should read
'Under primary network interface, change dhcp to static and add the '

Unknown said...

Hans, thank you. Good catch. It's fixed now.

Mike2k said...

Hi Don,

Your post is great by pointing out the last step: "disable dhclient" if desired.

I followed your instructions and set eth0 to a static ip: about 50 days ago on a Ubuntu 16.04 server. At that time I did not disable dhcp client. Today I can not ssh into the server anymore, then found the server's ip address changed to which I do not understand why. I greped the /var/log/ and got:

syslog.1:Jul 26 19:17:06 scens dhclient[1041]: DHCPREQUEST of on eth0 to port 67 (xid=0x46a59385)

Does this mean if I had disabled dhclient, the static ip address should stay FOREVER?