Set static IP in Ubuntu using Terminal
Everything you need to know about setting static IP on an Ubuntu machine using the command line.
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:
Doing so will show a similar result:
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:
That will give you the device name, type, state and connection status.
Here is what it looks like on my computer:
This is more readable at first glance. I can make out that my ethernet interface is named 'enp1s0'.

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.
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:
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:
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:
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'.
Add the following line to it:
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:
Since the name of network interface for my ethernet is 'enp1s0', I will run the following command:
running this command shows that the file I am looking for is '00-installer-config.yaml'. So let us take a look at it.
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.
To apply the settings, run the following command:
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.
Perfect! The IP address has now changed successfully.

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.

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)


routes:
-to: default
via: 10.10.10.1
There is an example file /usr/share/doc/netplan/examples/static.yaml
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
- 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).
- 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.
- 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.
network:
ethernets:
enp6s0:
dhcp4: no
addresses:
- 192.168.0.250/24
nameservers:
addresses: [1.1.1.1, 1.0.0.2]