Skip to main content
How To

Set static IP in Ubuntu using Terminal

Everything you need to know about setting static IP on an Ubuntu machine using the command line.

Pratham Patel

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.

Pratham Patel
13
Avatar Placeholder
Alan
at step 4 terminal says . is not a directory, I don't know how to proceed
1
Hide Replies 2
Avatar
Abhishek
Which command exactly?
Hide Replies 1
Avatar Placeholder
Alan
grep -iH
It might have to do with the fact i’m running 22.04 lts
Avatar Placeholder
Guest
At last a good explanation. It worked
Avatar Placeholder
Guest
I tried to follow the instructions closely, but at the end, the output of hostname -I was unchanged from the beginning. does netplan generate a log message someplace when it runs.
syslog seems to indicate that it did something with a different interface:
NetworkManager[931235]: [1733711176.9638] device (xenbr0): Activation: starting connection 'netplan-xenbr0' (ef8b35e7-a9c7-319f-9650-8978e1e7f40e)
Avatar Placeholder
Lefteris
Unix at its worst! This is really huge complication to set up for such a simple thing to switch to static from dynamic. Ubuntu is following a really bad UNIX path. It is getting more complicated than Windows even and beats the original definition and purpose of Unix systems.
Avatar Placeholder
Nigel
gatway4 has been depreciated. from now we use
routes:
-to: default
via: 10.10.10.1

There is an example file /usr/share/doc/netplan/examples/static.yaml
11
Hide Replies 1
Avatar
Abhishek
Thanks for the info
1
Avatar
Post169
Some questions about the YAML file:
First to come up: Why add the "/24" to the end of the desired IP address?
Second: Why manually set the DNS servers?
Most important: I wasn't able to save the YAML file, but received warning "Error in network definition: unknown key 'addresses' "
My YAML file looked like:
network:
ethernets:
enp6s0:
dhcp4: no
enx012:
dhcp4: no
version: 2
addresses: <- Point of trouble
- 192.168.0.250/24
nameservers:
addresses: [1.1.1.2, 1.0.0.2] <- I'd expect trouble here if it got any further
Hide Replies 2
Avatar Placeholder
tirupathi
/24 is the class c network range netplant assign the total dns manual
Avatar
Pratham Patel
  1. The "/24" is the CIDR notation for the "255.255.255.0" subnet mask. You can learn more about it here (https://www.ipaddressguide.com/cidr).
  2. When your computer gets an IP from a DHCP server, a DNS server is specified by the DHCP server to the client (your computer). But since we are not using DHCP and manually configuring our IP address, we need to specify which DNS server to use.
  3. For this I assume you want to specify a static IP address for the interface "enp6s0". The part which you highlighted as "Point of trouble" must go under the "enp6s0" interface. So "addresses" will be either above or below the "dhcp4: no" field but in the same indentation depth. Also, the "nameservers" field is part of an interface, so it must go with the "dhcp4" field too, per each statically assigned interface.
With assumption made in the 3rd answer, your file should look like following:
network:
ethernets:
enp6s0:
dhcp4: no
addresses:
- 192.168.0.250/24
nameservers:
addresses: [1.1.1.1, 1.0.0.2]
Avatar Placeholder
Nick
“nmcli device status” not “devices”
Hide Replies 1
Avatar
Abhishek
Thanks for spotting that. Fixed the typo.
1