Setup static IP for your Ubuntu Server

Good day my Linux Yogi’s,

I usually would expect as an Administrator that when you install a Server OS that you would have a step during the installation process to choose weather or not to configure either DHCP or Static IP for your server. We can only hope that this option will be available in the future. Therefore today I am going to illustrate for you how to set up your Ubuntu Server with a static IP address. 

Ubuntu Server will use out of the box DHCP to assign an available IP address to the system. In a few step I will show you how to change it to a static IP address. Let’s get started.

What we need to know for that is:

  • IP Address:
  • Subnet Mask
  • Gateway Address
  • DNS Server Address(es)
  • Search Domain

I am going to use IP address 192.168.69.95, subnet mask of 255.255.255.0, gateway address of 192.168.69.1, DNS Server of 192.168.69.2 and search domain of “linuxmeditation.lan“.

The first thing I always do is to elevate the console to have root rights. Enter the following command:

sudo -s

enter your password and you have successfully elevated your console session and from now on you don’t have to use sudo in front of each command as long as you stay in this console session.

Let’s open up the file /etc/network/interfaces with your preferred editor. I use nano.

nano /etc/network/interfaces

you should see something similar like the following:

Default settings for interfaces
Default settings for interfaces

Now look under the section “# The primary network interface” in particular the last line. It shows dhcp which means its waiting to get an ip address assigned by the dhcp server. I will show you now how I always change it for my systems and then I will explain it.

auto enp0s3
iface enp0s3 inet static
address 192.168.69.95
netmask 255.255.255.0
gateway 192.168.69.1
network 192.168.69.0
dns-nameservers 192.168.69.2 8.8.8.8
dns-search linuxmeditation.lan

The second line “iface enp0s3 inet static” means that the interface enp0s3 will get a static IP address assigned.

The third line “address 192.168.69.95” means that we assign the address 192.168.69.95 to this interface.

the fourth line “netmask 255.255.255.0” means that the IP address is in a full Class C network which means it can address directly IP addresses from  192.168.69.1 to 192.168.69.254.

The fifth line “gateway 192.168.69.1” means that it has to go here for addresses beyond this Class C network.

The sixth line determines the network 192.168.69.0

The seventh line “dns-nameservers 192.168.69.2 8.8.8.8” means that it should use first dns server 192.168.69.2 to resolve names or IP addresses and if this one is not available it should use the secondary of 8.8.8.8. You can add up to three dns server IP addresses separated by a simple space.

The eighth and last line “dns-search linuxmeditation.lan” means that it should try using/adding the domain suffix of “linuxmeditation.lan” if it is not given.

Now go ahead and save the file. Reboot the server and it should be responding on the newly set up IP address.
I also recommend that while you at it to edit the file /etc/hosts. It should look like the following:

127.0.0.1 localhost
127.0.1.1 ubuntuserver

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

The second line I always change to prevent for errors for example Apache is throwing an error message at you when you restart it that it was unable to determine the local servers full qualified domain name etc. Make it look like this and you avoid this issue all together as well as other issues:

127.0.0.1 localhost
127.0.1.1 ubuntuserver.linuxmeditation.lan ubuntuserver

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

In the case you like this article or think it was useful to you I would appreciate if you register and share this article. Please leave me a note of criticism or suggestion for future articles.

Until next time, Namaste my friends! 😀