Subscribe to How-To Geek

Recommended: Click Here to Run a Free Scan for Common PC Errors   [Sponsored Link]

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)

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.

The Geek is the founder of How-To Geek and a geek enthusiast. When he's not coming up with great how-to articles, he's probably writing at his personal blog. This article was written on 12/13/06 and tagged with: Ubuntu, SysAdmin

Comments (16)

  1. Brett

    And what about configuring your router to force that server to recieve that IP address through dhclient?

  2. Nickname007

    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 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.

  3. The Geek

    @NIckname007

    Thanks for noticing the missing information! I've updated the guide with your comments.

  4. Charles Tucker

    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

  5. Charles Tucker

    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.

  6. twodinners

    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.

  7. SaltyMule

    I have been searching for this exact information for hours! I wish google had psychic powers (coming in 2009)

  8. Torsten

    Worked immediately - excellent.

  9. Alexis

    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!

  10. Theo

    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!

  11. Jose

    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.

  12. Brian Marquardt

    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?

  13. Peter Zandbergen

    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.

  14. qu4ck

    Thanks, Good article

  15. Sheldon Cooper

    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!

  16. sota

    this article was really helpful for a newbie like me.. thanks a lot the responser


Leave a Comment




Leave your friendly comment here. If you have a computer help question, leave it on the forums instead.

Note: Your comment may not show up immediately on the site.

Copyright © 2006-2008 HowToGeek.com. All Rights Reserved.