Linux Basics - Set A Static IP On Ubuntu

Want to support HowtoForge? Become a subscriber!
 
Submitted by galiclick (Contact Author) (Forums) on Mon, 2012-04-16 09:20. :: Ubuntu

Linux Basics - Set A Static IP On Ubuntu

This tutorial explains how to set a static IP on an Ubuntu system from the command line.

 

Step 1

In this step you will manually configure your network interface by editing the following files using your preferred text editor(nano gedit vi). For the purpose of this example I am using the "nano" editor. You can edit the appropriate file by entering the following command into the terminal:

You can copy and paste directly from this line.

sudo nano /etc/network/interfaces

Enter your root password, once your prefered editor opens the file you can see...

auto lo eth0
iface lo inet loopback
iface eth0 inet dynamic

If you have more than one network card, please be sure to make changes to the correct network interface.

Statically configured network cards will have a section like:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
        address xxx.xxx.xxx.xxx(enter your ip here)
        netmask xxx.xxx.xxx.xxx
        gateway xxx.xxx.xxx.xxx(enter gateway ip here,usually the address of the router)

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: interfaces

<---ENTER

Here is an example:

auto lo eth0
iface lo inet loopback
iface eth0 inet static
	address 192.168.1.101
	netmask 255.255.255.0
	gateway 192.168.1.1

 

Step 2

In this step you will manually configure your dns configuration file.

sudo nano /etc/resolv.conf

Once your editor opens the file you want to enter the following information...

nameserver xxx.xxx.xxx.xxx(enter your dns server ip)
nameserver xxx.xxx.xxx.xxx(enter your alt dns server ip)

If you use "nano" editor, type Ctrl+x to save changes.

Save modified buffer (ANSWERING "No" WILL DESTROY CHANGES) ?

<---Type "y"

File Name to Write: resolv.conf

<---ENTER

Here is an example:

nameserver 213.60.205.175
nameserver 213.60.205.173

 

Step 3

Manually restart your network interface with the new settings:

sudo /etc/init.d/networking restart

This should return a result that looks like the following:

*Reconfiguring network interfaces… [OK]

At this point you can check if the settings are correct:

ifconfig

If everything is correct you will get this result.

eth0      Link encap:Ethernet  direcciónHW 00:33:27:46:2v:34
          Direc. inet:192.168.1.101  Difus.:0.0.0.0  Másc:255.255.255.0
 ...

See you...


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by brubakes (not registered) on Sat, 2013-10-19 15:27.
Just what I needed.  Thanks!
Submitted by Anonymous (not registered) on Sun, 2013-04-28 15:34.

Hmm any idea why you don't just do everything within the /etc/network/interfaces configuration folder like here: http://draalin.com/setting-up-a-static-ip-address-in-ubuntu/

 Or does it make more sense to actually split things up like you did within the resolv.conf and interfaces configuration folder?

Submitted by Ranting (not registered) on Tue, 2012-10-09 21:06.

I used to use the command line method before Ubuntu 12.04 but now prefer the network manager method of setting a static IP.

This is only because after using this method then opening the connection manager to check what was showing (I know I could have checked on cli but dont like to be limited!) all the settings were reset to dhcp and my modifications were reset!

I didn't really fancy removing the network manager. Anybody else feel like they are being forced to use graphical methods more and more with newer Ubuntu releases?

Submitted by Ranting (not registered) on Tue, 2013-11-26 23:46.
These instructions are slightly dated now and a lot of other instructions I'm seeing fail to address Network Manager managing the connection.
 
This article about setting a static IP in Ubuntu the proper way was very useful for me.
 
It's useful to note that the instructions on many sites and askubuntu answers do not apply to wireless devices!
 

 

 

Submitted by Freaki (not registered) on Sat, 2012-09-15 03:22.

I dunno about you but I don't like using the resolv.conf, I prefer to set the the nameserver to read my routers, and typically I'll use google's 8.8.8.8 or 8.8.4.4

 Heres a small guide I found if anyone is interested.

 http://ubuntuserverhelp.com/setting-up-a-static-ip/

Submitted by Katz (not registered) on Mon, 2012-04-16 16:18.
What´s the ubuntu version, 11.10 or 12.04?
Submitted by Pilgrim (not registered) on Mon, 2012-04-16 11:16.

This command is deprecated on new debian and ubuntu:

/etc/init.d/networking restart

solution is a use command:

ifdown eth0 && ifup eth0

See #565187 netbase: Command deprecated and not explained

Submitted by Anonymous (not registered) on Tue, 2013-12-10 05:52.
Bless you.  I saw that error and was noticing I had to completely restart to get the command to take.  I was not liking that and once I scrolled down to see if someone saw the same, I found your post.  Thank you so much!