Thursday 13 February 2014

Ubuntu Server Setting IP Address by Editing “Interfaces” File


First login to Ubuntu server. Ubuntu server stores the configuration of network interfaces in the file “/etc/networks/interfaces”. To view the content of the file use the command “cat /etc/networks/interfaces”. So let see and example file in the below picture.

Here as can be seen there are two interfaces. The one with the name “lo” is the loopback interface. The other with the name “eth0” is the real interface connected to this machine. Here it is set to “DHCP” as shown by the line “iface eth0 inet dhcp”.
Now let us check the IP obtained by the machine. For this we have to use the command “ifconfig”. The ip as shown by the command is depicted in the picture below.

Now let us edit the file “/etc/networks/interfaces” to set the IP address to static. We will set the IP address to “192.168.56.105”. Here we assume the network interface is taken as “eth0”. For this you have to add the following lines as shown in the picture below:
[Use the command “sudo vi /etc/networks/interfaces” to edit the file]
auto eth0
iface eth0 inet static
address 192.168.56.105
netmask 255.255.255.0
gateway 192.168.56.1

After this save the file. (Hope you are familiar with vi editor).  Now to have the settings take effect you need to restart the service named “networking”. For this use the command “sudo service networking restart”. After that let us check the ip again using the “ifconfig” command”". See the picture below:

Now the IP has changed. Now let us ping to “google.com” and see the result. [I have Internet access from this system. Otherwise this ping won’t work. You can ping to your domain computers if yours is internal network]

My God no ping. I had set the network interface with IP, so what is the problem. Oh! I forget to set dns entries. OK let us add the same to the file “/etc/networks/interfaces”. For this you have to add the line “dns-nameservers <ip1> <ip2>”. Normally two ips for dns that is why. You can any number of ips separated by spaces.

Now restart the “networking” service again. Let us check the content of “/etc/resolv.conf” file by using the command “cat /etc/resolv.conf”.

You can see the dns server entries are now in this file. Don’t edit this file directly as it is generated dynamically on every reboot.
Now let us again ping to ”google.com”.

Got the ping. Now everything is working fine.

No comments:

Post a Comment