facebook youtube pinterest twitter reddit whatsapp instagram

Understanding & Assigning a Static IP addresses in Ubuntu

An IP address is a unique string of numbers that are assigned to each device connected to a network using the internet protocol for communication. When you buy a Virtual Private Server (although, it doesn't have to be a VPS, it can be anything from shared hosting to a dedicated server, etc), you are given an IP address for that server.

For a moment, assume the IP address is a phone number assigned to a specific name, you see that! An IP address is a way to identify a specific computer from one another, the concept of IP address could well cover a whole book, so, let's get back to the meaty section of this guide...

Why Should You Assign Static IP Addresses?

It is highly recommended your IP addresses remain fixed and do not change, and here is why...

If an IP address changes (e.g a dynamic IP), your visitors or users will experience downtime and outage. Also, you wouldn't want IP changes if you are running some sort of online services, if the IP changes, it would take some time for the DNS caches to flush on other computers that know the previous IP address, which would, in turn, cause an outage, although, dynamic IP's have its own place, you should only use it if you know what you are doing.

If you bought a server from a cloud provider, you will automatically be assigned a static IP address, and that would have been configured to stick to the server. In the case of a physical or virtual server you manage yourself, Ubuntu will automatically grab a dynamical lease from your DHCP server, let's go over how to assign a static IP address.

Assigning a Static IP address

There are two ways of assigning a permanent address to a server. The first is by using a static IP assignment, the way this works is to use an IP address that is not being used by anything (more on this below) and configure your Ubuntu server to use that address. This way, you would no longer be dealing with your DHCP server, you are making it fixed.

Wait! I need to clarify some info before we continue. You just don't use a random IP addresses, if you would be using a static IP address for your home network, the address numbers should be chosen from the private IP address ranges defined by the Internet Protocol standard (meaning they can only be used on networks which are not connected to the global Internet):

  • 10.0.0.0 - 10.255.255.255
  • 172.16.0.0.0 - 172.31.255.255
  • 192.168.0.0 - 192.168.255.255

You can pick thousands of different IP addresses from the above ranges, here are some don't...

  • Make sure you aren't choosing an address that ends with either .0 or .255, they are reserved for use by the network protocol
  • Do not choose address at the beginning of a private range, attackers are fond of attacking the first addresses, so, just grab at random, e.g: 192.168.66.5

The other way of assigning a fixed address to a server is by using a static lease, also known as DHCP reservation. A static lease is beyond the scope of this guide, just wanted you to be aware of that, let's get back to static IP address assignment.

First, let see how we can set a static IP in older versions of the Ubuntu server (16.04 below).

Setting a Static IP in the legacy version of Ubuntu (16.04 & below)

Before you set a static IP, you need to determine what network interfaces you currently have. That interface represents the Ethernets ports on your computer, go ahead and run the following to determine the current network:

ifconfig -a

This would display all interfaces which are currently available, even if down:

eth0      Link encap:Ethernet  HWaddr b2:4c:71:49:0c:8d
          inet addr:192.168.43.137  Bcast:192.168.43.255  Mask:255.255.255.0
          inet6 addr: 2604:a880:800:c1::aa:6001/64 Scope:Global
          inet6 addr: fe80::b04c:71ff:fe49:c8d/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:15574785 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13383399 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:2155218368 (2.1 GB)  TX bytes:74623337732 (74.6 GB)

My current one is eth0, before you go off assigning a random static IP address, let me be clear:

You can't just set a random static IP address and expect it to work, your dhcp server will have a range of IP addresses that will be automatically assigned to host that request an assignment. (Note: If you are using a VPS, you will have a static ip in place, so, just ask your VPS provider for that)

To get a static IP, you need to assign an IP range outside of the dhcp server's range, for example, if the dhcp range is from 192.168.43.50 through 192.168.43.100, you'll want to use an IP outside of that range. For this tutorial. I would be using192.168.43.150

To assign a static IP, you will need to edit the /etc/network/interfaces file.

The /etc/network/interfaces contain network interface configuration information, this is where you configure how your system is connected to the network.

Note: Lines starting with `#' are ignored. You should also note that end-of-line comments are NOT supported, comments must be on a line of their own.

Here is an example from my old servers, when I run the following command

sudo nano /etc/network/interfaces

Output:

# The primary network interface
auto enth0
iface enth0 inet dhcp

This is usually the default file when you install Ubuntu server virtually or on a physical machine, it might look a little bit different on a VPS, the interface might be different.

The "auto" is used to identify the physical interfaces to be brought up when ifup is run with the -a parameter, in this case, the physical interface name (eth0).

The other line "iface enth0 inet dhcp" is allowing the interface to receive a dynamic IP assignment from a DHCP server.

If you want to manually assign a fixed IP address, you would need to make changes to the file, for example:

auto eth0
iface eth0 inet static
    address 192.168.43.150
    netmask 255.255.255.0
    gateway 192.168.43.1

I highlighted the output I changed in red...

  • I changed the dhcp to static since we are creating a static IP address.
  • I am assigning a static IP address to 192.168.43.150
  • The subnet mask to 255.255.255.0 and the...
  • gateway address to 192.168.43.1

I know the subnet mask and gateway are a little bit confusing, and they are beyond the scope of this guide, but here is how it works...

The subnet mask is used by a computer to determine if another computer is on the same network or a different network, for example, let's say Computer A and B are on the same LAN (Local Area Network), and Computer A wants to talk to Computer B, this is where the subnet mask comes into play, with the subnet mask, the computer would be able to identify which is local and which is remote. if it is a remote (outside the network ), then it would be sent to the gateway.

The gateway helps to go from your local area network to other networks outside of the Lan, it is like a gateway to other networks, think of it this way...

A private network is only accessible by the private users of the network, and a public network (e.g google, amazon, etc) is available for everyone, now, to go from the private network to the public network, you need a gateway, it is like a bridge to reach another network. The default gateway address is provided by your internet service provider, if you are using a VPS, you should see this in the console of your VPS provider, e.g DigitalOcean's console:

 IP, gateway and netmask digitalocean

I guess that clears things a bit, Now, that we have discussed the way to assign a static IP on a legacy version of Ubuntu, you need to restart networking, using:

sudo systemctl restart networking.service

let's see how it is done on a newer version...

Setting a Static IP address using NetPlan in Ubuntu (18.04 above)

The configuration files on Ubuntu 18.04 and above resides in /etc/netplan, the file is in YAML format. YAML is used for configuartion files and the syntax are beyond this guide, however, it should be fairly simple to grasp.

Open up the configuration file in /etc/netplan directory:

cd /etc/netplan

When you list the files in that directory, you should see somthing similar:

files in etc-netplan

Mine is named 50-cloud-init.yaml, it is also possible to be saved with a different name, such as 01-netcfg.yaml.

It looks like this when opened:

network:
    ethernets:
        enp0s3:
            dhcp4: true
    version: 2

If you look carefully at the above configuration, you can see the network "enp0s3" has dhcp enables, which means it would grab an IP address automatically from your dhcp server.

To address a static IP address, you would want to change the dhcp from true to false, and right underneath that, you configure the static IP, for example:

network:
   renderer: networkd
   ethernets:
     enp0s3:
       dhcp4: false
       addresses: 
         -  192.168.43.150/24
       gateway4: 192.168.43.1
       nameservers:
         addresses: [192.168.43.1, 1.1.1.1]
   version: 2

The renderer netword is a system daemon that manages network configurations. It detects and configures network devices as they appear.

I added an IP address of 192.168.66.5 and a subnet mask address of 255.255.255.0, which is the same as /24.

Next, I added a default gateway address of 192.168.66.1. and I finally added dns servers to 192.168.43.1 and 1.1.1.1 this is to resolve the names from a local DNS server and an external one (1.1.1.1 is a free DNS by Cloudflare)

To apply the changes, you run sudo netplan apply (this would catch any error you've made), make sure you are doing this from a VM console, if you are doing this over SSH, your SSH connection will drop. 

If you get error:

Inconsistent indentation yaml

Make sure they are structures as the one above.

You can check your new static ip by running ip a.

I hope you learnt one or two things about assigning a static IP addresses, would see you later ;)

Related Post(s)

  • How To Send Mail To Multiple Addresses Using (mailx)

    In this guide, you'll learn a couple of ways you can send mail to multiple addresses using mailx. mailx is a utility program for sending and receiving mail. I assume you already have mailx command, i

  • Monitoring Multiple Log Files In RealTime With MultiTail (Ubuntu)

    Oh my... I really find scanning through the logs file time consuming, and painful. Luckily for me, I founded Multitail, which is an awesome, and powerful tool for not only browsing through several f

  • How To Find Files Using Locate in Ubuntu

    It's kind of frustrating when you are searching for a particular file, and you have no idea of how to find it, In this guide, I'll walk you through on two different ways you can find a file in your G

  • Managing MariaDB Databases (Ubuntu Server)

    In this guide, you'll learn how to manage MariaDB databases in your terminal, from connecting to the database server using the mariadb command, creating a database, removing (drop) database, and mana

  • Understanding the Linux Filesystem

    Understanding the filesystem is important if you want to know how data is stored and retrieved, but really, the term might be somewhat confusing in Linux, as it means two different things. A filesys

  • Viewing Disk Usage in Ubuntu

    There are several ways to view disk usage in your Linux system, and that is what we would be going over in this guide... Out of the box, Linux provides us the df command, which is the standard Unix c