Change IP Address in Ubuntu Server 12.04 LTS

In Linux everything is a file. For almost everything including the devices that are connected to the system, you will be able to easily access the file related to it. To edit IP Address you have to open networking file using a text editor. In this example I will be using vi to change IP Address information.

Configure your Server to use DHCP on the network

Enter Networking services file with the following command.


sudo vi /etc/network/interfaces


To insert text into the text file. Press the letter "I" on your keyboard and insert the following two lines. Note that, you will already have a line with "auto lo". Don't overwrite these two lines, it's called the loopback network interface. By default you will have eth0 for network interface. If you have multiple network interfaces it will start from zero, like eth0, eth1.


auto eth0
iface eth0 inet dhcp


After finishing, press Esc key, type ":wq" and press Enter. "w" means "write, "q" means quit. By combining wq, it writes the changes to the file and quits the vi editor. If you want to exit vi editor without saving changes, just include the letter "q" only.


Now restart Networking Service with the following command


sudo /etc/init.d/networking restart




Manual IP Addresses Configuration

Configuring DHCP is easy isn't it? Now let us manually assign IP Address to our server. Usually servers have manually assigned IP Addresses.

Now, enter Networking Services file with the following command.


sudo vi /etc/network/interfaces


Now enter the following lines and save the file.

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

interfaces file

The indentation beginning from line 3 is not required, it's just for extra clarity. If you want you can write all with the same indentation.
The indentation beginning from line 3 is not required, it's just for extra clarity. If you want you can write all with the same indentation.

Tip: Look at the difference between manual IP configuration and DHCP. There are two differences.

  • It is determined whether it's static or dynamic at the end of second line.

  • Static has these extra lines, address, netmask, network, broadcast and gateway. Just memorize these differences.


Edit the DNS settings file

Type the following command to edit the DNS file for manual entries. If you want to switch back to DHCP, make sure the file contents are cleared, otherwise network connection would be using the manual DNS entries.


sudo vi /etc/resolvconf/resolv.conf.d/base


This is just a blank file. Now enter your DNS ip addresses like below. You can have a single or two DNS addresses. Two addresses are always recommended.


nameserver 8.8.8.8
nameserver 8.8.8.4


Reboot Computer

Type the following command to reboot the computer.


sudo reboot


Now test if you can ping to www.google.com with the following command, to make sure everything is alright. Or try pinging to your router or another pc. To stop the ping command press Ctrl+C.


ping www.google.com



Now type the following command to make sure the system is using the DNS you have set previously.


sudo vi /etc/resolv.conf

resolv.conf file

resolv.conf file contains dns entries which are automatically updated by the system. This file will show the actual DNS IP addresses you are using now. You can change DNS IP addresses from here also, but it would change every time when the system is restarted. resolv.conf loads data from two places. One is the IP addresses provided by DHCP and the other is the IP addresses written in resolv.conf.d/base file.

To quit the file without writing to it, press Esc, type ":q" then Enter.

Hope this helped you :-)

More by this Author


Comments

No comments yet.

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working