How to assign static IP address on Ubuntu 21.04

Step-1 : Identify the ethernet interface name

ip link –> From the below example Interface name is enp0s3
Now we will assign the IP to this interface

sandeepsr@master:~$ ip link
 1: lo:  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: enp0s3:  mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000
     link/ether 08:00:27:49:8e:45 brd ff:ff:ff:ff:ff:ff

Step-2 : Choose the IP

For this example I will use the IP already assigned by DHCP which is 192.168.0.107

sandeepsr@master:~$ ip addr show
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
     inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
 2: enp0s3:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     link/ether 08:00:27:49:8e:45 brd ff:ff:ff:ff:ff:ff
     inet 192.168.0.107/24 brd 192.168.0.255 scope global dynamic enp0s3
        valid_lft 5974sec preferred_lft 5974sec
     inet6 fe80::a00:27ff:fe49:8e45/64 scope link
        valid_lft forever preferred_lft forever

Step-3 : Assign the IP by editing the config YAML

Since am using Ubuntu 21.04 the config YAML stored in /etc/netplan/ folder
and the file name is 00-installer-config.yaml

Since this server already assigned with DHCP the file looks as below

sandeepsr@master:~$ cat /etc/netplan/00-installer-config.yaml
 This is the network config written by 'subiquity'
 network:
   ethernets:
     enp0s3:
       dhcp4: true
   version: 2

So we need to add IP address , gateway & DNS to the above file

Edit the file as below. please make sure follow the correct indentation

sandeepsr@master:~$sudo vim /etc/netplan/00-installer-config.yaml
This is the network config written by 'subiquity'
 network:
   ethernets:
     enp0s3:
       dhcp4: no
       addresses:
         - 192.168.0.107/24
       gateway4: 192.168.0.1
       nameservers:
         addresses: [8.8.8.8, 4.4.4.4]
   version: 2

And apply the changes by running below command

sandeepsr@master:~$ sudo netplan apply

Static IP was set and you can see the same below

sandeepsr@master:~$ ip addr show
 1: lo:  mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
     inet 127.0.0.1/8 scope host lo
        valid_lft forever preferred_lft forever
     inet6 ::1/128 scope host
        valid_lft forever preferred_lft forever
 2: enp0s3:  mtu 1500 qdisc fq_codel state UP group default qlen 1000
     link/ether 08:00:27:49:8e:45 brd ff:ff:ff:ff:ff:ff
     inet 192.168.0.107/24 brd 192.168.0.255 scope global enp0s3
        valid_lft forever preferred_lft forever
     inet6 fe80::a00:27ff:fe49:8e45/64 scope link
        valid_lft forever preferred_lft forever

Leave a Comment

Your email address will not be published.

Scroll to Top