Change a Static IP Address in Ubuntu Using the Command Line | Change the IP Address in Ubuntu 24.04

This article will cover the steps necessary to change a static IP to another static IP.

I am a PHP developer and use virtual machines running Linux for PHP development and testing.

My configuration consists of a Windows 10 laptop with VirtualBox running on it. VirtualBox is virtualization software that allows one to add guest virtual machines (VMs).

In my case, I use this configuration to build website hosting configurations. I create these web hosting servers using Linux, Apache, MySQL, and PHP (LAMP).

What is an IP Address

Simply stated an IP address is the address of a device on a network and/or internet. An IP is like the address of your house or your phone number on your phone.

What Is a Static IP Address

A static IP is an IP address that does not change. There are two types of IP addresses. 1) There is dynamic which is given by the router and 2) static which is configured never to change.

The Benefits of a Static IP

In the case of web servers, they must have static IP addresses. The static IP is required because of how servers work and how other devices interact with them.

This article is about changing a static IP address on the Ubuntu 24.04LTS server.

Step 1 is to identify how the server is configured.

How to Determine What IP Address Your Server is Using

If you are using VirtualBox, once the VM is activated, you will be able to log in.  Once logged in you want to use the commands “ip a” and ifconfig”. Both provide the same information.

Ifconfig was not available on my server so I had to install it by installing “net-tools”.

To install net-tools issue the command:

sudo apt install net-tools

After installing net-tools you will be able to issue the command ifconfig.

The output from ifconfig :

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.1.80  netmask 255.255.255.0  broadcast 192.168.1.255
        inet6 fe80::a00:27ff:fe9a:8638  prefixlen 64  scopeid 0x20<link>
        ether 08:00:27:9a:86:38  txqueuelen 1000  (Ethernet)
        RX packets 478  bytes 330373 (330.3 KB)
        RX errors 0  dropped 1  overruns 0  frame 0
        TX packets 165  bytes 18169 (18.1 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        inet6 ::1  prefixlen 128  scopeid 0x10<host>
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 96  bytes 7624 (7.6 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 96  bytes 7624 (7.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

The command “ip a” will provide the same information, just in a different format.

An Example of a Dynamic (DHCP) Configuration

Log into your server.

Issue the following command: “cd /etc/netplan” and press the enter key.

Then issue the command “vdir” and press the enter key.

There should be a file that ends in .yaml in that directory.

Mine is :  50-cloud-init.yaml

At that point enter “sudo vi 50-cloud-init.yaml”. Make sure you use your file’s name in place of mine.

Sudo allows you to become the root user/superuser. Vi is my editor of choice.

At this point, the configuration file will be opened in vi.

Your file should look very similar to the following:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            dhcp4: true
    version: 

If your file looks like this one your network is configured for dynamic IP allocation or Dynamic Host Configuration Protocol (DHCP). This means you will need to read and follow this article : How to Change IP Address From DHCP to Static in Ubuntu

An Example of a Static Configuration

Log into your server.  

Issue the following command : “cd /etc/netplan” and press the enter key.

Then issue the command “vdir” and press the enter key.

There should be a file that ends in .yaml in that directory.

Mine is :  50-cloud-init.yaml

At that point enter “sudo vi 50-cloud-init.yaml”. Make sure you use your file’s name in place of mine.

Sudo allows you to become the root user/superuser. Vi is my editor of choice.

At this point, the configuration file will be opened in vi.

Your file should look very similar to the following:

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            addresses:
            - 192.168.1.80/24
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
                search: []
            routes:
            -   to: default
                via: 192.168.1.1
    version: 2

This is the configuration for a static IP address.

Backup Current Network Configuration Before Starting

It is a good habit to get into – backing up your configuration files before changing them.

This is easy to do. For example, file 50-cloud-init.yaml is easily backed up by just making a copy of the file with a change to the file name. Here is the command :

sudo cp 50-cloud-init.yaml 50-cloud-init.yaml-original

Cp is the copy command. Notice I added “-original” to the back up file name.  

So now we should have two files in the directory :

1) 50-cloud-init.yaml – the working configuration file

2) 50-cloud-init.yaml-original – the original configuration file that is a backup of the original configuration.

You can verify by using the vdir command, which lists the contents of the directory we are in.

Making a backup of the original backup file could save you some pain if you need to figure out how the server was originally configured.

Changing a Static IP

This is a two-step process assuming your server is already configured with a static IP.

Step 1 :

Issue the command “sudo vi /etc/netplan/50-cloud-init.yaml” on the command line. Replace your file name for mine.

You can make several changes to this file :

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}
network:
    ethernets:
        enp0s3:
            addresses:
            - 192.168.1.80/24
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
                search: []
            routes:
            -   to: default
                via: 192.168.1.1
    version: 2

1) Change the IP of the server by replacing the current IP address. This file shows an IP of 192.168.1.80.  Change it to what you would like it to be.

2) Change the name servers. This file shows Google’s name servers of 8.8.8.8 and 8.8.4.4. You can change them to other name server values.

3) Change the gateway which in this file is 192.168.1.1. Change it to another gateway if need be.

4) The subnet can be modified by editing the subnet that is part of the IP address. In this case, the IP along with the subnet is 192.168.1.80/24. Notice the “/24”. That is the subnet for this configuration. You can modify the “/24” to increase or decrease the number of IP addresses that are available in the configured network.

Step 2 :

In the static configuration file “/etc/netplan/50-cloud-init.yaml” you will notice that there is some notes. These notes must be followed.

# This file is generated from information provided by the datasource.  Changes
# to it will not persist across an instance reboot.  To disable cloud-init's
# network configuration capabilities, write a file
# /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg with the following:
# network: {config: disabled}

This can be accomplished by issuing the command “sudo vi /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg”.

If the file is empty add the line : “network: {config: disabled}” and save it.

If you are using vi press the “i” key to place vi into insert mode, then copy and paste the line. Then press the escape key to place vi into command mode. Then press the colon key followed by “wq”.

The “wq” is for write and quit.

At this point issue the command “sudo netplan try”. If there are no errors you can issue “sudo netplan apply”.  

If there are no errors you have success.

Potential Error That Might Occur During This Process

If your configuration file is not indented properly, you will get an error.

Here is what it might look like :

Error in network definition: expected mapping (check indentation)

ethernets:”

The error tells you where the error is located. In this case, it is an indentation error.

Edit the configuration file and verify it is configured correctly.  

Then issue the command “sudo netplan try”. If all is correct you might see a message that the configuration will automatically be accepted.

If not issue the command “sudo netplan apply”.

Verifying the IP Has Been Changed

To verify the configuration has taken place you can issue the commands “ip a” and/or  “ifconfig”.

I would recommend that you power off your server and then power it back on and the verifying the IP is correct.

Conclusion

In this article, we learn what an IP is, why it is important, and how to manage IP addresses on an Ubuntu 24.04LTS server. We learned how to identify a static and a dynamic configuration. Then we learned how to change the static IP from one address to another.