To configure a static IP address on your Ubuntu 18.04 server you need to modify a relevant Netplan network configuration file within /etc/netplan/ directory. Ubuntu introduced Netplan utility to configure the network on Ubuntu 18.04 LTS. It uses YAML file format to read network configuration from /etc/netplan/ directory.
Following are the steps for configuring IPs on Ubuntu 18.04
You might find there a default Netplan configuration file called 01-netcfg.yaml or 50-cloud-init.yaml (may vary from version to version).
WARNING: You must adhere to a correct code indent for each line of the block. In other words, the prefix number of spaces for each line is essential.
# cat /etc/netplan/01-netcfg.yaml
# For more information, see netplan(5).
Here we have disabled dhcp4 with no option since we do not want dynamic allocation of IP addresses.
addresses: line should contain server IP address with subnet CIDR Prefix (More about CIDR)
gateway4: The line should contain the default gateway.
nameservers: contains IP addresses of primary and secondary DNS. In the above example, we have used Google's Public DNS servers IP addresses
Please make sure you have added those lines with the correct format.
Once ready apply changes with:
# netplan apply
In case you run into some issues execute:
# netplan --debug apply
The above the command will help you to identify if there's an issue with the data you have inputted.
Congratulations! You’ve successfully configured a network static IP addresses to your Ubuntu servers.