Configure Static IP Address on Ubuntu 20.
04 (Server CLI and
Desktop)
In your IT environment, sometimes you may be compelled to configure
a static IP instead of relying on the DHCP protocol. A perfect example is
when you are setting up an Ubuntu server to act as a file or a web server
for your organization. A static IP, as the name suggests, ensures that the
IP address of your system remains unchanged. With DHCP, the IP
address changes once the lease time for the IP address expires and this is
undesirable for servers.
In this guide, we will explore two ways of manually assigning a static IP
on Ubuntu 20.04. We will demonstrate how you can configure a static IP
on an instance of Ubuntu server and Ubuntu desktop.
Assign a static IP on Ubuntu server 20.04
From Ubuntu 17.10 and later versions, networking is controlled by
the Netplan feature. The configuration files for Netplan are located in
the /etc/netplan directory and are written in YAML. Inside this
directory, you will find YAML configuration files labeled either 50-
cloud-init.yaml, or 00-installer-config.yaml.
However, if you are running a cloud instance of Ubuntu, chances are
that it is managed by cloud-init which auto-assigns it an IP address
by leveraging the DHCP protocol. Before we proceed further, you need
to disable cloud-init. To achieve this, open the subiquity-
disable-cloudinit-networking.cfg cloud-init configuration
file in the /etc/cloud/cloud.cfg.d/ directory
$ sudo vim /etc/cloud/cloud.cfg.d/subiquity-disable-
cloudinit-networking.cfg
Set the 'network' directive to 'disabled'.
$ sudo vim /etc/netplan/00-installer-config.yaml
From the configuration file, we can see the 'network' directive that has
2 elements. The first one is the 'ethernets' which specifies the network
interface and the second one is the version of the renderer which is
'systemd-networkd' for non-GUI instances and NetworkManager for
Ubuntu desktop (With GUI)
# This is the network config written by 'subbiquity'
network:
ethernets:
enp0s3:
dhcp4: true
version:2
We are going to set the 'dhcp4' value to 'no' to disable the DHCP
protocol and specify the interface's Static IP as follows.
To assign a static IP address to ens3 interface, modify the file as follows:
Specify the static IP address of the server. in the addresses: section,
specify an IPv4 address to be assigned to the network interface.
Next, Specify the gateway.
Under nameservers, specify the DNS or IP addresses of the
nameservers. Here, we have specified Google's DNS which is
8.8.8.8 and the Router's IP.
network:
ethernets:
enp0s3:
dhcp4: no
addresses: [192.168.1.17/22]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8,
192.168.1.1]
Save the YAML file and exit. To apply the changes made, run the
command:
$ sudo netplan apply
You can use the ifconfig or ip command to verify that your network
interface is set to use the static IP configured moments ago.
Additionally, you can use the IP route show command to display the
new routes on your system.
$ ip route show