A dynamic IP address is an IP address automatically allocated to a device on a network by a DHCP (Dynamic Host Configuration Protocol) server. Unlike a static IP address, which must be manually configured and remains constant, a dynamic IP address can change each time a device connects to the network or after a specified period. Dynamic IP addresses are temporary and can be reused by other devices after the device is disconnected from the network. To get a static IP address for your machine, we need to configure it. From Ubuntu 17.10 to the most recent version, Ubuntu 26.04, the network configuration utility now uses netplan. This article will show you how to install a static IP address on Ubuntu 26.04.
Prerequisites
- Ubuntu 26.04 VPS
- SSH root access or a regular system user with sudo privileges
Conventions
# – given commands should be executed with root privileges either directly as a root user or by use of sudo command
$ – given commands should be executed as a regular user
What is a Dynamic IP Address?
A dynamic IP address is an address assigned automatically by a DHCP server and can change each time a device connects to the network or after a specified period. Dynamic IP addresses reduce the need for manual configuration and are often used for devices that don’t require a fixed IP address, such as personal computers or mobile devices.
What is a Static IP Address?
A static IP address is an IP address that is manually assigned to a device and does not change. Once configured, this IP address remains the same every time the device connects to the network. Static IP addresses are often used for devices that require a fixed IP address, such as servers or network printers, which must be accessible at a consistent address.
When specific configurations for individual interfaces are provided, devices will not automatically activate using DHCP. Instead, each interface must be explicitly defined in a file in /etc/netplan/, complete with its corresponding YAML settings for either the networkd or NetworkManager backend renderers.
So, let’s complete these steps to configure a static IP address on Ubuntu using Netplan.
Step1. Identify Ethernet Interfaces
Run the following command to identify which Ethernet interface we want to configure.
# ip link
The command above will print the result below, which lists all the available network interfaces: one is a loopback interface, and the others are Ethernet interfaces.
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: ens33: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DEFAULT group default qlen 1000
link/ether 00:0c:29:ee:3a:cb brd ff:ff:ff:ff:ff:ff
altname enp2s1
For example, in this case, we will use the ens33 interface. lo is a loopback interface and cannot be used for this purpose.
Step 2. Edit Netplan Configuration File
Netplan is a new network configuration utility introduced in Ubuntu 17.10 that reads YAML files and generates all the configurations for the renderer tool (NetworkManager or networkd). Netplan reads the network configuration from /etc/netplan/*.yaml. Installing Netplan will automatically create YAML files (.yaml) in /etc/netplan. Within this directory, you may see files such as 01-netcfg.yaml, 50-cloud-init.yaml, or other similar names, contingent upon your specific configuration.
For example, on this server, we can see the /etc/netplan/00-installer-config.yaml file. Let’s see what’s inside the file.
# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
ethernets:
ens33:
dhcp4: true
dhcp6: true
match:
macaddress: 00:0c:29:ee:3a:cb
set-name: ens33
version: 2
That’s the content of our /etc/netplan/00-installer-config.yaml file. If there is no YAML file in /etc/netplan on the system, run the following command to create it.
# netplan generate
Now, to check the existing netplan configuration, we can execute this command:
# netplan get
The command will return an output like this:
network:
version: 2
renderer: networkd
ethernets:
ens33:
dhcp4: true
We can edit the files available in /etc/netplan. Now, to configure a static IP address, we need to modify the YAML configuration file at /etc/netplan/00-installer-config.yaml. Please note that when editing a YAML file, make sure you follow the YAML code indentation standards. The recommended YAML syntax is to use 2 spaces for indentation; do not use TABS. The changes will not be applied if the indentation and syntax are incorrect.
# nano /etc/netplan/00-installer-config.yaml
Under the Ethernet section, add the following configuration line: Replace ens3 with the Ethernet interface name that we want to update.
network:
ethernets:
ens33:
addresses:
- 192.168.100.55/24
dhcp6: false
routes:
- to: default
via: 192.168.100.1
nameservers:
addresses:
- 192.168.100.1
- 1.1.1.1
search: []
optional: true
version: 2
This is a YAML file; make sure to use spaces and not tabs. Save and close the file. The information below shows you a detailed breakdown of the configuration file.
network: This is main part of the configuration file
ethernets: This section specifies that the configuration is for Ethernet interfaces. It’s the main key under which individual Ethernet interfaces are defined.
ens3: this is the ethernet address of your machine, you can check yours with the ip link command
addresses: Specify a static IP address range under addresses: You can also add one or more IPv4 or IPv6 IP addresses to assign to the network interface. We can also mention the IP range.
dhcp4: no and dhcp6: no, meaning DHCP is disabled
nameserver: Set the IP address of the DNS server.
Also, make sure you know your gateway IP address. Using an incorrect IP address as the gateway can make your computer inaccessible when you want to remote it.
Step 3. Apply changes
After making changes to the Netplan configuration file, we need to apply them to make them take effect. However, before applying the changes, we can run the command below for a dry run to check if everything is okay.
# netplan try
We can then execute the following command to apply the changes if no error is reported.
# netplan apply
Run the following command to verify the configuration. You can replace ens33 with the Ethernet interface you want to modify.
# ip addr show dev ens33
or, simply
# ip a
Please note: Be careful when applying the changes you made. If you follow this article and apply it to your server, it may lose its network connectivity. So, make sure to follow the steps above with caution.
Conclusion
That’s it. You have learned how to install static IP addresses on Ubuntu 26.04. You should now be able to switch from a dynamic IP address to a static IP address.
If you liked this post on how to install a static IP address on Ubuntu 26.04, please share it with your friends or leave a comment below.