Change Ubuntu Server from DHCP to a Static IP Address
If the Ubuntu Server installer has set your server to use DHCP, you will want to change it to a static IP address so that people can actually use it.
Changing this setting without a GUI will require some text editing, but that’s classic linux, right?
Let’s open up the /etc/network/interfaces file. I’m going to use vi, but you can choose a different editor
sudo vi /etc/network/interfaces
For the primary interface, which is usually eth0, you will see these lines:
auto eth0
iface eth0 inet dhcp
As you can see, it’s using DHCP right now. We are going to change dhcp to static, and then there are a number of options that should be added below it. Obviously you’d customize this to your network.
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.1
Now we’ll need to add in the DNS settings by editing the resolv.conf file:
sudo vi /etc/resolv.conf
On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. (You can do ifconfig /all to find out what they are)
You need to also remove the dhcp client for this to stick (thanks to Peter for noticing). You might need to remove dhcp-client3 instead.
sudo apt-get remove dhcp-client
Now we’ll just need to restart the networking components:
sudo /etc/init.d/networking restart
Ping www.google.com. If you get a response, name resolution is working(unless of course if google is in your hosts file).
Really pretty simple.
Updated Thanks to Nickname007 in the comments for noting that I forgot the DNS entries in the guide.

Daily Email Updates
You can get our how-to articles in your inbox each day for free. Just enter your name and email below:


And what about configuring your router to force that server to recieve that IP address through dhclient?
Why does no body write a complete guide for this? Follow these steps and all sorts of things will break such as web-surfing, updates, mail – basically anything that required name resolution. This is half of what you need to do. Here is the rest.
1. With a dynamic ip address, at the console as root type: ifconfig /all. Write down the IP address for your name server.
2. Follow this guys steps.
3. As root, type: vim /etc/resolv.conf. On the line ‘name server xxx.xxx.xxx.xxx’ replace the x with the IP of your name server. Restart networking. Ping http://www.google.com. If you get a responce, name resolution is working(unless of course if google is in your hosts file).
Good luck people, enjoy.
@NIckname007
Thanks for noticing the missing information! I’ve updated the guide with your comments.
When I follow above directions network will not restart…error ifdown: couldn’t read interfaces file “/etc/network/interfaces” /etc/network/interfaces:2: misplaced option. Same for ifup. Even if change back to original configuration, get same error.
Changes are:
auto lo
iface lo inet loopback
auto eth0
iface eth0 inet static
address 192.168.0.5
netmask 255.255.255.0
network 192.168.0.0
broadcast 192.168.0.255
gateway 192.168.0.1
Should have mentioned further….cannot run ifconfig – same error. Did verify that /etc/resolv.conf does already point to correct nameservers….
Seems to be some other conf file that gets dynamically changed when making the above changes and needs to be corrected, but don’t know where to begin. Any help is greatly appreciated.
HI Charles,
I have just met and solved this problem myself. It had me tearing my hair out and, as usual, it was something small I had overlooked. The error message is telling you that there is a problem with the syntax of the interfaces file. In my case there was a comment mark (#) missing from the second line of the file, right at the top where it gives a bit of info about what the file does – I was concentrating so hard on the meat of the file I didn’t think to look up there for quite a while. Hope this helps.
I have been searching for this exact information for hours! I wish google had psychic powers (coming in 2009)
Worked immediately – excellent.
I found what the problem was for me.. my editor for the TOP commented line of the ‘interfaces’ file had for some reason gone onto the next line -
e.g.
# This file describes the network interfaces available on your
system and how to activate them. For more information, see interfaces(5).
IT SHOULD HAVE BEEN -
# This file describes the network interfaces available on your system and how to activate them. For more information, see interfaces(5).
I JUST DELETED THE CARRAGE RETURN AND IT WORKED A TREAT!
Just a little thing…
You have a typo… it should be “nameserver xxx.xxx.xxx.xxx.” (no space between name and server).
Otherwise spot on.
Thanks!
When i type in
sudo vi /etc/network/interfaces
all i get is a blank screen with the following character
~
~
~
~
~
Can anyone help? Im a noob when it comes to this.
Any Help would be appreciated.
when i type in
sudo vi /etc/resolv.conf
i get the same screen mentioned in the post above by Jose. should i just type in my nameserver xxx.xxx whatnot?
I experienced that it is necessary to remove the dhcp client package in order to prevent overwriting of the resolv.conf file. (dhcp-client or dhcp3-client). Use Aptitude to remove these packages if they are installed. Then the modification to resolv.conf will last.
Good luck.
Thanks, Good article
Works great…. Until my ubuntu server decides to go out and ask my router for a dhcp lease anyways. I still see that “dhclient3″ is still running and listening to eth0. how do I fix that? It’s really annoying when every morning my ip address changes from x.x.x.50 to x.x.x.102 and all of my port forwardings break and I can’t remotely access my box until I go in and do an /etc/init.d/networking restart. please help!
this article was really helpful for a newbie like me.. thanks a lot the responser
This How-To is very helpful. I did need the follow-up comments from Peter Zandbergen and Sheldon Cooper to keep the server from reverting to DHCP. To remove the dhcp client and kill the process:
.
$ sudo apt-get remove dhcp3-client
…
$ ps aux | grep dhcp
dhcp 4294 0.0 0.0 14096 680 ? S
yes, for some reason pico wrapped the top comment line and then saved it…so you get the misplaced option error really quite annoying, I’m glad someone else figured it out and saved me several hours of annoyance!
Thanks home dog.
Thanks! Reading a clear walkthrough makes this stuff a lot easier. Keep writing, i’ll keep reading.
Good stuff.
On my machine the package is called dhcp3-client, not dhcp-client3.
Cheers, worked a treat, had a problem with machine dropping ip address with dhcp, this soon sorted it out
Yeh about the router. Wouldn’t you still have to config it a static ip too. I have cable and its set to dynamic. If so is there any way I can find my dns server name (ie xxxx.xxxx.xxxx.xxxx) without contacting my isp provider.
HI all……….I tried the as given below but error comes every time same…
After vi/etc/network/interfaces I made changes like
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
After I restart the network
sudo /etc/init.d/networking restart
I got the following error msg
root@excel:~# sudo /etc/init.d/networking restart
* Reconfiguring network interfaces… /etc/network/interfaces:1: misplaced option
ifdown: couldn’t read interfaces file “/etc/network/interfaces”
/etc/network/interfaces:1: misplaced option
ifup: couldn’t read interfaces file “/etc/network/interfaces”
[fail]
I tried this
ls -lah /etc/network/interfaces
Iget
root@excel:~# ls -lah /etc/network/interfaces
-rwxrw-rw- 1 root root 202 2008-07-30 12:05 /etc/network/interfaces
Then I tried this one
sudo chown root:root /etc/network/interfaces
After I add #
# The loopback network interface
auto lo
iface lo inet loopback
#
# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
I get same error any one can help me….
thanks,
ricky
Thanks for the article. It’s guys like you that make Linux bearable.
This worked straight away. Thanks.
I had the misplaced option file problem as well. Turns out, at the very bottom of my interfaces file (like 20 returns down) it had repeated the options for eth0, which must have been confusing the OS. so make sure there’s nothing hidden at the bottom of your file.
I have a proxy server with two interfaces. The Internet interface gets its IP via DHCP from a DSL modem on a 10.0.0.0 network, the other is static 192.168.1.0 LAN network which is setup as this article.
Therefore I cannot get rid of the DHCP client.
Thanks very much. Your tips helped me significantly.
Thanks alott !
Good explanation ! and Works perfectly !
In Ubuntu server 9 (and perhaps earlier versions) the dhcp client package is called dhcp3-client and not dhcp-client3 as it is stated in the step-by-step guide.
Thanks for the guide though.
Please correct the correction… it is dhcp3-client, not dhcp-client3.
There is no space between name & server in *name server xxx.xxx.xxx.xxx”.
Thanks for the guide!
If you still have to use dhcp, then the client can be started with an option so as not to overwrite /etc/resolv.conf (and maybe some others). Errr, I wish I could be more specific but I just uninstalled dhcp
. Thanks for the guide.
After making changes to file, how do you save your changes?
absolutely exelent!!!!!
ur info is exactly useful
thanks a lot and more power!!!!!!!
i really love it!!!!!!!!
thanks you very much.
ifconfig -a (and not ifconfig /all)
u wrote:
(You can do ifconfig /all to find out what they are)
in fact u mixed Linux and Windows
Under Linux:
ifconfig -a
Under Windows:
ipconfig /all
but the tutorial was pretty good
thanks.
I wouldn’t recommend removing dhcp-client
What if one day u want to get an IP from ur DHCP, what if ur PC has more than one nic and one needs a DHCP IP ?
On a newly installed ubuntu ur primary eth is in dhcp, then u set it static by updating /etc/network/interface (u can edit resolv.conf also for dns) following given instructions on this page, then try a :
ps ax|grep dhc
u probably will see a running dhclient3 process running
or a dhclient if u runned it by hand
kill them respectively with :
killall dhclient3
killall dhclient
then a “ps ax|grep dhc” should show no dhcp client running.
Now ur server is set to ur static IP and wont request dhcp IPs anymore, even on reboot.
but u still can request for a dhcp IP by hand.
It is not necessary to uninstall the dhcp-client. Just ensure you use the spaces before the lines
address, … in the interfaces file as shown above, then it works.