How to Configure Static IP Address on Ubuntu 18

How to Configure Static IP Address on Ubuntu 18

The newer versions of Ubuntu uses ‘Predictable Network Interface Names’ that, by default, start with en[letter][number].

The first step is to identify the name of the ethernet interface you want to configure. To do so use the ip link command, as shown below:

ip link

The command will print a list of all the available network interfaces. In this case, the name of the interface is ens3:

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN mode DEFAULT group default qlen 1000

link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000

link/ether 00:16:3e:48:d6:33 brd ff:ff:ff:ff:ff:ff

Netplan configuration files are stored in the /etc/netplan or /etc/network directory and have the extension .yaml. You’ll probably find one or two YAML files in this directory. The file may differ from setup to setup. Usually, the file is named either 01-netcfg.yaml, 50-cloud-init.yaml,  NN_interfaceName.yaml,or interfaces but in your system it may be different.

Open the YAML configuration file with your text editor :

sudo nano /etc/netplan/01-netcfg.yaml

network:

version: 2

renderer: networkd

ethernets:

ens3:

dhcp4: yes

or

sudo nano /etc/network/interfaces

# The loopback network interface

auto lo

iface lo inet loopback

# The primary network interface

auto eth0

iface eth0 inet static

address 5.254.113.216

netmask 255.255.255.192

gateway 5.254.113.193

Cop

Before changing the configuration, let’s explain the code in a short.

Each Netplan Yaml file starts with the network key that has at least two required elements. The first required element is the version of the network configuration format, and the second one is the device type. The device type can be ethernets, bonds, bridges, or vlans.

The configuration above also includes the renderer type. Out of the box, if you installed Ubuntu in server mode, the renderer is configured to use networkd as the back end.

Under the device’s type (in this case ethernets), you can specify one or more network interfaces. In this example, we have only one interface ens3 that is configured to obtain IP addressing from a DHCP server dhcp4: yes.

To assign a static IP address to ens3 interface, edit the file as follows:

  • Set DHCP to dhcp4: no.
  • Specify the static IP address 5.254.113.216/26. Under addresses: you can add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface.
  • Specify the gateway gateway4: 5.254.113.193
  • Under nameservers, set the IP addresses of the nameservers addresses: [8.8.8.8, 1.1.1.1]

network:

version: 2

renderer: networkd

ethernets:

ens3:

dhcp4: no

addresses:

– 5.254.113.216/26

gateway4: 5.254.113.193

nameservers:

addresses: [8.8.8.8, 1.1.1.1]

Cop

When editing Yaml files, make sure you follow the YAML code indent standards. If there are syntax errors in the configuration, the changes will not ne applied.

Once done save and close the file and apply the changes with:

sudo netplan apply

or

sudo /etc/init.d/networking restart

Verify the changes by typing:

ip addr show dev eth0

2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000

link/ether 00:16:3e:48:d6:33 brd ff:ff:ff:ff:ff:ff

inet 5.254.113.216/26 brd 5.254.113.255 scope global eth0

valid_lft forever preferred_lft forever

inet6 fe80::216:3eff:fe48:d633/64 scope link

valid_lft forever preferred_lft forever

That’s it! You have assigned a static IP to your Ubuntu server