How to configure static and DHCP IP address in Ubuntu [18.04 / 20.04]

The method of configuring static IP address in Ubuntu Distribution has changed from Ubuntu 16.04 to Ubuntu 18.04, the following steps illustrate the configuration of IP address in Ubuntu 18.04 as well as Ubuntu 20.04:

1- configure 01-netcfg.yaml file:

as a root user , access to /etc/netplan directory and configure netcfg yaml (01-netcfg.yaml) file:

$ sudo cd /etc/netplan/

$ sudo nano 01-netcfg.yaml

  • Static IP configuration in 01-netcfg.yaml:
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 172.29.40.199/24
      gateway4: 172.29.40.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]
  • If you want to configure a DHCP address:
network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes

2- apply the config:

you should type :

$ sudo netplan apply

in order to apply the config of the previosuly modified file (01-netcfg.yaml).

3- check the address of your interface:

through ifconfig command , you can verify your config if applied correctly:

$ sudo netplan apply

ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    link/ether 56:00:00:60:20:0a brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 brd 192.168.1.255 scope global dynamic ens3
       valid_lft 3575sec preferred_lft 3575sec
    inet6 fe80::5054:ff:feb0:f500/64 scope link 
       valid_lft forever preferred_lft forever

That’s it, have a good day !