How to change IP address from DHCP to static in Ubuntu

I am a PHP programmer using Ubuntu for Linux, Apache, MySQL, and PHP (LAMP ) servers, and I need a static IP address on that server.

If for some reason the server is configured with Dynamic Host Configuration Protocol (DHCP) I will need to convert the IP configuration to static.

Under DHCP the router assigns an IP address to the device. This IP may change on subsequent reboots.

In this article, I will show you how to convert a DHCP configuration to a static IP configuration on Ubuntu server 24.04LTS.

I have a Windows laptop that runs VirtualBox and the Ubuntu server is a guest on VirtualBox.

The first thing to do is to determine the network interface name.

To do so I need to ensure VirtualBox and the Ubuntu server are up and running.

To access a Ubuntu server I use PuTTY to Secure Shell (SSH) into the server. This will require you to know the existing IP address or domain name of the server.

Since I created the server as a VirtualBox guest it is easy for me to gain (SSH) access by simply launching the server.

Step 1 is to log into the Ubuntu Server on the command line. Since this is a server it will not have a Graphical user interface (GUI).

Step 2 is to update the server’s repository. We do so by entering the below command on the command line.

sudo apt update

Step 3 is upgrading those packages. This means if there are packages that can be upgraded to a newer version they will be upgraded.

sudo apt upgrade

Step 4 is to remove any old and non-used packages and any old Kernels.

sudo apt autoremove

Step 4 is to install net-tools. Net-tools is a collection of networking utilities.

  • arp
  • hostname
  • ifconfig
  • ipmaddr
  • iptunnel
  • mii-tool
  • nameif
  • netstat
  • plipconfig
  • rarp
  • route
  • slattach

Of which we will be only using ifconfig.

sudo apt install net-tools

Step 5 is to run ifconfig on the command line.

This is the output from my Ubuntu server:

$ ifconfig

enp0s3: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
       inet 192.168.1.112  netmask 255.255.255.0  broadcast 192.168.1.255
       inet6 fe80::a00:27ff:fefd:65fc  prefixlen 64  scopeid 0x20<link>
       ether 08:00:27:fd:65:fc  txqueuelen 1000  (Ethernet)
       RX packets 726  bytes 347718 (347.7 KB)
       RX errors 0  dropped 0  overruns 0  frame 0
       TX packets 266  bytes 36629 (36.6 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

We need two bits of information 1) the network device is enp0s3, and 2) the IP address of this server is 192.168.1.112.

We can also use the command “ip a”. It will return something like this:

$ ip a

: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
    link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
    inet 127.0.0.1/8 scope host lo
       valid_lft forever preferred_lft forever
    inet6 ::1/128 scope host noprefixroute 
       valid_lft forever preferred_lft forever
2: enp0s3: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP group default qlen 1000
    link/ether 08:00:27:fd:65:fc brd ff:ff:ff:ff:ff:ff
    inet 192.168.1.112/24 brd 192.168.1.255 scope global enp0s3
       valid_lft forever preferred_lft forever
    inet6 fe80::a00:27ff:fefd:65fc/64 scope link 
       valid_lft forever preferred_lft forever

This tells us the same two things:

  1. For this server, the network device is enp0s3. Yours might be eth0 or something to that effect.
  2. The IP of this server is 192.168.1.112

Netplan is the default network management tool. Netplan was introduced to Ubuntu in 2016.

In the directory /etc/netplan/ we will find a file named something like “50-cloud-init.yaml”

From a DHCP configuration:

File: /etc/netplan/50-cloud-init.yaml

# 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: 2


Notice the line “ This file is generated from information provided by the datasource Changes to it will not persist across an instance reboot.”.

Then there is the second to the last line that says we must create a file /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg. And the last line says we must write “network: {config: disabled}” into that file and save it.

From a static configuration :

File: /etc/netplan/50-cloud-init.yaml

# 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:
    version: 2
    ethernets:
        enp0s3:
            addresses:
            - 192.168.1.83/24
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
                search: []
            routes:
            -   to: default
                via: 192.168.1.1

We change the server to a static IP by editing it’s configuration. To do so enter the following command: (not the file name might be different)

sudo vi /etc/netplan/50-cloud-init.yaml, and create a configuration like this:

network:
    version: 2
    ethernets:
        enp0s3:
            addresses:
            - 192.168.1.83/24
            nameservers:
                addresses:
                - 8.8.8.8
                - 8.8.4.4
                search: []
            routes:
            -   to: default
                via: 192.168.1.1

Replace:

  • 192.168.1.83/24 with your server’s IP and submask
  • 8.8.8.8 and 8.8.4.4 are Google’s name servers, which you can keep or use your own.
  • 192.168.1.1 is the gateway and you need to replace it with your own.

Then create the file /etc/cloud/cloud.cfg.d/99-disable-network-config.cfg and add to it “network: {config: disabled}” and save it.

Once you have completed these steps test the configuration with the command “netplan try”. If there are no errors, issue the command “netplan apply”.

To confirm the configuration is working, issue the command “ip a”. If this shows the correct IP address then power down your server and then power it on again. Issue the command “ip a”. If this shows the correct IP address, you have successfully changed your Ubuntu 24.04LTS network configuration from dynamic to static.

Note if your remote connection loses its connection, the changes you have made may have taken effect and you will need to establish a new remote connection.

Conclusion

In this article, we have taken an Ubuntu 24.04LTS server that was initially configured with a dynamic network connection configuration (DHCP) to a static IP. This required editing the network configuration and changing it to a static configuration. We also created a file that disabled the network from automatically reverting back to the original configuration.