How To Set A Static IP In Ubuntu or Linux Using The Command Line

Posted by & filed under IT Administration, Linux, Technology.

After installing Ubuntu Linux 7.10 Server Edition, I found that the IP address was assigned by DHCP served by my home router. This is fine for getting the server installed, but under most conditions, you will want to assign a static IP for your server. This Blog post will show you how to do just that. One of the issues with the Ubuntu server editions is that everything is done from a command line. There is no pretty windowed GUI.

One thing to note is that these commands need to be issued from either root or using sudo. I prefer to use the sudo su method to avoid having to type sudo each time I issue a command. (WARNING) if you do use sudo su, remember to exit the root when you are done.

In this example, I will be assigning the a static IP address of 192.168.1.2 to the primary network interface on my server.
My network subnet is 192.168.1.0
My network gateway is 192.168.1.3
And my netmask is 255.255.255.0

1. Gain root privledges

sudo su

Enter your password when prompted You should now have root access.

2. Backup your interfaces file

cp /etc/network/interfaces /etc/network/interfaces.backup

It is important to always create a backup of important files in case you need to restore your original files/settings.

3. Edit the Interfaces file
Now using a text editor, modify the /etc/network/interfaces file. I prefer to use nano, but you can use any editor you prefer. For this example, I am going to use 192.168.1.2 as my static ip address.

The original the interfaces file looks like this:

# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface

auto lo
iface lo inet loopback

# The primary network interface

auto eth0
iface eth0 inet dhcp

To set a static IP, you need to change the line under “The primary network interface” to something like this.

# The primary network interface
auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.3

5. Confirm Your Changes
Your edited interfaces file should now look something like this.
# This file describes the network interfaces available on your system
# and how to activate them. For more information, see interfaces(5).
# The loopback network interface
auto lo
iface lo inet loopback
# The primary network interface

auto eth0
iface eth0 inet static
address 192.168.1.2
netmask 255.255.255.0
network 192.168.1.0
broadcast 192.168.1.255
gateway 192.168.1.3

6. Save Your Changes and Restart Networking

If your changes are correct, you now need to save them to the interfaces file. If you used Nano, to save the new file, you press Ctrl-o to write the changes (hit enter when asked to overwrite the existing file) and the Ctrl-x to quit nano.

Once your changes are saved you need to restart the networking service on the server by issuing the following command.

/etc/init.d/networking restart

7. Test and Confirm
If everything went well, you now have a static IP assigned to the eth0 network interface.
To test and make sure your changes were applied successfully, issue this command:

ifconfig eth0

Your output should look something like this:

eth0 Link encap:Ethernet HWaddr 00:02:E3:09:C2:FB
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::202:e3ff:fe09:c2fb/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:512598 errors:0 dropped:0 overruns:0 frame:0
TX packets:5428 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:30923166 (29.4 MB) TX bytes:547585 (534.7 KB)
Interrupt:10 Base address:0x6000

Note the second line
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
now shows the correct static ip, Bcast and Mask information that we assigned.

If you gained root by using the sudo su option, remember to exit root by simply issuing the “exit” command.

Comments are closed.