A machine or other devices connected to your router usually have a dynamic IP address. The DHCP server will assign a dynamic IP address for your device connected to the network. That same device will likely get a different IP address when connected to the network later. To get a static IP address for your machine, we need to configure it. Netplan has been the default tool for configuring Linux networking on Ubuntu machines since 2016. It is a utility designed to make network configuration easier and more descriptive. This article will show you how to configure a static IP address on Ubuntu 24.04.
Table of Contents
Prerequisites
- Ubuntu 24.04 VPS with at least 1GB of RAM
- 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
Without proper configuration, Netplan remains inactive. The most basic configuration example for enabling DHCP on workstations is as follows:
network:
version: 2
renderer: NetworkManager
This configuration allows Netplan to delegate control to NetworkManager, which will manage all devices according to its default settings. Once a carrier signal is detected, any Ethernet device will be activated with DHCP.
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 the ethernet interfaces we want to configure.
# ip link
We should see an output like the following, which lists all the available network interfaces: one is a loopback interface, and the others are ethernet interfaces.
1: lo: 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: mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether 00:16:3e:a2:fd:a2 brd ff:ff:ff:ff:ff:ff
altname enp0s3
For example, we will use the ens3 interface. lo is a loopback interface and cannot be used for this purpose.
Step 2. Edit Netplan Configuration File
Installing Netplan will automatically create YAML files (.yaml) in /etc/netplan. Netplan configuration files are located in the /etc/netplan/ directory. 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.
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:
ens3:
dhcp4: true
That is actually the content of /etc/netplan/50-cloud-init.yaml
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/50-cloud-init.yaml. Please note, when editing a YAML file, make sure you follow the YAML code indentation standards. The suggested syntax for YAML files 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/50-cloud-init.yaml
Under the ethernet section, add the following configuration line: Replace ens3 with the ethernet interface name that we want to update.
network:
ethernets:
ens3:
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
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.
Step 3. Apply changes
Before applying the changes, we can run this command below for a dry run.
# netplan try
We can run the following command to apply the changes if no error is reported.
# netplan apply
Run the following command to verify the configuration. Replace ens3 with the ethernet interface that we want to modify.
# ip addr show dev ens3
Please note: Be careful when applying the changes you made. If you follow this article and apply it to your server, your server may lose its networking. So, make sure to follow the steps above with caution.
If you want to configure your Ubuntu desktop to get a static IP address, follow our blog post at https://www.rosehosting.com/blog/how-to-configure-static-ip-address-on-ubuntu-20-04/.
You’ve Learned How to Configure Static IP Address on Ubuntu 24.04
That’s it. You have learned how to configure static IP addresses on Ubuntu 24.04.
Of course, you don’t have to learn how to configure a static IP address on Ubuntu 24.04 if you use one of our Ubuntu VPS Hosting services. In that case, you can ask our expert Linux admins to do it for you. They are available 24×7 and will take care of your request immediately.
If you liked this post on how to configure static IP address on Ubuntu 24.04, please share it with your friends or leave a reply below. Thanks.