Static IP in Ubuntu
How To

Set static IP in Ubuntu using Terminal

Pratham Patel
Pratham Patel

Table of Contents

Normally, the router's DHCP server handles assigning the IP address to every device on the network, including your computer.

The DHCP server may also give you a new IP address occasionally. This could cause a problem if you have a home lab or server setup that works on a fixed IP address.

You need to set a static IP address on your Ubuntu system to avoid problems.

Step 1: Identify the correct network interface

The first step is always to know the name of your network interface.

"But why?" you might ask. That is because since Ubuntu 20.04, the network interfaces are named using predictable network interface names. This means your one and only ethernet interface will not be named 'eth0'.

Ubuntu Server and Ubuntu Desktop use different renderers for 'netplan', they are 'systemd-networkd' and 'NetworkManager', respectively. So let's go over their differences.

Ubuntu Server

To see available network interfaces on Ubuntu Server, run the following command:

ip link

Doing so will show a similar result:

$ ip link
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
2: enp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
    link/ether 52:54:00:43:48:1f brd ff:ff:ff:ff:ff:ff

The output enumerates network interfaces with numbers.

From this, I can see that the ethernet interface is 'enp1s0'.

Ubuntu Desktop

The advantage (at least in my opinion) of having Ubuntu Desktop is having NetworkManager as the renderer for netplan.

It has a pretty CLI output :)

Run the following command to view the available network interfaces:

nmcli device status

That will give you the device name, type, state and connection status.

Here is what it looks like on my computer:

$ nmcli device status
DEVICE  TYPE      STATE      CONNECTION
enp1s0  ethernet  connected  Wired connection 1
lo      loopback  unmanaged  --

This is more readable at first glance. I can make out that my ethernet interface is named 'enp1s0'.

Cloud IDE · Online Code Editor · Codeanywhere
Save time by deploying a development environment in seconds. Collaborate, code, learn, build, and run your projects directly from your browser.

Step 2: See current IP address

Now that you know which interface needs to be addressed, let us edit a file.

Before I change my IP address/set a static one, let us first see what my current IP address is.

$ hostname -I
192.168.122.69

Nice! But let's change it to '192.168.122.128' for demonstration purposes.

Step 3: See the gateway

A gateway is a device that connects different networks (basically what your all-in-one router is). To know the address of your gateway, run the following command:

ip route

The gateway address will be on the line that begins with "default via".

Below is the output of running the ip command on my computer:

$ ip route
default via 192.168.122.1 dev enp1s0 proto static
192.168.122.0/24 dev enp1s0 proto kernel scope link src 192.168.122.69

On the line that starts with "default via", I can see that my gateway address '192.168.122.1'

Make a note of your gateway address.

Step 4: Set static IP address

Now that you have detail like interface name and gateway address, it is time to edit a config file.

Step 4-A: Disable cloud-init if present

The easiest way to know if cloud-init is present or not is to check if there is a package with that name.

Run the following command to check:

apt-cache pkgnames | grep cloud-init

If you get an outupt, you have 'cloud-init' installed.

Now, to disable could-init, create a new file inside the /etc/cloud/cloud.cfg.d directory. The name does not matter, so I will call it '99-disable-cloud-init.cfg'.

sudoedit /etc/cloud/cloud.cfg.d/99-disable-cloud-init.cfg

Add the following line to it:

network: {config: disabled}

Please reboot your Ubuntu system now so that cloud-init does not interfere when we set our static IP address in the next step. :)

Back to Step 4

Once the 'cloud-init' related configuration is complete, we must now edit the netplan configuration to add our static IP address.

Go to the /etc/netplan directory. It is better if there is one file (easier to know which one to edit), but in some cases, there might also be more than one file with the extension '.yml' or '.yaml'.

When in doubt, grep for the name of your network interface. Use the following command if you are not comfortable with grep:

grep -H INTERFACE_NAME *.*

Since the name of network interface for my ethernet is 'enp1s0', I will run the following command:

$ grep -iH enp1s0 *.*
00-installer-config.yaml:    enp1s0:

running this command shows that the file I am looking for is '00-installer-config.yaml'. So let us take a look at it.

network:
  ethernets:
    enp1s0:
      dhcp4: true
  version: 2

You might have noticed a line that says 'ethernet' and our network interface name under that. Under this is where we configure our 'enp1s0' network interface.

Since we do not want DHCP assigned IP address, let us change that field from true to no.

Add a field called addresses. Write the IP address you wish to assign your computer along with the network prefix. So I will write 192.168.122.128/24 in the addresses field.

Finally, we also need to specify DNS nameservers. For that, create a new field called nameservers and under that, create a field called addresses which contains the IP address for your DNS servers. I used Cloudflare's DNS servers but you can use whatever you want.

This is what my '00-installer-config.yaml' file looks like after editing it to my liking.

network:
  ethernets:
    enp1s0:
      dhcp4: no
      addresses:
        - 192.168.122.128/24
      gateway4: 192.168.122.1
      nameservers:
        addresses: [1.1.1.1, 1.0.0.1]
  version: 2

To apply the settings, run the following command:

sudo netplan apply

This will take only a few seconds, and the IP address will be updated once it is done.

You can check the IP address using the hostname -I command.

$ hostname -I
192.168.122.128

Perfect! The IP address has now changed successfully.

UptimeRobot: Free Website Monitoring Service
Start monitoring in 30 seconds. Use advanced SSL, keyword and cron monitoring. Get notified by email, SMS, Slack and more. Get 50 monitors for FREE!

I know that it feels complicated but this is the proper procedure when you are trying to assign static IP via the command line in Ubuntu.

Let me know if you are stuck at some point or encounter any technical issues.