How to use the netplan command on Ubuntu

In Ubuntu 17.10, a new command-line network configuration tool was introduced known as “Netplan”. Netplan makes it simple to configure and manage network settings in your Ubuntu system. It also permits you the YAML abstraction for configuring a network interface. It interacts with the kernel through the systemd-networkd and NetworkManager daemons which are also known as renderers. You have to choose any one from both of them. Netplan reads the network configurations from “/etc/netplan/*.yaml” files, which can save the configuration settings for all network interfaces.

If you use the netplan command on Ubuntu, then It becomes easy to restart and change network settings. Netplan is also good for automation (humans and programs can read YAML). This write-up is focusing on how to use the netplan command on Ubuntu. Let’s begin:

How to list out all active network interfaces on Ubuntu

Any Linux system administrator should be familiar with the “ip” command, a powerful tool for configuring network interfaces. It can be used to assign and delete addresses and routes, put up or down interfaces, control ARP cache, and more. To get a list of active network interfaces on your system, utilize the “a” option in the ip command:

$ ip a

How to use the netplan command on Ubuntu 1

How to use netplan command for setting dynamic IP address on Ubuntu

This section will show you how to set a dynamic address on Ubuntu using the netplan command. But firstly, if the required YAML file does not exist in your system, then generate the renderer configuration file by writing out the below-given command in your terminal:

$ sudo netplan generate

How to use the netplan command on Ubuntu 2

Now, verify its presence on your system by listing out the “/etc/netplan/” files:

$ ls /etc/netplan/

How to use the netplan command on Ubuntu 3

Check the content of the network renderer configuration file:

$ cat /etc/netplan/*.yaml

How to use the netplan command on Ubuntu 4

Open up the “/etc/netplan/*.yaml” file in your nano editor:

$ sudo nano /etc/netplan/*.yaml

How to use the netplan command on Ubuntu 5

Now write out the below-given code in the /etc/netplan/*.yaml” file. We are going to set a dynamic IP address for our “enp0s3” network interface:

network:
 version: 2
 renderer: networkd
 ethernets:
   enp0s3:
     dhcp4: yes
     dhcp6: yes

In the above-given command:

  • dhcp4and dhcp6 are IPv4 and IPv6 DHCP properties.
  • enp0s3is the network interface name.

How to use the netplan command on Ubuntu 6

Press “CTRL+O” to save the changes in this file:

How to use the netplan command on Ubuntu 7

To apply modified network settings, utilize the below-given command in your terminal:

$ sudo netplan apply

How to use the netplan command on Ubuntu 8

Write out this command to confirm the IP address:

$ ifconfig -a

How to use the netplan command on Ubuntu 9

How to use netplan command for setting static IP address on Ubuntu

First of all, open up the “/etc/netplan/*.yaml” file in your nano editor:

$ sudo nano /etc/netplan/*.yaml

How to use the netplan command on Ubuntu 10

Then, write out the following code in the “/etc/netplan/*.yaml” file for setting a static IP address on your system:

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: no
      dhcp6: no
      addresses: [10.0.2.15/24]
      gateway4:  10.0.2.255
      nameservers:
              addresses: [8.8.8.8, 8.8.4.4]

In the above-given command:

  • dhcp4and dhcp6 are IPv4 and IPv6 DHCP properties.
  • enp0s3 is the network interface name.
  • addresses contain the sequence of static addresses to the network interface.
  • gateway4contains default gateway’s IPv4 address.
  • nameserverscomprise a sequence of IP addresses for nameservers.

How to use the netplan command on Ubuntu 11

Press “CTRL+O” to save the changes we made in the file:

How to use the netplan command on Ubuntu 12

After that, apply the modified network settings by utilizing the below-given command:

$ sudo netplan apply

How to use the netplan command on Ubuntu 13

Now, check out if the configured “enp0s3” is connected to the local network and have a static IP address which we assigned earlier:

$ ifconfig -a

How to use the netplan command on Ubuntu 14

How to get more information about netplan on Ubuntu

To know more about the configuration options and information related to the netplan command, you can check out its man page:

$ man netplan

How to use the netplan command on Ubuntu 15

How to use the netplan command on Ubuntu 16

Follow this link to check out more configuration examples.

Conclusion

Netplan was introduced in Ubuntu 17.10 and is active by default in the latest releases, including Ubuntu 20.04. The purpose of this tool is to use YAML configuration files to abstract network interface configurations. Those configurations are then translated into networkd setups and NetworkManager for the given renderer. I hope the usage of netplan command for setting the static and dynamic addresses on Ubuntu will be clear for you after reading this article. To know more about trendy technology topics, keep following Linux Hint.

Leave a Comment

Your email address will not be published. Required fields are marked *