Linux

How to Configure a Static IP Address on Ubuntu 20.04

How to Configure a Static IP Address on Ubuntu 20.04

This article describes how to set a static IP address on Ubuntu 20.04.

Usually, in most network configurations, the IP address is assigned dynamically by the router’s DHCP server. Setting a static IP address may be required in different situations, such as configuring port forwarding or running a media server.

Static IP address configuration using DHCP

The easiest and recommended way to assign static IP addresses to devices on your LAN is to configure Static DHCP on your router. DHCP backup or static DHCP is a feature found in most routers that makes the DHCP server automatically assign the same IP address to a specific network device whenever the device requests an address from the DHCP server. It works by assigning a static IP to the unique MAC address of the device.

The steps for configuring DHCP backup vary from router to router. See the vendor documentation for more information.

Netplan

Ubuntu 17.10 and later uses Netplan as the default network management tool. Previous versions of Ubuntu used ifconfig and its / etc / network / interfaces configuration file to configure networking.

Netplan configuration files are written in the YAML syntax with the file extension .yaml. To configure a network interface with Netplan, you will need to generate a YAML description for the interface, and Netplan will generate the necessary configuration files for the selected renderer tool.

Netplan supports two renderers, NetworkManager and Systemd-networkd. Network Manager is mostly used on Desktop machines, whereas Systemd-networkd is used on servers without a GUI.

Configuring Static IP address on Ubuntu Server

In Ubuntu 20.04, the system identifies the network interface using the ‘predictable network interface name’.

The first step to setting up a static IP address is to identify the name of the ethernet interface you wish to configure. To do so, use the ip link command, as shown below:

ip link

The command prints a list of all available network interfaces. In this example, the interface name is ens3:

Output
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: 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 the / etc / netplan directory. You will probably find one or more YAML files in this directory. The file name may differ from setting to setting. Typically, the file is named 01-netcfg.yaml, 50-cloud-init.yaml, or NN_interfaceName.yaml, but it may be different on your system.

If your Ubuntu cloud instance comes with cloud-init, you will need to disable it. To do this create the following files:

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

To assign a static IP address on a network interface, open the YAML configuration file with your text editor:

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

/etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: yes

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

Every Netplan Yaml file starts with a network key which has at least the two required elements. The first element required is the network configuration format version, and the second is the device type. Device types can be ethernet, bond, bridge, or vlans.

The configuration above also has a line indicating the render type. Out of the box, if you install Ubuntu in server mode, the renderer is configured to use networkd as the back end.

Under device type (ethernets), you can specify one or more network interfaces. In this example, we only have one ens3 interface configured to get IP addressing from the DHCP 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. Below address: You can add one or more IPv4 or IPv6 IP addresses to assign to the network interface.
  • Specify a gateway.
  • Under Name servers, specify the name server IP address.

/etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens3:
      dhcp4: no
      addresses:
        - 192.168.121.221/24
      gateway4: 192.168.121.1
      nameservers:
          addresses: [8.8.8.8, 1.1.1.1]

When editing Yaml files, make sure you follow the YAML code indenting standards. If the syntax is incorrect, the changes will not be applied.

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

sudo netplan apply

Verify the changes by typing:

ip addr show dev ens3
Output
2: 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

There she is! You have assigned a static IP to your Ubuntu server.

Configuring Static IP address on Ubuntu Desktop

Setting up a static IP address on an Ubuntu Desktop computer requires no technical knowledge.

In the Activity screen, look for “settings” and click the icon. This will open the GNOME settings window. Depending on the interface you want to change, click on the Network or Wi-Fi tab. To open interface settings, click the gear icon next to the interface name.

In the “IPV4 Method” tab, select “Manual” and enter your static IP address, Netmask and Gateway. When done, click the “Apply” button.

1(3)

To verify changes, open your terminal either by using the keyboard shortcut Ctrl + Alt + T or by clicking on the terminal icon and running:

ip addr

The output will show the IP address of the interface:

Output
...
2: 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 on Ubuntu 20.04.

If you have any questions, feel free to leave a comment below.

Related posts

How to Install and Use PHP Composer on Ubuntu 20.04

Linux

How to Upgrade From Windows 7 to Linux

Howto

How to Install the Rudder System Audit Tool on Ubuntu 20.04

Linux

How to Install Flatpaks on Ubuntu 20.04

Linux

How to Install Django on Ubuntu 20.04 LTS

Linux

How to Install Chromium Web Browser on Ubuntu 20.04

Linux

How to Install Notepadqq on Ubuntu 20.04, a decent Notepad ++ alternative

Linux

How to install headless CAC Strapi Node.js

Abim

How to Destroy / Erase Data Safely on Hard Drive with Damaged on Linux

Linux