How to add a Static IP address in Ubuntu 20.04 server

Share this article

As a system administrators, we have seen many instances where we need to set up an IP address. In this article, we are going to see how to configure static IP addresses in Ubuntu 20.04. We will discuss two strategies here one with CLI and the other with Netplan

IP configuration to assign

We want to assign the following IP address to the network device enp0s31f6 (See the find network interface section)

  • IP address: 192.168.1.2
  • Subnet Mask : 255.255.255.0
  • Default gateway192.168.1.1

Find network interface

In order to set up a static IP address. The first step is to identify the name of the ethernet interface you want to configure. To check out the interface list below-mentioned command lists all the interfaces in the system.

ip a

Below code section is the output of the command.

root@ubuntu:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s31f6: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state DOWN group default qlen 1000
    link/ether 00:2b:67:2c:72:48 brd ff:ff:ff:ff:ff:ff
4: wlp0s20f3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 74:d8:3e:b1:4d:1f brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.189/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp0s20f3
       valid_lft 604072sec preferred_lft 604072sec
    inet6 2a01:c22:b02f:d600:732c:f49c:37ed:87ad/64 scope global temporary dynamic 
       valid_lft 186848sec preferred_lft 85438sec
    inet6 2a01:c22:b02f:d600:751:afc8:d3e1:dc1e/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 186848sec preferred_lft 100448sec
    inet6 fe80::cf94:fcc9:6b6d:eee7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
5: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:3d:22:8f brd ff:ff:ff:ff:ff:ff
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:3d:22:8f brd ff:ff:ff:ff:ff:ff
7: wwan0: <BROADCAST,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 12:e0:d0:a0:10:60 brd ff:ff:ff:ff:ff:ff
root@ubuntu:~# 

In this article we will configure enp0s31f6 network interface.

Configuring Static IP through CLI

To set the above IP address we will use the IP command. See the below-mentioned command to set the IP address and route.

sudo ip addr add 192.168.1.2/24 dev enp0s31f6
sudo ip route add default via 192.168.1.1 dev enp0s31f6

Verify

The output of the above command is shown in the below code section.

root@ubuntu:~# ip a
1: lo: <LOOPBACK,UP,LOWER_UP> 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: enp0s31f6: <BROADCAST,MULTICAST,UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
    link/ether 00:2b:67:2c:72:48 brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.2/24 scope global  dynamic enp0s31f6
       valid_lft forever preferred_lft forever
4: wlp0s20f3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000
    link/ether 74:d8:3e:b1:4d:1f brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.189/24 brd 192.168.1.255 scope global dynamic noprefixroute wlp0s20f3
       valid_lft 603156sec preferred_lft 603156sec
    inet6 2a01:c22:b02f:d600:732c:f49c:37ed:87ad/64 scope global temporary dynamic 
       valid_lft 185930sec preferred_lft 84521sec
    inet6 2a01:c22:b02f:d600:751:afc8:d3e1:dc1e/64 scope global dynamic mngtmpaddr noprefixroute 
       valid_lft 185930sec preferred_lft 99530sec
    inet6 fe80::cf94:fcc9:6b6d:eee7/64 scope link noprefixroute 
       valid_lft forever preferred_lft forever
5: virbr0: <NO-CARRIER,BROADCAST,MULTICAST,UP> mtu 1500 qdisc noqueue state DOWN group default qlen 1000
    link/ether 52:54:00:3d:22:8f brd ff:ff:ff:ff:ff:ff
6: virbr0-nic: <BROADCAST,MULTICAST> mtu 1500 qdisc fq_codel master virbr0 state DOWN group default qlen 1000
    link/ether 52:54:00:3d:22:8f brd ff:ff:ff:ff:ff:ff
7: wwan0: <BROADCAST,MULTICAST,NOARP> mtu 1500 qdisc noop state DOWN group default qlen 1000
    link/ether 12:e0:d0:a0:10:60 brd ff:ff:ff:ff:ff:ff
root@ubuntu:~# 

Configuring Static IP through Netplan

In the earlier configuration, we set the IP address through CLI which is not persistent. The right way of doing configuration is doing by
Netplan.

Netplan related Configuration files can be found in the /etc/netplan directory. /etc/netpan directory has multiple YAML files. In ubuntu 20.04 server it named as 01-network-manager-all.yaml. Name is different setup to setup this file can with 01-netcfg.yaml and 50-cloud-init.yaml name.

To assign a static IP address on the network interface, open the 01-network-manager-all.yaml configuration file. Configuration file look like shown in below mention snapshot

Add the following entry into the file

  ethernets:
    enp0s31f6:
      dhcp4: no
      addresses:
        - 192.168.1.2/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8]

Now the whole file is mentioned in the code section

# Let NetworkManager manage all devices on this system
network:
  version: 2
  renderer: NetworkManager
  ethernets:
    enp0s31f6:
      dhcp4: no
      addresses:
        - 192.168.1.2/24
      gateway4: 192.168.1.1
      nameservers:
          addresses: [8.8.8.8]

Once done, save the file and apply the changes by running the following command:

sudo netplan apply

Verify

Use the ipconfig -a or ip a command to list all interface and check the IP address assigned or not

root@ubuntu:/home/gautam# ifconfig -a
enp0s31f6: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.2  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::cf94:fcc9:6b6d:eee7  prefixlen 64  scopeid 0x20<link>
        inet6 2a01:c22:b02f:d600:732c:f49c:37ed:87ad  prefixlen 64  scopeid 0x0<global>
        inet6 2a01:c22:b02f:d600:751:afc8:d3e1:dc1e  prefixlen 64  scopeid 0x0<global>
        ether 00:2b:67:2c:72:48  txqueuelen 1000  (Ethernet)
        RX packets 2299027  bytes 3054063698 (3.0 GB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 315996  bytes 52209921 (52.2 MB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
 

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 7100  bytes 698784 (698.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 7100  bytes 698784 (698.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:3d:22:8f  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

virbr0-nic: flags=4098<BROADCAST,MULTICAST>  mtu 1500
        ether 52:54:00:3d:22:8f  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

wwan0: flags=4226<BROADCAST,NOARP,MULTICAST>  mtu 1500
        ether 12:e0:d0:a0:10:60  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

root@ubuntu:/home/gautam# 

Summary

In this article, we have seen how to set up a static IP address from the command line as well as from the Netplan configuration file.

In case you face any issues don’t hesitate to comment.

11 thoughts on “How to add a Static IP address in Ubuntu 20.04 server”

  1. Superb post but I was wondering if you could write a litte more on this topic?
    I’d be very grateful if you could elaborate a little bit more.
    Appreciate it!

  2. Your style is very unique compared to other people I have read stuff from.
    I appreciate you for posting when you’ve got the opportunity, Guess I will just book mark this web site.

  3. Nice post. I was checking continuously this blog and I am impressed!
    Very useful information particularly the last part :
    ) I care for such information a lot. I was looking for this particular
    info for a long time. Thank you and best of luck.

  4. Your style is so unique in comparison to other folks I ave read stuff from. Thank you for posting when you have the opportunity, Guess I all just bookmark this page.

Leave a Comment

Your email address will not be published. Required fields are marked *