Set a Static Ip Address in Ubuntu from Command Line

By marc • May 1st, 2008 • Category: Ubuntu

A common task that you will need to do on a Ubuntu/Debian server is set a static IP address. By default the ip address is set through DHCP, so you will need to change that.

First edit the /etc/network/interfaces file:

  1.  
  2. sudo nano /etc/network/interfaces

Change your file so that it looks similar to the following, but replacing the specific ip addresses with your own.

  1.  
  2. # This file describes the network interfaces available on your system
  3. # and how to activate them. For more information, see interfaces(5).
  4.  
  5. # The loopback network interface
  6. auto lo
  7. iface lo inet loopback
  8.  
  9. # The primary network interface
  10. auto eth0
  11. iface eth0 inet static
  12.         address 192.168.1.102
  13.         netmask 255.255.255.0
  14.         network 192.168.1.0
  15.         broadcast 192.168.1.255
  16.         gateway 192.168.1.1

The next thing you will need to do is set the appropriate nameservers in /etc/resolv.conf.

  1.  
  2. sudo nano /etc/resolv.conf

Your file should look something like this:

  1.  
  2. nameserver xx.xx.xx.xx1
  3. nameserver xx.xx.xx.xx2
  4.  
  5. # use verizon's publicly available nameservers as a backup
  6. nameserver 4.2.2.2

Now, you just need to restart your networking service to kick everything into gear.

  1.  
  2. sudo /etc/init.d/networking restart

Finally, ping a website to make sure that your name resolution works correctly.

  1.  
  2. ping yahoo.com

If you get a response, everything should be all set! That was easy, wasn’t it?

Now, if you find that your /etc/resolv.conf file is getting overwritten when you reboot, you will need to make changes to one last configuration file.

  1.  
  2. sudo nano /etc/dhcp3/dhclient.conf

You will need to add the same nameservers that you set in /etc/resolv.conf on the line that starts with “prepend domain-name-servers” and remove the comment.

The line in the /etc/dhcp3/dhclient.conf will look like this:

  1.  
  2. prepend domain-name-servers xx.xx.xx.xx1, xx.xx.xx.xx2;

marc is a Web Developer in Los Angeles, CA. He likes to receive comments :)
All posts by marc

Leave a Reply