How to solve error in network definition expected mapping error / set a static IP adress on Ubuntu 18.04 LTS Desktop and Server via netplan

You have to edit the netplan’s yaml file to configure static IP address on the system. Give your attention please. It’s required to use all chars as spaces in this yaml file. You have to copy and paste below lines to your file without any modification. Please give your attention to this point. Otherwise the system will throw an error when you try to apply netplan settings. Error could be look like “Error in network definition //etc/netplan/x.yaml line y column z: expected mapping

Now cd into /etc/netplan/ directory and in this directory for reference, backup the original .yaml extensioned file and edit the .yaml extensioned file with sudo privileges. For example

sudo nano /etc/netplan/01-netcfg.yaml

And replace content with below lines.

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
version: 2
renderer: networkd
ethernets:
ens18:
dhcp4: no
addresses: [A.A.A.A/24]
gateway4: G.G.G.G
nameservers:
addresses: [N.N.N.N]

Change A.A.A.A with your static IP Address and “/24” at this line defines your subnet mask. So that you do not need to enter subnet mask manually in another line.

Change G.G.G.G with your gateway’s IP address.

Change N.N.N.N with your preferred DNS’ IP address.

AND AGAIN DO NOT FORGET TO CHECK .yaml file for any more whitespaces or any other wrong or missmatching characters.

If modification done and all checks are as required than you can apply settings to your netplan with below command

sudo netplan apply

if this command finishes succesfully that’s all for setting a static IP address on an Ubuntu 18.04 LTS Server or Desktop System via netplan.

If any error encounters then you can check with this command for detailed error information.

sudo netplan --debug apply