In this tutorial, we are going to learn how to configure static IP Addresses on Ubuntu 18.04 server using netplan command.
To manage network interfaces, Ubuntu 18.04 uses the NetPlan utility. The NetPlan command has replaced the commonly used static interfaces configuration file, /etc/network/interfaces, with various network configuration files in the /etc/netplan/ directory. The files in this directory are in yaml format. On Ubuntu 18.04 server, networkd renderer is used to control network interfaces.
If you need to connect to WiFi in Linux using NMCLI command, then you can check our guide by following the link below;
Configure Static IP Addresses on Ubuntu 18.04 Server using Netplan
Before we proceed to configure static IP address on an interface on Ubuntu 18.04 Server, let us have a look at the format of the default netplan configuration file, /etc/netplan/01-netcfg.yaml
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: enp0s3: dhcp4: yes
From the above output, DHCP is enabled for interface enp0s3 and is thus relying on DHCP server for IP addresses.
To set static IP address on an interface, we will use the same format as in the output of the configuration file above, disable the DHCP server for our interface along with adding other details as shown below;
In the example below, we will configure enp0s8 interface with static IP address 192.168.59.81, netmask 255.255.255.0, gateway 192.168.59.1, nameserver 184.108.40.206, 220.127.116.11.
To reduce too much work, we will just copy the above configuration file and do some modification.
cp /etc/netplan/01-netcfg.yaml /etc/netplan/02-netcfg.yaml
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: enp0s8: dhcp4: no addresses: [192.168.59.81/24] gateway4: 192.168.59.1 nameservers: addresses: [18.104.22.168,22.214.171.124]
Save the configuration file and apply the changes using the command below.
Check the IP add on the enp0s8 interface to confirm the assignment.
# ip add show enp0s8 4: enp0s8: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000 link/ether 08:00:27:87:d4:14 brd ff:ff:ff:ff:ff:ff inet 192.168.59.81/24 brd 192.168.59.255 scope global enp0s8 valid_lft forever preferred_lft forever inet6 fe80::a00:27ff:fe87:d414/64 scope link valid_lft forever preferred_lft forever
As you can see, the IP address is assigned.
If you want to assign different IPs to different interfaces in the same configuration file, you can do so as follows.
# This file describes the network interfaces available on your system # For more information, see netplan(5). network: version: 2 renderer: networkd ethernets: enp0s8: dhcp4: no addresses: [192.168.59.81/24] gateway4: 192.168.59.1 nameservers: search: example.com addresses: [126.96.36.199,188.8.131.52] enp0s9: dhcp4: no addresses: [192.168.58.92/24] gateway4: 192.168.58.1 nameservers: addresses: [184.108.40.206,220.127.116.11]
Once done with configuration, remember to run the
netplan apply command to apply the changes and bring the interfaces up.
That is all about how to configure Static IP Addresses on Ubuntu 18.04 Server using netplan command. Enjoy