How to configure Static IP address in Ubuntu 20.04

How to configure Static IP address in Ubuntu 20.04

Typically, in most network configurations, IP addresses are dynamically assigned by a DHCP server. Static IP address setting may be required in various situations such as port forwarding configuration or media server implementation. Join us in this tutorial to introduce you to how to configure Static IP address in Ubuntu 20.04.

Static IP address configuration using DHCP:

The easiest and most recommended way to assign a static IP address to a device on your LAN is to configure static DHCP on your router. Static DHCP or DHCP reservation is a feature found on most routers that causes the DHCP server to automatically assign an IP address to a dedicated network device each time the device requests an address from the DHCP server. It works by assigning a static IP to a device’s unique MAC address. DHCP reservation configuration steps vary from router to router.

Netplan:

Ubuntu 17.10 and later versions use Netplan as the default network management tool. Previous versions of Ubuntu used ifconfig and its configuration file /etc/network/interfaces for network configuration. Netplan configuration files are written with YAML command with .yaml file extension. To configure a network card with Netplan, you need to create a YAML description for the network card, and Netplan will create the required configuration files for the selected provider tool. Netplan supports two providers, NetworkManager and Systemd-networkd. NetworkManager is mostly used on desktop machines, while Systemd-networkd is used on non-GUI servers.

Static IP address configuration on Ubuntu server:

In Ubuntu 20.04, the system identifies NICs using “predictable NIC names”. The first step to setting up a static IP address is to identify the name of the Ethernet card you want to configure. To do this, use the ip link command:

ip link

The command displays a list of all available network cards. In this example, the user card name is ens3:

۱: 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  ۲: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP mode DEFAULT group default qlen 1000      link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff

Netplan configuration files are stored in /etc/netplan directory. You will probably find one or more YAML files in this directory. The file name may be different in different systems. Usually the file name is 01-netcfg.yaml, 50-cloud-init.yaml or NN_interfaceName.yaml, but it may be different on your system. If your Ubuntu cloud instance has cloud-init, you need to disable it. To do this, create the following file:

sudo nano /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg
network: {config: disabled}

To assign a static IP address on the network card, 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

Before changing the config, let’s briefly explain the code.

Each Netplan Yaml file starts with a network key that contains at least two required elements. The first required element is the version of the network config template and the second is the device type. The device type can be ethernets, bonds, bridges, or vlans. Under Device type (ethernets), you can specify one or more network interfaces. In this example, we have only one ens3 interface configured to obtain an IP address from a DHCP4 server: dhcp4:yes.

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

  • Set DHCP to dhcp4:no.
  • Specify a static IP address. Under Addresses: you can add one or more IPv4 or IPv6 IP addresses that will be assigned to the network interface.
  • Specify the gateway.
  • Under nameservers, set the nameservers IP addresses.
network:  version: 2  renderer: networkd  ethernets:  ens3:  dhcp4: no  addresses:  - ۱۹۲٫۱۶۸٫۱۲۱٫۲۲۱/۲۴  gateway4: 192.168.121.1  nameservers:  addresses: [8.8.8.8, 1.1.1.1]

When editing Yaml files, make sure you follow YAML code indentation standards. If it is not true, the changes will not be applied. After finishing the work, save the file and apply the changes by running the following command:

sudo netplan apply
Confirm the changes by typing the following commands:
ip addr show dev ens3
۲: ens3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000      link/ether 08:00:27:6c:13:63 brd ff:ff:ff:ff:ff:ff      inet 192.168.121.221/24 brd 192.168.121.255 scope global dynamic ens3         valid_lft 3575sec preferred_lft 3575sec      inet6 fe80::5054:ff:feb0:f500/64 scope link          valid_lft forever preferred_lft forever

You have assigned a static IP to your Ubuntu server.

Fixed IP address configuration in Ubuntu desktop:

Setting up a static IP address on Ubuntu desktops requires no technical knowledge.

On the Activities page, search for “settings” and click on the icon. This will open the GNOME Settings window. Click on Network or Wi-Fi depending on the interface you want to change. Click the cog icon next to the interface name to open interface settings. In the “IPV4” tab, select the “Manual” method and enter your static IP address, Netmask and Gateway. When you are done, click the “Apply” button.

ip addr

The output shows the IP address of the network card:

...  ۲: wlp1s0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP group default qlen 1000      link/ether 50:5b:c2:d8:59:7d brd ff:ff:ff:ff:ff:ff      inet 192.168.121.221/24 brd 192.168.31.255 scope global dynamic noprefixroute wlp1s0         valid_lft 38963sec preferred_lft 38963sec      inet6 fe80::45e3:7bc:a029:664/64 scope link noprefixroute 

Conclusion :

We have shown you how to configure a static IP address in Ubuntu 20.04.

support hosting100