8 C
Texas
angeloma
Senior Writer and partner

How to set a Static IP address on Ubuntu 20.04?

A server usually always has a static IP address that facilitates the rest of the network services. So although it is not mandatory if it is of great help for the management of the server. So this post is about how to define a static IP address in Ubuntu 20.04

A Static IP address

An IP address is an identifier held by each device that connects to the Internet or a computer network. In the case of the Internet, it must be unique to avoid connection conflicts.

On the one hand, there are dynamic IP addresses that change their value from time to time. Normally, these addresses are assigned by a DCHP server. The sysadmin does not have to worry about the assigned address as they are renewed from time to time.

On the other hand, we have static or fixed IP addresses, which unlike dynamical ones, do not change over time. In this case, it must be assigned and configured manually in the system.

- Advertisement -

Each of them has its own advantages, however, for internal networks, it is convenient to have equipment with static IP addresses. This facilitates the administration and routing of packets within the network. It is also easier to maintain the network.

So, let us work!

How to set up a static IP address on Ubuntu 20.04?

Before starting the work, it is necessary to know which is the network interface to which we want to assign the static IP address. To do this, execute the following command:

:~$ ip addr
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:80:31:48 brd ff:ff:ff:ff:ff:ff
inet 192.168.250.8/16 brd 192.168.255.255 scope global dynamic enp0s3
valid_lft 86204sec preferred_lft 86204sec
inet6 fdd4:a148:ea1:7a00:a00:27ff:fe80:3148/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 7006sec preferred_lft 3406sec
inet6 fe80::a00:27ff:fe80:3148/64 scope link
valid_lft forever preferred_lft forever
1.- Check the current IP address on Ubuntu 20.04
1.- Check the current IP address on Ubuntu 20.04

As you can see in the image, I only have a network interface called enp0s3 whose IP address is 192.168.250.8 but it was assigned with DHCP. So I’m going to assign it the static IP address 192.168.250.20

In Ubuntu the network configuration is done by netplan where the configurations are written in yml format.

So, before modifying the configuration file, it is convenient to make a backup of this file to prevent problems.

:~$ sudo cp /etc/netplan/00-installer-config.yaml /etc/netplan/00-installer-config.yaml.bak

And now you can modify the file with the help of nano.

:~$ sudo nano /etc/netplan/00-installer-config.yaml

You will see the following content:

network:
ethernets:
enp0s3:
dhcp4: true
version: 2

Then modify it to look like this:

dhcp4: no
addresses:
- 192.168.121.199/24
gateway4: 192.168.121.1
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
2.- Set a static IP address on Ubuntu 20.04
2.- Set a static IP address on Ubuntu 20.04

I think the options are very easy to understand but I will explain it to you anyway:

  • dhcp4: it is not so that the interface does not use DHCP and only uses the IP address we set.
  • addresses: The IP address that we are going to define.
  • gateway4: The gateway of the network
  • nameservers: the DNS servers that the computer will use.

Then save the changes and close the file. Remember that the file is in yml format so it respects the rules and syntax so it does not give any error.

To apply the changes, execute the following command:

:~$ sudo netplan apply

And check the changes with the following command:

:~$ ip addr
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: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
link/ether 08:00:27:80:31:48 brd ff:ff:ff:ff:ff:ff
inet 192.168.250.20/16 brd 192.168.255.255 scope global enp0s3
valid_lft forever preferred_lft forever
inet6 fdd4:a148:ea1:7a00:a00:27ff:fe80:3148/64 scope global dynamic mngtmpaddr noprefixroute
valid_lft 7148sec preferred_lft 3548sec
inet6 fe80::a00:27ff:fe80:3148/64 scope link
valid_lft forever preferred_lft forever
3.- Static IP address on Ubuntu 20.04
3.- Static IP address on Ubuntu 20.04

As you can see, everything went well.

Conclusion

Setting a static IP address to Ubuntu 20.04 is quite simple even from the terminal. With this you will be able to manage your server or computer within a network more easily.

Please share this post and join our Telegram channel.

- Advertisement -
Everything Linux, A.I, IT News, DataOps, Open Source and more delivered right to you.
Subscribe
"The best Linux newsletter on the web"

4 COMMENTS

  1. Watch out for the indentation error you made in the 00-installer-config.yaml.
    Should look like this:
    “”
    dhcp4: no
    addresses:
    – 192.168.121.199/24
    gateway4: 192.168.121.1
    nameservers:
    addresses: [8.8.8.8, 1.1.1.1]

  2. I don’t have a 00-installer-config.yaml file in my /etc/netplan directory. I only have a 01-network-manager-all.yaml file which does not have the same content. ???

LEAVE A REPLY

Please enter your comment!
Please enter your name here



Latest article