Setting a Static IP Address and DNS Server in Ubuntu

Share on:

This article was written using a Ubuntu server which is debian based. Other distros such as RedHat and SUSE may take different steps to configure networking.

Intro

If you require a static IP address for a Linux machine, you must configure a specific config file via the command line, there are GUI tools available which allow you to achieve this too.

So let’s start by first identifying the following:

  • IP Address for the machine
  • Netmask (AKA subnet mask)
  • Default Gateway
  • Broadcast Address
  • Network Address
  • Internal DNS Servers

To find your broadcast and network address use a subnet calculator

Once you have worked out the details above, you will then be ready to configure your machine. Please note it’s best to have physical access to the machine when doing the configuration, as doing so remotely can leave you disconnected permanently if you get something wrong.

Setting a static IP Address

We will do the configuration all from the command line (Terminal).

Identify the network interface you want to assign the IP Address to, to do this execute the following command:

1ifconfig -a

You should see something similar to the image below:

ifconfig

As you can see this machine only has one physical interface, eth0 and one loopback. You will need to note down the interface name before continuing.

So let’s configure the interface, execute the following command:

1sudo nano /etc/network/interfaces

You need to insert the details as shown in the screenshot below:

interfaces

Once you have finished editing type Ctrl Xand type Yes to save the configuration file. Even though we have edited the interface details it will not be live until we restart the networking services, which you can do via a reboot or by executing the following command:

1sudo /etc/init.d/networking restart

You have now configured your linux machine with a static IP Address on interface eth0, but to confirm this has occurred execute the following command:

1ifconfig

If you output as below you were successful:

ifconfig

You can also test connectivity by pinging your local gateway address.

1ping 10.98.231.1 –c 4

Setting DNS Servers

  • DNS servers are required for name resolution.

So to start we will create a file called resolv.conf and will use google’s OpenDNS servers.

1sudo nano /etc/resolv

Write the following into the config file:

ifconfig

Once you have edited type Ctrl X and type Yes to save the configuration file. You now have DNS resolution so pinging Google.com will work and so will normal web browsing.