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
Step 1
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
OUTPUT
# For more information, see netplan(5).
network:
renderer: networkd
ethernets:
enp2s0:
dhcp4: no
addresses: [192.168.XX.XXX/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8,8.8.4.4]
version: 2
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.
Step 2
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.
Example output
Congratulations! You’ve successfully configured a network static IP addresses to your Ubuntu servers.