How to configure a network interface on Ubuntu 18.04 using netplan

How to configure a network interface on Ubuntu 18.04 using netplan.

First you need to identify the interface you are going to configure. You can use ifconfig command to list the available interfaces.

$ ifconfig -a

We are going to reconfigure enp0s3.

How to set a static IP address.

Generate a YAML configuration file, if there is no file under /etc/netplan/

$ sudo netplan generate

A configuration file will be generated under /etc/netplan/ (file name may change)

$ sudo nano /etc/netplan/50-cloud-init.yaml
network:
    version: 2
    ethernets:
        enp0s3:				
            addresses: [10.3.2.207/24]
            gateway4: 10.3.2.1
            nameservers:
                addresses: [8.8.8.8,8.8.4.4]
            dhcp4: no   
            dhcp6: yes

Save and exit the file. Then apply the changes using following command.

$ sudo netplan apply

Now verify the interface with the command ifconfig

$ ifconfig -a

You can see the interface with the new IP

How to set a dynamic IP address.

Use the following configuration to receive an IP address from a DHCP server.

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

Save and exit the file.

Apply the changes and verify the interface.

$ sudo netplan apply
$ ifconfig -a