How to configure Network IPv4 DNS or DHCP using terminal on Ubuntu server

How to configure Network IPv4 DNS or DHCP using terminal on Ubuntu server

In this post we will show you how to configure your network IPv4, DNS or DHCP using terminal in Ubuntu Linux. Nowadays almost every network gateway uses DHCP based IP address for establishing network connection. The default setting of Ubuntu network interface is for DHCP only. So your network card is automatically detected and configured for automatic DHCP during Ubuntu desktop installation and you rarely need to change it. But sometimes specially on Ubuntu server you will need to manually configure the IP address, DNS or other network options. We will show you how to do this.

Lets first learn how to check the current network interface settings, you can use the ifconfig command as shown below.

Ubunti ifconfig command


How to configure DHCP address for a specific network card

In order to configure DHCP address you will need to edit the 'interfaces' file /etc/network/interfaces .

sudo vi /etc/network/interfaces

Use vi editor to edit this file, vi editor is available by default on the Ubuntu server. You can use gedit on Ubuntu desktop edition, gedit is a GNOME based editor. Inside the file delete or comment (for commenting add hash '#' just before each line) everything. You are advised to use # to comment original setting so that you can revert to old setting if necessary. Now enter the following lines and save the file. And restart the network service.

# The primary network interface use DHCP to find our address
           auto eth0
           iface eth0 inet dhcp

sudo /etc/init.d/networking restart
           

Note: Replace eth0 with your own network interface card. Also learn vi commands to edit/save files - here.

How to configure Static IP address for your network card

In order set static IP address you need to edit the /etc/network/interfaces.

sudo vi /etc/network/interfaces

Replace everything with the following lines. Also change eth0 with your own network interface card. Here 192.168.1.7 is the static IP address that we want to set.

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

After you are done with editing, save the file then you will have to restart the networking services in order to apply the new network settings. Use the following command to do so.

sudo /etc/init.d/networking restart

How to configure the DNS

DNS configuration of the Ubuntu is similar to that of other linux flavors or distributions. For static lookup you will need to add hostname and corresponding IP address to the file /etc/hosts as explained in next section.

In order to use a particular domain name server for name lookup you simply need to add its IP address to the file /etc/resolv.conf. For example if we want to use a domain name server, with IP address 192.168.1.10, for domain name lookup we will have to edit resolv.conf file. Open the file with the following command.

sudo vi /etc/resolv.conf

And then add the following lines.

search test.com
           nameserver 192.168.1.10

How to set or change hostname in Ubuntu

Hostname can be very easily manipulated in Ubuntu. You can directly query, or set the hostname with the hostname command.

To see the current hostname use the following command.

sudo /bin/hostname

To change the hostname use the command given below, change the newhostname with your own hostname.

sudo /bin/hostname newhostname

When your system reboots it will automatically read the hostname from the file /etc/hostname.

How to setup second IP address or virtual IP address in Ubuntu

Sometimes you may need to setup a second IP address for your computer, specially if you are a server administrator. To do this you need to edit the /etc/network/interfaces file.

sudo vi /etc/network/interfaces

Add the following lines in the file. Change according to your IP address settings.

auto eth0:1
           iface eth0:1 inet static
           address 192.168.1.60
           netmask 255.255.255.0
           network x.x.x.x
           broadcast x.x.x.x
           gateway x.x.x.x

You need to enter correctly all the network details such as address, netmask, network, broadcast and gateway values. Once you have completed this, save this file and then restart networking services using the following command so that new settings are applied.

sudo /etc/init.d/networking restart